资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringMvc接收参数方法总结(必看篇)

接收参数的方式:

罗江ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

1.HttpServletRequest方式接收

public ModelAndView test1(HttpServletRequest req){
    String userName = req.getParameter("userName");
    String password = req.getParameter("password");
    System.out.println(userName);
    System.out.println(password);
    return new ModelAndView("jsp/hello");
  }

2.@RequestParam方式

 public ModelAndView test2(String userName,
      @RequestParam("password") String pwd){
    System.out.println(userName+","+pwd);
    return new ModelAndView("jsp/hello");
  }

3.对象的方式接收

 public ModelAndView test3(User user){
    System.out.println(user);
    return new ModelAndView("jsp/hello");
  }

4.

 /**
  * 使用ModelAndView传出参数 内部 HttpServletRequest的Attribute传递 到jsp页面
   * ModelAndView(String viewName,Map data)data是处理结果
  */
@RequestMapping("action")
public ModelAndView test4(User user){
   Map data = new HashMap();
   data.put("user", user);
   return new ModelAndView("jsp/hello",data);
}

5. Session的方式

/**
   * session存储  可以使用HttpServletRequest的getSession方法访问
   */
  @RequestMapping("action")
  public ModelAndView test7(HttpServletRequest req){
    HttpSession session = req.getSession();
    session.setAttribute("salary", 6000.0);
    return new ModelAndView("jsp/hello");
  }

6.重定向:

@RequestMapping("/updateitem")
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
public ModelAndView updateitem(Items items){
 
itemsService.updateitems(items);
 
//不可以加斜杠 解析不了 itemList.action
return new ModelAndView(new RedirectView("itemList.action"));
}

7.重定向

@RequestMapping("/updateitem")
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
public String updateitem(Items items){
 
itemsService.updateitems(items);
//重定向到action 可以加斜杠 redirect:/itemList.action 解析的了
return "redirect:itemList.action";
}

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

如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。

以上这篇SpringMvc接收参数方法总结(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持创新互联。


文章标题:SpringMvc接收参数方法总结(必看篇)
分享网址:http://cdkjz.cn/article/ghojpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220