分享

SpringMVC——打怪升级

 太极混元天尊 2018-04-27


上一回合SpringMVC——走出新手村

参数绑定你要知道的

springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过滤器。 springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过request域传输到页面。(感兴趣的可以看看底层方法,就和你当时学的servlet是一样。)

万年的ID

页面点击修改按钮,发起请求

http://127.0.0.1:8080/springmvc/itemEdit.action?id=1

需要从请求的参数中把请求的id取出来。Id包含在Request对象中。可以从Request对象中取id。想获得Request对象只需要在Controller方法的形参中添加一个参数即可。Springmvc框架会自动把Request对象传递给方法。

@RequestMapping('/itemEdit')
public ModelAndView queryItemById(HttpServletRequest request) {
 String strId = request.getParameter('id');
 // 把结果传递给页面
 ModelAndView modelAndView = new ModelAndView();
 // 把数据放在模型中
 modelAndView.addObject('item', service.findById(strId));
 // 设置逻辑视图
 modelAndView.setViewName('itemEdit');
 return modelAndView;


END

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

怎么可能那么短。不存在的!打起精神下面才是重头。

Model

除了ModelAndView以外,还可以使用Model来向页面传递数据,Model是一个接口,在参数里直接声明model即可。如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。

@RequestMapping('/itemEdit')
public String queryItemById(HttpServletRequest request, Model model) {
 // 从request中获取请求参数
 String strId = request.getParameter('id');
 // 把数据放在模型中
 model.addAttribute('item', service.findById(strId));
 return 'itemEdit';
}
ModelMap

ModelMap是Model接口的实现类,也可以通过ModelMap向页面传递数据使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

@RequestMapping('/itemEdit')
public String queryItemById(HttpServletRequest request, ModelMap model) {
 // 从request中获取请求参数
 String strId = request.getParameter('id');
 // 把数据放在模型中
 model.addAttribute('item', service.findById(strId));
 return 'itemEdit';
}
绑定简单类型

当请求的参数名称和处理器形参名称一致时会将请求参数与形参进行绑定。这样,从Request取参数的方法就可以进一步简化。

@RequestMapping('/itemEdit')
public String queryItemById(int id, ModelMap model) {
 // 把数据放在模型中
 model.addAttribute('item', service.findById(id));
 return 'itemEdit';
}

支持的数据类型

参数类型推荐使用包装数据类型,因为基础数据类型不可以为null

整形:Integer、int

字符串:String

单精度:Float、float

双精度:Double、double

布尔型:Boolean、boolean

说明:对于布尔类型的参数,请求的参数值为true或false。或者1或0

请求url:http://localhost:8080/xxx.action?id=2&status=false

处理器方法:public String editItem(Model model,Integer id,Boolean status)

@RequestParam

使用@RequestParam常用于处理简单类型的绑定。

value:参数名字,即入参的请求参数名字,如value=“itemId”表示请求的参数区中的名字为itemId的参数的值将传入。

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报错。TTP Status 400 - Required Integer parameter 'XXXX' is not present

defaultValue:默认值,表示如果请求中没有同名参数时的默认值。

@RequestMapping('/itemEdit')
public String queryItemById(@RequestParam(value = 'itemId', required = true
               defaultValue = '1'
)int id,ModelMap model) 
{
 // 把数据放在模型中
 model.addAttribute('item', service.findById(id));
 return 'itemEdit';
}
绑定pojo类型

实体

public class Items {
   private Integer id;
   private String name;
   private Float price;
   private String pic;
   private Date createtime;
   private String detail;
get...set....

jsp页面

'itemForm'action='/addItem.action' method='post'>
<input type='hidden' name='id'  />
<input type='text' name='name'  />
<input type='text' name='pic'  />

controller层

@RequestMapping('/addItem')
public String addItem(Item item) {
 service.save(item);
 // 返回逻辑视图
 return 'success';
}

注意:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。

绑定包装pojo
public class QueryVo {
     private Item item;
set/get...
}

jsp页面

'itemForm'action='/addItem.action' method='post'>
<input type='hidden' name='item.id'  />
<input type='text' name='item.name'  />
<input type='text' name='item.pic'  />

controller层

@RequestMapping('/queryItem')
 public String queryItem(QueryVo queryVo) {
   System.out.println(queryVo.getItem().getId());
   System.out.println(queryVo.getItem().getName());
   return 'success';
 }

你以为这就完了吗?No.......! 还有其他形式的参数绑定....想知道更多!老规矩请期待....see you!


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多