在实际开发时,可能会遇到前端请求需要传递到后台一个或多个相同名称参数的情况(如批量删除),此种情况采用简单数据绑定的方式显然是不合适的。
如何处理这种数据类型的请求呢?
针对上述这种情况,如果将所有同种类型的请求参数封装到一个数组中,后台就可以进行绑定接收了。
接下来,以一个批量删除用户的例子来详细讲解绑定数组的操作使用:
1.创建Web项目,并导入相关Jar包
2.创建Spring MVC配置文件,并配置组件扫描器和视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"
default-autowire="byName">
<context:component-scan base-package = "com.itheima.controller"/>
<bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
</beans>
3.在web.xml中配置Spring MVC的前端控制器等信息
<?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/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.创建一个用户信息列表页面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}/deleteUsers" method = "post">
<table width = "20%" border = 1>
<tr><td>选择</td><td>用户名</td></tr>
<tr><td><input name = "ids" value = "1" type = "checkbox"></td><td>tom</td></tr>
<tr><td><input name = "ids" value = "2" type = "checkbox"></td><td>jack</td></tr>
<tr><td><input name = "ids" value = "3" type = "checkbox"></td><td>lucy</td></tr>
</table>
</form>
</body>
</html>
5.在处理器类中编写批量删除用户的方法Delete.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;
@Controller
public class Delete {
@RequestMapping("/deleteUsers")
public String deleteUsers(Integer[] ids) {
if(ids != null) {
for(Integer id : ids) {System.out.println("删除了id为"+id+"的用户!");}
}else {System.out.println("ids = null");}
return "success";
}
}
5.控制台信息
6.启动项目,访问http://localhost:8081/chapter13/deleteUsers
7.解决中文乱码则在web.xml中添加((注意,需要设置forceEncoding参数值为true;最好在web.xml的最前面))
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 对所有请求有效 -->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <form:form modelAttribute="person" action="action32">