spring MVC 参数传递

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

jsp

后台获得数据

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获得数据有以下方法


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


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

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);
		ModelAndView mav=new ModelAndView("MyJsp");
return mav;}

页面获得数据的方式

用ModelAndView对象传递

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

 或者 
  

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;
	}
或者
@RequestMapping("test.html")
public ModelAndView outPut(HttpServletResponse response,HttpServletRequest request ,ModelMap model){
	Map map = new HashMap();
	map.put("mes","asd");
	ModelAndView mav=new ModelAndView("MyJsp");
	mav.addObject("mes","asd");
	return mav;
}

 
  
jsp
<%=request.getAttribute("mes") %>
${mes}

 
  

 
  




 
  





你可能感兴趣的:(工程开发)