return"index";
}
}
先调用reg4方法,再调用reg5方法。我们发现控制台打印出来:尚学堂高淇
Controller类中方法参数的处理
Controller类中方法返回值的处理
1. 返回string(建议)
a) 根据返回值找对应的显示页面。路径规则为:prefix前缀+返回值+suffix后缀组成
b) 代码如下:
| @RequestMapping(params="method=reg4") public String reg4(ModelMap map) { System.out.println("HelloController.handleRequest()"); return"index"; } |
| 前缀为:/WEB-INF/jsp/ 后缀是:.jsp 在转发到:/WEB-INF/jsp/index.jsp |
2. 也可以返回ModelMap、ModelAndView、map、List、Set、Object、无返回值。一般建议返回字符串!
请求转发和重定向
代码示例:
| package com.sxt.web;
import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes;
@Controller @RequestMapping("/user.do") publicclass UserController {
@RequestMapping(params="method=reg4") public String reg4(ModelMap map) { System.out.println("HelloController.handleRequest()"); // return "forward:index.jsp"; // return "forward:user.do method=reg5"; //转发 // return "redirect:user.do method=reg5"; //重定向 return"redirect:http://www.baidu.com"; //重定向 }
@RequestMapping(params="method=reg5") public String reg5(String uname,ModelMap map) { System.out.println("HelloController.handleRequest()"); System.out.println(uname); return"index"; }
} |
访问reg4方法,既可以看到效果。
获得request对象、session对象
普通的Controller类,示例代码如下:
| @Controller @RequestMapping("/user.do") publicclass UserController {
@RequestMapping(params="method=reg2") public String reg2(String uname,HttpServletRequest req,ModelMap map){ req.setAttribute("a","aa"); req.getSession().setAttribute("b","bb"); return"index"; } } |
ModelMap
是map的实现,可以在其中存放属性,作用域同request。下面这个示例,我们可以在modelMap中放入数据,然后在forward的页面上显示这些数据。通过el表达式、JSTL、java代码均可。代码如下:
| package com.sxt.web;
import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller @RequestMapping("/user.do") publicclass UserControllerextends MultiActionController {
@RequestMapping(params="method=reg") public String reg(String uname,ModelMap map){ map.put("a","aaa"); return"index"; } } |
| <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
${requestScope.a}
|
ModelAndView模型视图类
见名知意,从名字上我们可以知道ModelAndView中的Model代表模型,View代表视图。即,这个类把要显示的数据存储到了Model属性中,要跳转的视图信息存储到了view属性。我们看一下ModelAndView的部分源码,即可知其中关系:
| publicclassModelAndView {
/** View instance or view name String */ private Objectview;
/** Model Map */ private ModelMapmodel;
/** * Indicates whether or not this instance has been cleared with a call to{@link #clear()}. */ privatebooleancleared =false;
/** * Default constructor for bean-style usage: populating bean * properties instead of passing in constructor arguments. * @see #setView(View) * @see #setViewName(String) */ public ModelAndView() { }
/** * Convenient constructor when there is no model data to expose. * Can also be used in conjunction with * @param viewName name of the View to render, to |