zoukankan      html  css  js  c++  java
  • 18SpringMvc_在业务控制方法中收集数组参数

    这篇文章我们要解决的问题的多选框选中,并批量删除。

    比如:

    给出案例:

    案例结构:

    第一步编写首页面emp.jsp,代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      <body>
    <form action="${pageContext.request.contextPath}/user/register.action"  method="post">
    
    <table border="2" align="center">
    <tr>
        <th>编号</th>
        <th>姓名</th>
         
    </tr>
    <tr>
       <td><input type="checkbox" name="id" value="1"  ></td>
       <td> 哈哈</td>
    </tr>
    <tr>
       <td><input type="checkbox" name="id" value="2"  ></td>
       <td> 呵呵</td>
    </tr>
    <tr>
       <td><input type="checkbox" name="id" value="3"  ></td>
       <td> 爱爱</td>
    </tr>
    <tr>
       <td><input type="checkbox" name="id" value="4"  ></td>
       <td> 小美</td>
    </tr>
    <tr>
       <td><input type="submit" value="删除"  ></td>
       
    </tr>
    </table>
    
    
    
    </form>
      </body>
    </html>

    第二步:编写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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>SpringMvc_10day_self</display-name>
      <!-- Spring提供了一个Filter专门用来解决Post提交中文的乱码问题 -->
       <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>
      <!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头,
      
      所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差)
      
      -->
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- 通知DispatcherServlet去指定目录下找到springmvc.xml配置文件 -->
     <!-- 
     注意这里的  <param-name>contextConfigLocation</param-name>一个字母都不能有错
     一旦有错就会去WEB-INF下面去找
      -->
              <init-param>
                   <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:spring.xml</param-value>
              </init-param>
     </servlet>
     <servlet-mapping>
       <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
     
     </servlet-mapping>
    
     
      <welcome-file-list>
       
        <welcome-file>index.jsp</welcome-file>
      
      </welcome-file-list>
    </web-app>

    第三步;编写spring.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          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
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
    <import resource="com/guigu/shen/Action9/springmvc_009.xml"/>
    </beans>

    第四步:编写springmvc_009.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          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
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
         <!-- 控制器(程序员)(必须配置) -->
    <context:component-scan base-package="com.guigu.shen.Action9"/>
      <!-- 基于注解的映射器(可选) 
      这个类和以前的xml方式的类不同,专门是注解用的
      -->
          <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
          
          <!-- 基于注解的适配器(可选)
            这个类和以前的xml方式的类不同,专门是注解用的
           -->
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
          
          <!-- 视图解析器(可选) 
            这个类和以前的xml方式的类一样
          -->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       
       </bean>
    
    
    </beans>

    第五步:编写控制器类UserAction.java

    package com.guigu.shen.Action9;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.omg.CORBA.PUBLIC_MEMBER;
    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.ServletRequestDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    /**
     * 
     * 
    请求路径可以拆分为:根模块的名字+分模块的名字
    就是相当于当访问http://127.0.0.1:8080:项目名/user/register时就会进入到
    registerMethod方法。
     */
    @Controller
    @RequestMapping(value="/user")//根模块的请求名字
    public class UserAction {
        
    @RequestMapping(method=RequestMethod.POST,value="/register")//分模块的请求名字
    /*用数组的方式去收集提交的参数(搜集复选框选中的数据)
     */
    public String deleteall(Model model,int[] id)
    {
        
        System.out.println("UserAction:deleteall");
        //输出被选中的框框
        for(int i=0;i<id.length;i++)
        {
            
            System.out.println(id[i]);
            
        }
    
        
        model.addAttribute("message", "批量删除成功");
        
     return "/jsp/success.jsp";
    }
    
    
    
    
    }

    测试:正常:

    UserAction:deleteall
    1
    2
    3

  • 相关阅读:
    神经网络梯度下降的推导
    在虚拟环境里安装TensorFlow-cpu完成Ng作业
    利用anaconda配置虚拟环境
    Java基础(三)面向对象(下)
    Java基础(二)面向对象(上)
    Java基础(一)
    《深入理解计算机系统》阅读笔记--程序的机器级表示(上)
    《深入理解计算机系统》阅读笔记--信息的表示和处理(下)
    《深入理解计算机系统》阅读笔记--信息的表示和处理(上)
    《深入理解计算机系统》阅读笔记--计算机系统漫游
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5754845.html
Copyright © 2011-2022 走看看