前言 |
内容 |
controller的参数类型默认分为四种:
- HttpServletRequest(controller形参名和request传入参数名称一致)
controller:
@RequestMapping(value=("/finditems"),method=RequestMethod.GET)
public String findform(HttpServletRequest request) throws Exception {
System.out.println(request.getParameter("id"));
return "items/form";
}
jsp页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>这只是一个测试表单</title>
</head>
<body>
<h>这只是一个测试表单</h>
<a href="${pageContext.request.contextPath }/items/finditems.action?id=1">点击</a>
</body>
</html>
- HttpServletResponse
通过response处理响应的信息,例如
@RequestMapping(value=("/editItemsSubmit"),method=RequestMethod.POST)
public void editItemsSubmit(HttpServletRequest request,HttpServletResponse reponse,Integer items_id,ItemsCustom itemsCustom) throws Exception {
itemsService.updateItems(items_id, itemsCustom);
//重定向页面
reponse.sendRedirect("queryItems4.action");
}
- HttpSession
通过session对象得到session中存放的对象
- Model/ModelMap(主要填充request作用域)
@RequestMapping("/showItem")
public String show(Model model) throws Exception {
String id="1";
model.addAttribute("id",id);
return "items/form";
}
其它类型:
1.简单类型(string,integer)通常采用@requestParam方式:
jsp页面:request传入的参数形参是id
constroller:
@RequestMapping(value=("/editItem"),method=RequestMethod.GET)
public ModelAndView editItem(@RequestParam(value="id")Integer items_id) throws Exception{
Items items=itemsService.findItemById(items_id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsCustom",items);
modelAndView.setViewName("items/editItems");
return modelAndView;
}
这种requestParam方式不需要controller方法形参和request传过来的参数一致。
如果想更加简洁可以采用以下的方式
Integer items_id //错误
Integet id; //正确的方式,必须保证controller方式形参和request穿过的参数一致。
pojo类型:
public class ItemsCustom extends Items {
//添加对商品的信息的扩展信息
}
package cn.itcast.ssm.po;
import java.util.Date;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
}
@RequestMapping(value=("/editItemsSubmit"),method=RequestMethod.POST)
public String editItemsSubmit(ItemsCustom itemsCustom) throws Exception {
itemsService.updateItems(itemsCustom);
return "forward:queryItems4.action";
}
jsp页面:
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
@constroller的返回值类型有三种
- ModelAndView
需要方法结束时,定义ModelAndView,将model和view分别进行设置。
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("itemsCustom",items);
modelAndView.setViewName("items/editItems");
return modelAndView;
- String
1.表示返回逻辑视图名,redirect重定向,forward页面转发
return "items/editItems";
return "redirect:queryItems.action";
return "forward:queryItems4.action";
2.json:
@RequestMapping("/queryPerson")
@ResponseBody
public String load1(@RequestParam String name,@RequestParam String password) throws IOException{
System.out.println(name+" : "+password);
//return name+" : "+password;
Person preson=new Person();
preson.setName("常银玲");
preson.setAge("18岁");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString=objectMapper.writeValueAsString(person);
return jsonString;
}
- void
request.getRequestDispatcher("queryItems4.action").forward(request,reponse);
//或者这种:
reponse.sendRedirect("queryItems4.action");
总结 |
其实还有更多的参数绑定的方式比如list和map,以及返回值的方式可以自定义等等,接下来的学习会继续深入,感谢您的时间,希望您提出宝贵的建议!