springmvc入门(二)——参数绑定和返回值几种方式

前言
  springmvc中,接收页面提交的数据是通过方法形参来接收。而不是在controller类定义成员变更接收.
内容

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,以及返回值的方式可以自定义等等,接下来的学习会继续深入,感谢您的时间,希望您提出宝贵的建议!

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值