springMVC 参数绑定 默认支持的参数类型

 

 

 

默认支持的参数类型

需求

打开商品编辑页面,展示商品信息。

需求分析

编辑商品信息,首先要显示商品详情

需要根据商品id查询商品信息,然后展示到页面。

请求的url:/itemEdit.action

参数:id(商品id)

响应结果:商品编辑页面,展示商品详细信息。

ItemService接口

编写ItemService接口如下图:

 

 

ItemServiceImpl实现类

 

@Override
public Item queryItemById(int id) {
	Item item = this.itemMapper.selectByPrimaryKey(id);
	
	return item;
}

 

 

ItemController

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

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

 

需要从请求的参数中把请求的id取出来。

Id包含在Request对象中。可以从Request对象中取id。

 

想获得Request对象只需要在Controller方法的形参中添加一个参数即可。Springmvc框架会自动把Request对象传递给方法。

 

代码实现

 

 

/**
 * 根据id查询商品
 * 
 * @param request
 * @return
 */
@RequestMapping("/itemEdit")
public ModelAndView queryItemById(HttpServletRequest request) {
	// 从request中获取请求参数
	String strId = request.getParameter("id");
	Integer id = Integer.valueOf(strId);

	// 根据id查询商品数据
	Item item = this.itemService.queryItemById(id);

	// 把结果传递给页面
	ModelAndView modelAndView = new ModelAndView();
	// 把商品数据放在模型中
	modelAndView.addObject("item", item);
	// 设置逻辑视图
	modelAndView.setViewName("itemEdit");

	return modelAndView;
}

 

 

 

默认支持的参数类型

处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。

HttpServletRequest

通过request对象获取请求信息

HttpServletResponse

通过response处理响应信息

HttpSession

通过session对象得到session中存放的对象

 

Model/ModelMap

Model

除了ModelAndView以外,还可以使用Model来向页面传递数据,

Model是一个接口,在参数里直接声明model即可。

 

如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。

不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。

代码实现:

 

/**
 * 根据id查询商品,使用Model
 * 
 * @param request
 * @param model
 * @return
 */
@RequestMapping("/itemEdit")
public String queryItemById(HttpServletRequest request, Model model) {
	// 从request中获取请求参数
	String strId = request.getParameter("id");
	Integer id = Integer.valueOf(strId);

	// 根据id查询商品数据
	Item item = this.itemService.queryItemById(id);

	// 把结果传递给页面
	// ModelAndView modelAndView = new ModelAndView();
	// 把商品数据放在模型中
	// modelAndView.addObject("item", item);
	// 设置逻辑视图
	// modelAndView.setViewName("itemEdit");

	// 把商品数据放在模型中
	model.addAttribute("item", item);

	return "itemEdit";
}

 

 

 

 

ModelMap

ModelMap是Model接口的实现类,也可以通过ModelMap向页面传递数据

 

使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

 

代码实现:

 

 

 

/**
 * 根据id查询商品,使用ModelMap
 * 
 * @param request
 * @param model
 * @return
 */
@RequestMapping("/itemEdit")
public String queryItemById(HttpServletRequest request, ModelMap model) {
	// 从request中获取请求参数
	String strId = request.getParameter("id");
	Integer id = Integer.valueOf(strId);

	// 根据id查询商品数据
	Item item = this.itemService.queryItemById(id);

	// 把结果传递给页面
	// ModelAndView modelAndView = new ModelAndView();
	// 把商品数据放在模型中
	// modelAndView.addObject("item", item);
	// 设置逻辑视图
	// modelAndView.setViewName("itemEdit");

	// 把商品数据放在模型中
	model.addAttribute("item", item);

	return "itemEdit";
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值