Bind array

      In the actual development, you may encounter a request needs to be passed to the front of background one or more parameters of the same name (such as bulk delete), such a situation by way of simple data binding is clearly inappropriate.

 

How to handle this type of data requests it? 

       For the above case, if all the same type of request parameters package into an array, the background can bind received.

Next, a batch delete user examples to explain in detail the operation using bind array:

1. Create a Web project, and import-related Jar package

2. Create a Spring MVC configuration file and configure the components scanner and parser view

<?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. The front end configuration information Spring MVC controller in 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/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. Create a list of user information 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}/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. The method of preparation of the mass delete user class processor 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. console information

 

6. Start project, visit http: // localhost: 8081 / chapter13 / deleteUsers

 

 

 

7. Address Chinese distortion is added in web.xml ((note that the parameter value be set forceEncoding true; preferably in front of 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>  

 

to form form submission must post, get the following encoding filter way spring effect afford

<%@ 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">


 

 

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

Guess you like

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