springMVC--封装参数及页面回显

封装参数

注意:springmvc没有成员变量,把需要传递的参数对象放入方法中,当你请求这个方法的时候,

这个方法里面的对象会自动被创建,需要封装的参数自动被封装到方法的对象中

分析接受参数类型:

基本类型,int,String等等基本类型。

Pojo类型

包装类型

Springmvc默认支持类型:

HttpSession,HttpRequstServlet,Model等等。

Struts2参数:基于属性封装。

Springmvc参数封装:基于方法进行封装。

一、基本类型

1、页面:
<form action="${pageContext.request.contextPath }/user/recieveInt.do" method="post">
	ID:<input type="text" name="id" id="id">
	<input type="submit" value="提交">
</form>
 
2、方法:
@Controller//<bean class="UserController"/>
@RequestMapping("/user")
public class UserController {
	
	
	//跳转到add页面
	@RequestMapping("toAdd")
	public String toAdd(){
		
		return "add";
	}
	
	//接受int类型参数
	@RequestMapping("recieveInt")
	public String recieveInt(Integer id){
		
		System.out.println(id);
		
		return "success";
		
	}
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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
		
		<!-- 扫描,把Controller交给spring管理 -->
		<context:component-scan base-package="cn.hcx"></context:component-scan>
		
		<!-- 配置注解处理器映射器 
			功能:寻找执行类Controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
		
		<!-- 配置注解处理器适配器 
			功能:调用controller方法,执行controller
		-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
		
		
		<!-- 配置sprigmvc视图解析器:解析逻辑试图 
			 后台返回逻辑试图:index
			视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/jsps/index.jsp
		-->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsps/"></property>
		<property name="suffix" value=".jsp"></property>		
		</bean>
		</beans>

二、字符串类型

1、页面
<form action="${pageContext.request.contextPath }/user/recieveStr.do" method="post">
	姓名:<input type="text" name="username" id="username">
	<input type="submit" value="提交">
</form>
2、方法
//接受字符类型参数
	@RequestMapping("recieveStr")
	public String recieveStr(String username)
	{
		
		System.out.println(username);
		return "success";
	}

success.jsp:
<body>
<h1>接受参数成功</h1>
</body>

三、数组类型

分析:批量删除:checkbox复选框。Value必须有值。

1、页面
<form action="${pageContext.request.contextPath }/user/recieveArray.do" method="post">
	ID:<input type="checkbox" name="ids" value="1" id="ids">
	ID:<input type="checkbox" name="ids" value="2" id="ids">
	ID:<input type="checkbox" name="ids" value="3" id="ids">
	<input type="submit" value="提交">
</form>
2、方法
//接受数组类型参数
	@RequestMapping("recieveArray")
	public String recieveArray(Integer[] ids){
		
		System.out.println(ids);
		
		return "success";
	}

四、pojo类型

User:
public class User {
	
	private Integer id;
	private String username;
	private Date birthday;
	private String sex;
	private String address;
1、页面
<form action="${pageContext.request.contextPath }/user/recieveUser.do" method="post">
	姓名:<input type="text" name="username" id="username">
	生日:<input type="text" name="birthday" id="birthday">
	性别:<input type="text" name="sex" id="sex">
	地址:<input type="text" name="address" id="address">
	<input type="submit" value="提交">
</form>
2、方法
//接受参数封装User对象
	@RequestMapping("recieveUser")
	public String recieveUser(User user){
		
		System.out.println(user);
		
		return "success";
	}

五、包装类型

UserCustomer:
public class UserCustom {
	
	private User user;
1、页面
<form action="${pageContext.request.contextPath }/user/recieveUserCustom.do" method="post">
	姓名:<input type="text" name="user.username" id="username">
	生日:<input type="text" name="user.birthday" id="birthday">
	性别:<input type="text" name="user.sex" id="sex">
	地址:<input type="text" name="user.address" id="address">
	<input type="submit" value="提交">

</form>
 
2、方法
//接受包装类型参数
	@RequestMapping("recieveUserCustom")
	public String recieveUserCustom(UserCustom userCustom){
		
		System.out.println(userCustom);
		
		return "success";
	}

六、集合类型

方法体里不能直接传递list集合和map集合类型的参数

把list集合和map集合封装到包装类中

UserCustomer:

 

public class UserCustom {
	
	private User user;
	
	private List<User> userList;
	
