前台提交请求,后台通过名字获得数据
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>