四:视图名的确定。
spring mvc可以通过可以通过多种方式确定视图名,在前面的例子中,方法无返回值,视图名更具请求参数确定。Controller方法还是返回一个String类型的值作为视图名。
- @RequestMapping("/user/list.htm")
- public String listAllUser(){
- return "user/list-user";
- }
@RequestMapping("/user/list.htm")public String listAllUser(){ return "user/list-user";}
那么该例子的视图文件就为/WEB-INF/jsp/user/list-user.jsp
五:视图模型参数的传递。
在spring mvc中可以通过ModelMap对象传递模型参数到视图进行处理。在Controller方法中声明一个ModelMap参数,spring会创建一个ModelMap对象,并传入方法,方法处理完成后自动传递到视图进行处理。
- @RequestMapping("/user/list.htm")
- public String listAllUser(ModelMap model){
- List<User> users = new ArrayList<User>();
- User user = new User();
- user.setPassword("user1");
- user.setPassword("pwd");
- user.setAge(20);
- users.add(user);
- user = new User();
- user.setPassword("user2");
- user.setPassword("pwd2");
- user.setAge(22);
- users.add(user);
- model.addAttribute("users", users);
- return "user/list-user";
- }
@RequestMapping("/user/list.htm") public String listAllUser(ModelMap model){ List<User> users = new ArrayList<User>(); User user = new User(); user.setPassword("user1"); user.setPassword("pwd"); user.setAge(20); users.add(user); user = new User(); user.setPassword("user2"); user.setPassword("pwd2"); user.setAge(22); users.add(user); model.addAttribute("users", users); return "user/list-user"; }
model中的参数默认是添加在request作用域中的,该例中视图可以通过request.getAttribute("users")获得user列表。在视图中使用jstl输出
- <c:forEach var="user" items="${requestScope.users}">
- ${user.username }
- ${user.password }
- ${user.age }
- </c:forEach>
<c:forEach var="user" items="${requestScope.users}"> ${user.username } ${user.password } ${user.age }</c:forEach>
还可以通过在Controller方法返回一个ModelMap对象,这种情况下,视图名由请求Url确定。
- @RequestMapping("/user/list-user.htm")
- public ModelMap listAllUser() {
- List<User> users = new ArrayList<User>();
- //.........
- ModelMap model = new ModelMap();
- model.addAttribute("users", users);
- return model;
- }
@RequestMapping("/user/list-user.htm") public ModelMap listAllUser() { List<User> users = new ArrayList<User>(); //......... ModelMap model = new ModelMap(); model.addAttribute("users", users); return model; }
另外可以通过在Controller上添加@SessionAttributes注解指定某个model参数为session作用域的
六:特殊输入参数
spring mvc还可以往Controller方法传入request,session等特殊的参数。
- @Controller
- public class RequestController {
- @RequestMapping("/handleRequest.htm")
- public void handleRequest(HttpServletRequest request,
- HttpServletResponse response, HttpSession session)
- throws IOException {
- String param = (String) request.getParameter("param");
- if (param != null) {
- session.setAttribute("param", param);
- }
- PrintWriter out = response.getWriter();
- out.println("<h1>" + session.getAttribute("param") + "</h1>");
- out.flush();
- out.close();
- }
- }
@Controllerpublic class RequestController { @RequestMapping("/handleRequest.htm") public void handleRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { String param = (String) request.getParameter("param"); if (param != null) { session.setAttribute("param", param); } PrintWriter out = response.getWriter(); out.println("<h1>" + session.getAttribute("param") + "</h1>"); out.flush(); out.close(); }}
如果在Controller方法中直接使用了response输入,方法应该返回void,不再使用视图。
七:处理文件上传
在前面的例子中配置了multipartResolver bean。该bean对http请求中的mutipart form da
- @Controller
- public class FileUploadController {
- @RequestMapping("/fileupload.htm")
- public void upload(MultipartHttpServletRequest request,
- HttpServletResponse response) throws IOException {
- MultipartFile file = request.getFile("file");
- byte[] buffer = new byte[1024];
- int len = -1;
- InputStream in = file.getInputStream();
- String contextPath = request.getRealPath("/");
- String path = contextPath + "\\" + file.getOriginalFilename();
- OutputStream out = new FileOutputStream(path);
- while ((len = in.read(buffer)) != -1) {
- out.write(buffer, 0, len);
- }
- in.close();
- out.close();
- response.getWriter().println("Upload Success!");
- }
- }
@Controllerpublic class FileUploadController { @RequestMapping("/fileupload.htm") public void upload(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException { MultipartFile file = request.getFile("file"); byte[] buffer = new byte[1024]; int len = -1; InputStream in = file.getInputStream(); String contextPath = request.getRealPath("/"); String path = contextPath + "\\" + file.getOriginalFilename(); OutputStream out = new FileOutputStream(path); while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } in.close(); out.close(); response.getWriter().println("Upload Success!"); }}
MultipartHttpServletRequest可以获得多个上传文件,同时可以获得传入的其他Http参数。
八:异常处理
- <bean id="exceptionResolver"
- class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
- <property name="defaultErrorView">
- <value>/exception/error</value>
- </property>
- <property name="exceptionMappings">
- <props>
- <prop key="java.sql.SQLException">
- /exception/dberror
- </prop>
- </props>
- </property>
- </bean>