<form action="${pageContext.request.contextPath}/user/recievecustomer.do" method="post">
姓名:<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="提交">
</form>
@RequestMapping("recievecustomer")
public String recievecustomer(UserCustomer user){
System.out.println(user);
return "success";
}
接受集合类型参数
接收list集合
@RequestMapping("recieveList")
public String recieveList(UserCustomer userCustomer){
System.out.println(userCustomer);
return "success";
}
<hr color="blue" size="12">
<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].age" id="age">
姓名:<input type="text" name="userList[1].username" id="username">
年龄:<input type="text" name="userList[1].age" id="username">
<input type="submit" value="提交">
</form>
页面接受Map
@RequestMapping("recieveMaps")
public String recieveMaps(UserCustomer userCustomer){
System.out.println(userCustomer);
return "success";
}
<hr color="blue" size="12">
<form action="${pageContext.request.contextPath}/user/recieveMaps.do" method="post">
姓名:<input type="text" name="maps['username']" id="username">
年龄:<input type="text" name="maps['age']" id="age">
<input type="submit" value="提交">
</form>
struts和springmvc
struts2是基于过滤器实现的
springmvc基于servlet实现,servlet比过滤器快
请求来了struts2创建多个对象
ActionContext,valuestack,UserAction,ActionSuport,ModelDriven
userAction里面属性:User对象,userlist集合等
springmvc是单例
参数封装:struts基于属性进行封装
springmvc基于方法封装
页面回显
@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";
}
<table border="1">
<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.age}</td>
<td>${user.birthday}</td>
<td>${user.address}</td>
<td>
<a href="${pageContext.request.contextPath}/user/update.do?id=${user.id}">修改</a>
<%-- <a href="${pageContext.request.contextPath}/user/update/${user.id}">修改</a> --%>
<%-- <a href="${pageContext.request.contextPath}/rest/user/update/${user.id}">修改</a> --%>
</td>
</tr>
</c:forEach>
修改
<a href="${pageContext.request.contextPath}/user/updateByID.do?id=${user.id}">修改</a>
@RequestMapping("updateByID")
public String update(Integer id,Model model){
User user1=new User();
user1.setAddress("武当山");
user1.setAge("1233");
user2.setBirthday(new Date());
user2.setUsername("张三丰");
user2.setId(id);
return "edit";
}
<form action="${pageContext.request.contextPath}/user/recievecustomer.do" method="post">
姓名:<input type="text" name="username" value="${user.username}" id="username">
年龄:<input type="text" name="age" value="${user.birthday}" id="age">
生日:<input type="text" name="birthday" value="${user.sex}" id="birthday">
地址:<input type="text" name="address" value="${user.address}"id="address">
<input type="submit" value="提交">
</form>
URL模板映射过程
Restfull设计风格
Web.xml拦截方式:在rest目录下所有请求都被拦截,servlet可以拦截目录。
{}:匹配接受页面Url路径参数
@Pathariable:{}里面参数注入后面参数里面
转发重定向
关键字:forward
本类进行转发:
本类方法和方法之间进行forward
转发方式:
方式一:return ”forward:list.do“;
@RequestMapping("forward")
public String forward(){
return "forward:list.do";
}
测试方式:在list方法打断点,如果断点能成功,证明转发成功。
方式二:return ”forward:/user/list.do“;
@RequestMapping("forward")
public String forward(){
return "forward:/items/list.do";
}
注意:user根路径前面必须有/.
跨类进行转发:
转发方式:return ”forward:/items/list.do“;
重定向
关键字:redirect
本类进行重定向:
本类方法与方法之间进行redirect
重定向方式:
方式一:return ”redirect:list.do“;
@RequestMapping("redirect")
public String redirect(){
return "redirect:list.do";
}
方式二:return ”redirect:/user/list.do“;
@RequestMapping("redirect")
public String forward(){
return "redirect:/user/list.do";
}
跨类进行重定向:
转发方式:return ”redirect:/items/list.do“;
@RequestMapping("redirect")
public String redirect(){
return "redirect:/items/list.do";
}