springmvc参数封装3
来源:互联网 发布:精液优化处理什么意思 编辑:程序博客网 时间:2024/05/22 00:35
接受包装类型
定义包装类
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
- springmvc参数封装3
- SpringMVC 封装参数
- springmvc参数封装
- springmvc参数封装2
- springmvc参数封装(八)
- springmvc返回json参数封装
- springMVC(10) springMVC参数传递(封装对象接收参数)
- springMVC 在controller中将参数封装对象
- springMVC--封装参数及页面回显
- SpringMVC-----3、SpringMVC参数传递
- springmvc 后台用map封装前台传的参数
- SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解
- Springmvc+ajax如何通过ajax封装多个对象参数,controller接受多个对象参数
- springmvc(3)----参数绑定
- SpringMVC基础-3-参数绑定
- springMVC的参数绑定和返回值类型封装处理原理解析
- springMVC 在controller中将参数封装成对象--日期格式处理问题
- 测试springmvc能否同时将参数封装到两个实体中
- 2.区块链该看哪些会议论文
- JDBC包装类
- win7主机和linux虚拟机网络PING不通——解决
- 构造函数
- jQuery效果之隐藏与显示、淡入淡出、滑动、回调
- springmvc参数封装3
- Android开发WebView加载自签名认证的https网址
- 1.如何入门区块链?
- Zookeeper实例Curator API-使用Curator获取数据内容
- 【物理问题】URAL
- VS2015激活 密钥
- Actor示例简单介绍
- ORACLE ODBC驱动安装
- 梁昱森 高级软件工程 学习总结