	private Map<String,Object> maps = new HashMap<String, Object>();
 

 

接受list集合:
1、页面
<form action="${pageContext.request.contextPath }/user/recieveList.do" method="post">
	姓名:<input type="text" name="userList[0].username" id="username">
	地址:<input type="text" name="userList[0].address" id="address">
	
	姓名:<input type="text" name="userList[1].username" id="username">
	地址:<input type="text" name="userList[1].address" id="address">
	<input type="submit" value="提交">

</form>
 
2、方法
//接受集合类型参数
	@RequestMapping("recieveList")
	public String recieveList(UserCustom userCustom){
		
		System.out.println(userCustom);
		
		return "success";
	}
		

接受map集合:
1、页面
<form action="${pageContext.request.contextPath }/user/recieveMap.do" method="post">
	姓名:<input type="text" name="maps['username']" id="username">
	地址:<input type="text" name="maps['address']" id="address">

<input type="submit" value="提交">
2、方法
//接受集合类型参数
	@RequestMapping("recieveMap")
	public String recieveMap(UserCustom userCustom){
		
		System.out.println(userCustom);
		
		return "success";
	}
 

有了struts2,为什么还需要sprigmvc?

实现机制:

Struts2是基于过滤器实现的。

Springmvc基于servlet实现。Servlet比过滤器快。

 

运行速度:

Struts2是多例

请求来了以后,struts2创建多少个对象:

ActionContext,valuestack,UserAction,ActionSuport,ModelDriven

userAction里面属性:User对象,userlist集合等

 

Springmvc是单例。

 

参数封装来分析:

Struts基于属性进行封装。

Springmvc基于方法封装。颗粒更细

 

小明访问了n次这个类

N个UserAction,n个actionSupport,n个modelDriver,n个user,n个list,n个actionContext

N个valueStack等

Public Class userAction extendsActionSupport implements ModelDriven{

Private user = new user();

Private List<user> list;

 

}

一个,如果访问方法,只需要创建方法里面的几个对象,对象是局部变量,使用完毕后就销毁。

Public class userController(){}

 

页面回显

Springmvc使用model对象,model对象相当于application

Application对象中的数据可以使用el表达式进行获取

方法里面的对象会自动被创建,所以把model放到方法中

查询所有:
 
@RequestMapping("list")
	public String list(Model model){
		//model	相当于application域对象
		
		List<User> userList = new ArrayList<User>();
		
		User user1 = new User();
		user1.setId(1);
		user1.setSex("男");
		user1.setUsername("张山峰");
		user1.setAddress("武当山");
		user1.setBirthday(new Date());
		
		User user2 = new User();
		user2.setId(2);
		user2.setSex("男2");
		user2.setUsername("张山峰222");
		user2.setAddress("武当山222");
		user2.setBirthday(new Date());
		
		User user3 = new User();
		user3.setId(3);
		user3.setSex("男3");
		user3.setUsername("张山峰333");
		user3.setAddress("武当山333");
		user3.setBirthday(new Date());
		
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		
		model.addAttribute("userList", userList);
		
		return "list";
		
	}

页面获取:
 
<body>
<table border="1" style="color: blue">
<tr>
<td>姓名</td>
<td>生日</td>
<td>性别</td>
<td>地址</td>
<td>操作</td>
</tr>

<c:forEach items="${userList }" var="user">
	<tr>
	<td>${user.username }</td>
	<td>${user.birthday }</td>
	<td>${user.sex }</td>
	<td>${user.address }</td>
	<td>
	<a href="${pageContext.request.contextPath }/user/updateByID.do=${user.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>

修改:
 
	//修改
	@RequestMapping("updateByID")
	public String updateByID(Integer id,Model model){
		User user1 = new User();
		user1.setId(id);
		user1.setSex("男");
		user1.setUsername("张山峰");
		user1.setAddress("武当山");
		user1.setBirthday(new Date());
		
		model.addAttribute("user", user1);
		
		return "edit";
	}

回显:edit.jsp:
<form action="${pageContext.request.contextPath }/user/recieveUser.do" method="post">
姓名:<input type="text" name="username" value="${user.username }" id="username">
生日:<input type="text" name="birthday" value="${user.birthday }" id="birthday">
性别:<input type="text" name="sex" value="${user.sex }" id="sex">
地址:<input type="text" name="address" value="${user.address }" id="address">
<input type="submit" value="提交">
</form>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值