WEB前后端传参方法总结

接受参数方法

 

1、@Pathvariable

Method= RequestMethod.Get

@RequestMapping(value= " /{id}/{str} " )  

public ModelAndView helloWorld(@PathVariable String id, @PathVariable String str) {  

 System.out.println(id);  

 System.out.println(str);  

 return new ModelAndView( " /helloWorld " );  

 

来自 <http://blog.csdn.net/yaerfeng/article/details/23658391>

 

2、@ModelAttribute

Method = RequestMethod.Post

  • 推荐:主要用于表单上传

Jsp

<formmethod="post" action="hao.do">

 a: <input id="a"type="text" name="a"/>

 b: <input id="b"type="text" name="b"/>

 <input type="submit"value="Submit" />

 </form>

Java pojobean

 public class Pojo{

 private String a;

 private int b;

}

JavaController

@RequestMapping(method=RequestMethod.POST)

 public String processSubmit(@ModelAttribute(" pojo " ) Pojo pojo) {

 return " helloWorld " ;

 }

 

3、HttpServletRequest

Method=RequestMethod.Get

Java

@RequestMapping(method=RequestMethod.GET)

 public String get(HttpServletRequest request,HttpServletResponse response) {

 System.out.println(request.getParameter(" a " ));

 return " helloWorld " ;

}

 

4、@RequestParam

Method=RequestMethod.Get

@RequestMapping(value=" /requestParam " , method= RequestMethod.GET)

 public String setupForm(@RequestParam( "a " ) String a, ModelMap model) {

 System.out.println(a);

 return " helloWorld " ;}


返回页面及结果

 

PS:以下均可用EL表达式获取

1String

页面名称

Request

Attribute

@RequestMapping("/toPerson41.do")  

public String toPerson41(HttpServletRequest request) throws Exception {  

    request.setAttribute("p", newPesion());  

    return "index";  

}  

 

2.1ModelAndView

页面名称

Request

Map.put

@RequestMapping("/toPerson42.do")  

public ModelAndView toPerson42() throws Exception {  

    Map<String, Object> map = new HashMap<String, Object>();  

    map.put("p", newPesion());  

    return new ModelAndView("index", map);  

 

2.2String

页面名称

Request

Map.put

@RequestMapping("/toPerson43.do")  

public String toPerson43(Map<String, Object> map) throws Exception {  

    map.put("p", newPesion());  

    return "index";  

}  

建议使用这个

2.3String

页面名称

Request

Model.addAtrribute

@RequestMapping("/toPerson44.do")  

public String toPerson44(Model model) throws Exception {  

    // 把参数值放到request类里面去  

    model.addAttribute("p", newPesion());  

    return "index";  

}  


PS:

参数的作用范围Scope

 

Request & Response

在当前请求有效

Page

在当前页面有效

Session

在当前浏览器有效

Application

在当前服务器有效



No mapping found for http request with URL 问题

如果遇到这个问题,可以在Spring-mvc.xml中查看以下语句是否正确

<context:component-scanbase-package="cn.bnuz.party.**" />


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值