Spring MVC--绑定数组和集合(四)

(一)绑定集合

参考:Spring MVC官方文档

在批量修改用户操作时,前端请求传递过来的数据可能是各种类型的数据,比如int,String的Java基本数据类型时使用数据无法实现数据绑定,因为数组只能绑定相同数据类型的数据,所以前端请求不同数据类型的数据时,我们使用集合进行数据绑定。
绑定集合的类型有:List、Set、Map等等。

1.实例演示

01.项目结构
这里写图片描述
02.HelloController控制类

package com.wang;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {

    @RequestMapping("/test")
     public String UpdateUsers(UserVO uv) {
        List<User> userlist=uv.getUserlist();
        for(User user:userlist) {
             if(user.getName()!=null) {
                 System.out.println("编号:"+user.getId()+"\n"+user.getName());
             }
        }
        return "hello";
    }
 }

03.User.java

package com.wang;
public class User {
    private int id;
    private String name;
    ---省略setter和getter方法---
    } 

04.UserVO.java

package com.wang;
import java.util.List;
public class UserVO {

    private List<User> userlist;

    public List<User> getUserlist() {
        return userlist;
    }

    public void setUserlist(List<User> userlist) {
        this.userlist = userlist;
    }

}

05.index.jsp

<body>
    <form action="${pageContext.request.contextPath }/test"method="post">
        <table width="30%" border=1>
            <tr>
                <td>选择</td>
                <td>用户名</td>
            </tr>
            <tr>
              <td>
                <input name="userlist[0].id" value="1" type="checkbox" />
              </td>
              <td>
              <input name="userlist[0].name" value="张三" type="text" />
              </td>
            </tr>
            <tr>
              <td>
                <input name="userlist[1].id" value="2" type="checkbox" />
              </td>
              <td>
              <input name="userlist[1].name" value="李四" type="text" />
              </td>
            </tr>
        </table>
        <input type="submit" value="修改" />
    </form>
</body>

06.HelloWeb-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!--扫描com.wamg包下的所有类-->
   <context:component-scan base-package="com.wang" />

   <!--配置视图解析器-->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <!--前缀是/WEB-INF/jsp/-->
      <property name="prefix" value="/WEB-INF/jsp/" />

      <!-- 后缀以.jsp结尾 -->
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

07.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.4">
  <display-name>Spring MVC Application</display-name>
  <!-- 配置字符编码过滤器 -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>HelloWeb</servlet-name>
    <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloWeb</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

(二)绑定数组

在实际开发中,如果前端请求需要传递多个相同名称的参数值时,在控制类中的处理方法使用数组进行接收。
01.比如前端页面:

<body>
   <form action="${pageContext.request.contextPath}/test" method="post">
      <table width="30%" border="1">
          <tr>
              <td>选择</td>
              <td>用户名</td>
          </tr>
          <tr>
              <td><input name="ids" value="1" type="checkbox"/></td>
              <td>张三</td>
          </tr>
           <tr>
              <td><input name="ids" value="2" type="checkbox"/></td>
              <td>李四</td>
          </tr>
           <tr>
              <td><input name="ids" value="3" type="checkbox"/></td>
              <td>王五</td>
          </tr>
      </table>
      <input type="submit" value="删除">
   </form>
</body>

02.控制类的处理方法参数使用数组接收数据

package com.wang;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {

    @RequestMapping("/test")
     public String deleteUsers(int[] ids) {
        for(int id:ids) {
            System.out.println(id);
        }
        return "hello";
    }
 }

猜你喜欢

转载自blog.csdn.net/weixin_36279318/article/details/79899871
今日推荐