18SpringMvc_在業務控制方法中收集陣列引數

weixin_34162629發表於2016-08-09

這篇文章我們要解決的問題的多選框選中,並批量刪除。

比如:

給出案例:

案例結構:

第一步編寫首頁面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

 

相關文章