19SpringMvc_在业务控制方法中收集List集合中包含JavaBean参数

本文要实现的功能是给一张表单:

 

可以看到这样表格一共有四行,每一行代表一个员工(User),每一个员工有username和salary。我们要做的是把这四个员工信息装进一个List集合中。

那么怎么做呢?

List不就是一个数组吗?

我们这么考虑:

 

 

案例结构如下:

第一步编写首页面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">
<!-- 

尤其要注意这里的name="userList[0].usrname"这种写法,视屏的解释是这个userList[0]
相当于emp,把userList当做数组来处理,java中并没有List[0]这种写法,所以区别开,
顺便说一下这里的流程,当看到userList这个字段时自然会找到UserAction中的
public String deleteall(Model model,Bean bean)中的Bean的中userList
注入数据

 -->
姓名:<input type="text" name="userList[0].usrname" value="哈哈"/>
性别:<input type="text" name="userList[0].salary" value="100"/>
姓名:<input type="text" name="userList[1].usrname" value="哈哈2"/>
性别:<input type="text" name="userList[1].salary" value="100"/>
姓名:<input type="text" name="userList[2].usrname" value="哈哈3"/>
性别:<input type="text" name="userList[2].salary" value="100"/>
<tr>
 
<input type="submit" value="提交">
</tr>



</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.ArrayList;
import java.util.Date;
import java.util.List;

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")//分模块的请求名字
/*用数组的方式去收集提交的参数(搜集复选框选中的数据)
 */
/*
 * 
 * 这里为什么写Bean而不写其他的比如userList,因为我这里得到的是userlist,但是我对他进行
 * 一封装了,封装成了bean,所以我这里当然要写bean了,记住如果是封装的形式,这里写的是封装,要比
 * 要得到的那个大。
 * 
 */
public String deleteall(Model model,Bean bean)
{
    for(Emp emp:bean.getUserList())
    {
        System.out.println(emp.getUsrname());
        System.out.print(emp.getSalary());
        
    }

    
 return "/jsp/success.jsp";
}




}

结果:控制台输出:

哈哈
100.0哈哈2
100.0哈哈3
100.0

一切正常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值