spring MVC 参数传递

前台提交请求,后台通过名字获得数据

jsp
<form action="test.do">
  	<input id="input1Id" name="input1Name" />
  	<input id="input2Id" name="input2Name" />
  	<input id="button1" type="submit"/>
  </form>

后台获得数据

String cInputName1=request.getParameter("input1Name");
注意这时后台不能通过getAttribute()获得数据,因为要先setAttribute设置的数据然后getAttribute获得

后台代码如下,或跳转到MyJsp.jsp界面。

public String outPut(HttpServletResponse response,HttpServletRequest request ,ModelMap model){
		System.out.println("test");
		String cInputName1=request.getParameter("input1Name");
		request.setAttribute("mes", "setAttribute");
	//	String cInputName2=(String) request.getAttribute("mes1");
		System.out.println(cInputName1);
	//	System.out.println(cInputName2);
		return "MyJsp";
	}
MyJsp.jsp获得数据有以下方法

<body>
  	<%=request.getAttribute("mes") %><br>
  	${mes}<br>
  	<%=request.getParameter("input1Name") %><br>
  	${param.input1Name}<br>
  </body>
其中第一句和第二句等效,第三句和第四句等效


或者直接通过控制器变量名进行获取

public ModelAndView outPut(String input1Name,String input2Name){
		System.out.println("test");
		System.out.println(input1Name+" "+input2Name);
		ModelAndView mav=new ModelAndView("MyJsp");
		return mav;
	}
或者通过给变量取别名

public ModelAndView outPut(@RequestParam("input1Name")String inputName,String input2Name){
		System.out.println("test");
		System.out.println(inputName+" "+input2Name);
<pre name="code" class="java"><span style="white-space:pre">		</span>ModelAndView mav=new ModelAndView("MyJsp");
return mav;}

页面获得数据的方式

用ModelAndView对象传递

controller
ModelAndView mav=new ModelAndView("MyJsp");
mav.addObject("mes","asd");
jsp
<pre name="code" class="html"><%=request.getAttribute("mes") %><br>
${mes}<br>

 或者 

java
@RequestMapping("test.do")
	public ModelAndView outPut(Model model, String input1Name){
		model.addAttribute("mes","asd");
		System.out.println("test");
		model.addAttribute("mes","asd");
		ModelAndView mav=new ModelAndView("MyJsp");
		return mav;
	}
或者
<pre name="code" class="java">@RequestMapping("test.html")
public ModelAndView outPut(HttpServletResponse response,HttpServletRequest request ,ModelMap model){
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("mes","asd");
	ModelAndView mav=new ModelAndView("MyJsp");
	mav.addObject("mes","asd");
	return mav;
}

 
jsp
<pre name="code" class="java"><pre name="code" class="html"><%=request.getAttribute("mes") %><br>
${mes}<br>

 

 




 






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值