springmvc参数封装3

接受包装类型

定义包装类

package cn.itcast.domain;

public class UserCustom {
	private User user;

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}	
}


写接受方法

	//接受包装类型
	@RequestMapping("receiveUserCustom")
	public String receiveUserCustom(UserCustom user){
		System.out.println(user);
		return "success";
	}


写form表单

<form action="${pageContext.request.contextPath }/user/receiveUserCustom.do">
	姓名<input type="text" name="user.username" id="username">
	年龄<input type="text" name="user.age" id="age">
	生日<input type="text" name="user.birthday" id="birthday">
	地址<input type="text" name="user.address" id="address">
	<input type="submit" value="user包装提交">
</form>


写跳转方法

@RequestMapping("toAdd")
public String toAdd(){
return "add" ;
}


接受list集合类型

定义包装类

package cn.itcast.domain;

import java.util.List;

public class UserCustomList {
	private User user;
	private List<User> userList;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	

}


写接受方法

	//接受list或者map等集合类型
	@RequestMapping("receiveUserCustomList")
	public String receiveUserCUstomList(UserCustomList user){
		System.out.println(user);
		return "success";
	}


写form表单

<form action="${pageContext.request.contextPath }/user/receiveUserCustomList.do">
	姓名<input type="text" name="userList[0].username" id="username">
	年龄<input type="text" name="userList[0].age" id="age">
	姓名<input type="text" name="userList[1].username" id="username">
	年龄<input type="text" name="userList[1].age" id="age">	
	<input type="submit" value="集合包装提交">
</form>


写跳转方法

	@RequestMapping("toAdd")
	public String toAdd(){
		return "add" ;
	}

接受map集合类型

定义包装类

	private Map<String, Object> mps = new HashMap<String, Object>() ;
	
	public Map<String, Object> getMps() {
		return mps;
	}
	public void setMps(Map<String, Object> mps) {
		this.mps = mps;
	}


写接受方法

	//接受map集合类型
	@RequestMapping("receiveUserCustomMap")
	public String receiveUserCustomMap(UserCustom user){
		System.out.println(user);
		return "success";
	}



写form表单

<form action="${pageContext.request.contextPath }/user/receiveUserCustomMap.do">
	姓名<input type="text" name="mps['username']" id="username">
	年龄<input type="text" name="mps['age']" id="age">	
	<input type="submit" value="Map集合包装提交">
</form>


所有文件

1新建项目


2web.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_2</display-name>
    <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.xml</param-value>
  </init-param>  
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping> 
  
  <filter>
  	<filter-name>characterEncoding</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>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>




3配置文件springmvc.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.itcast"></context:component-scan>


<!-- 配置注解处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

<!-- 配置注解处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>


  <!-- 配置视图解析器 -->
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsps/"></property>
	<property name="suffix" value=".jsp"></property></bean>                     
</beans>


4代码

执行类Controller

package cn.itcast.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.itcast.domain.User;
import cn.itcast.domain.UserCustom;
import cn.itcast.domain.UserCustomList;

@Controller
@RequestMapping("/user")
public class UserController {
	//@RequestMapping("hello") //请求映射
	//@RequestMapping(value="/hello.do")
	//@RequestMapping(value="/hello.do", method=RequestMethod.GET)
	@RequestMapping(value="/hello.do", method={RequestMethod.GET , RequestMethod.POST})
	public String myHello(){
		return "hello" ; //返回逻辑视图
	}
		
//接受int类型参数
	@RequestMapping("receiveInt")
	public String receiveInt(Integer id){
		System.out.println("id=" + id);
		return "success" ;
	}
	
	@RequestMapping("toAdd")
	public String toAdd(){
		return "add" ;
	}
	
//接受String类型参数
	@RequestMapping("receiveString")
	public String receiveString(String username){
		System.out.println("name = " + username);
		return "success" ;
	}
//接受javaBean对象类型
	@RequestMapping("receiveUser")
	public String receiveUser(User user){
		System.out.println(user);
		return "success" ;
	}
	//接受数组
	@RequestMapping("receiveArray")
	public String receiveArray(Integer[] ids){
		System.out.println(ids);
		return "success" ;
	}
	//接受包装类型
	@RequestMapping("receiveUserCustom")
	public String receiveUserCustom(UserCustom user){
		System.out.println(user);
		return "success";
	}
	//接受list或者map等集合类型
	@RequestMapping("receiveUserCustomList")
	public String receiveUserCUstomList(UserCustomList user){
		System.out.println(user);
		return "success";
	}
	
	
 
}

域类


User

package cn.itcast.domain;

import java.util.Date;

public class User {
	private String username;
	private int age;
	private Date birthday;
	private String address;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	

}

UserCustom

package cn.itcast.domain;

public class UserCustom {
	private User user;

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}	
}


UserCustomList

package cn.itcast.domain;

import java.util.List;

public class UserCustomList {
	private User user;
	private List<User> userList;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getUserList() {
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}
	

}

页面

表单页面 add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/receiveInt.do">
	ID:<input type="text" name="id" id="id">
	<input type="submit" value="提交">
</form>
<hr color="blue" size="12">

<form action="${pageContext.request.contextPath }/user/receiveString.do">
	姓名:<input type="text" name="username" id="username">
	<input type="submit" value="提交">
</form>
<hr color="blue" size="12">
<form action="${pageContext.request.contextPath }/user/receiveUser.do">
	姓名<input type="text" name="username" id="username">
	年龄<input type="text" name="age" id="age">
	生日<input type="text" name="birthday" id="birthday">
	地址<input type="text" name="address" id="address">
	<input type="submit" value="user对象提交">
</form>
<hr color="blue" size="13">
<form action="${pageContext.request.contextPath }/user/receiveUserCustom.do">
	姓名<input type="text" name="user.username" id="username">
	年龄<input type="text" name="user.age" id="age">
	生日<input type="text" name="user.birthday" id="birthday">
	地址<input type="text" name="user.address" id="address">
	<input type="submit" value="user包装提交">
</form>


<hr color="blue" size="13">
<form action="${pageContext.request.contextPath }/user/receiveUserCustomList.do">
	姓名<input type="text" name="userList[0].username" id="username">
	年龄<input type="text" name="userList[0].age" id="age">
	姓名<input type="text" name="userList[1].username" id="username">
	年龄<input type="text" name="userList[1].age" id="age">	
	<input type="submit" value="集合包装提交">
</form>
<hr color="blue" size="13">
<form action="${pageContext.request.contextPath }/user/receiveArray.do">
	ID:<input type="checkbox" name="ids" id="ids" value="1">
	ID:<input type="checkbox" name="ids" id="ids" value="2">
	ID:<input type="checkbox" name="ids" id="ids" value="3">
	<input type="submit" value="提交">
</form>


</body>
</html>

跳转页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>success 成功添加</h1>
</body>
</html>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值