Binding collection

      In batch delete operation by the user, the front end passes the request user id parameter of the same name, as long as the same type of array used in the background binding of reception parameters, can be accomplished by the deletion process in a cyclic manner array parameter.

       However, if the bulk edit data is then operated by a user, the request is transmitted over the front end might include various types of data, such as Integer, String like.

 

How to handle this type of data requests it?

      For the above case, you can use a set of data binding. That is the definition of a class in the package contains a collection of information such as the user, then in the method of receiving a set of parameters that define the type of packaging.

 

Next to bulk modify users, for example, to explain the use of a set of data binding:

(Here import jar package, written in web.xml, written in spring-config.xml not go into details, you can also look at a few of my former blog, above all presentation)

1. Create a wrapper class POJO, the user set information package UserVO.java

package com.itheima.controller;

import java.util.List;
import org.apache.catalina.User;


public class UserVO {
	private List<User> users;
	public List<User> getUsers(){
		return users;
	}
	
	public void setUsers(List<User> users) {
		this.users = users;
	}

}

2. The method of preparation of bulk edit user class processor AlterUsers.java

package com.itheima.controller;

import org.apache.catalina.User;
import org.springframework.expression.spel.ast.Indexer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
public class AlterUsers {
    @RequestMapping("/editUsers") 
	public String editUsers(UserVO userList) {
    	List<User> users = userList.getUsers();
    	if(users != null) {
    	for(User user:users) {
    		if(user.getName() != null) {
    			System.out.println("修改了id为"+user.getName()+"用户名为:"+user.getUsername());
    		}
    	}
    	}
    	
    	return "success";
    }
}

3. Write bulk edit user page success.jsp

     <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Result page</title>
  </head>

        
<body>
   <form action = "${pageContext.request.contextPath}/editUsers" method = "post">
        <table width = "30%" border = 1>
           <tr><td>choose</td><td>username</td></tr>
           <tr><td><input name = "users[0].id" value = "1" type = "checkbox"/></td>
             <td><input name = "users[0].username" value = "tome" type = "text"/></td></tr>
               <tr><td><input name = "users[1].id" value = "2" type = "checkbox"/></td>
              <td><input name = "users[1].username" value = "jack" type = "text"/></td></tr>
              
        </table>
        <input type = "submit" value = "Edit"/>
   </form>

  </body>
     </html> 


4. Console Information

5. Start project, visit http: // localhost: 8081 / chapter13 / eiditUsers

 

Published 376 original articles · won praise 172 · views 90000 +

Guess you like

Origin blog.csdn.net/Eider1998/article/details/104206709