在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。本文再介绍一些View对应JSP取值的方式。
增加一个Controller,ViewsController:
package org.springframework.samples.mvc.views; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/views/*") public class ViewsController { @RequestMapping(value="html", method=RequestMethod.GET) public String prepare(Model model) { model.addAttribute("foo", "bar"); model.addAttribute("fruit", "apple"); return "views/html"; } @RequestMapping(value="/viewName", method=RequestMethod.GET) public void usingRequestToViewNameTranslator(Model model) { model.addAttribute("foo", "bar"); model.addAttribute("fruit", "apple"); } @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET) public String pathVars(@PathVariable String foo, @PathVariable String fruit) { // No need to add @PathVariables "foo" and "fruit" to the model // They will be merged in the model before rendering return "views/html"; } @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET) public String dataBinding(@Valid JavaBean javaBean, Model model) { // JavaBean "foo" and "fruit" properties populated from URI variables return "views/dataBinding"; } }
1. 访问"http://localhost:8080/web/views/html",返回到"webapp/WEB-INF/views/views/html.jsp":
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>My HTML View</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> </head> <body> <div class="success"> <h3>foo: "${foo}"</h3> <h3>fruit: "${fruit}"</h3> </div> </body> </html>
prepare(Model model)同FileUpload一样,通过model.addAttribite("foo", "bar");,JSP的${foo}就能获得"bar"。
2. 访问"http://localhost:8080/web/views/viewName",返回到"webapp/WEB-INF/views/views/viewName.jsp",这个jsp文件和html.jsp一样。
usingRequestToViewNameTranslator(Model model)和prepare(Model model)稍有不同,该方法的返回值为void,SpringMVC认为返回值为void的View名字就是@RequestMapping中映射的完整路径,即"views/viewName"。
3. 访问"http://localhost:8080/web/views/pathVariables/bar/orange",返回到"webapp/WEB-INF/views/views/html.jsp"
pathVars方法多了@PathVariable注解,该注解解析URL路径,并赋值给带有@PathVaribale的变量,View对应的JSP可以直接读取到这个变量的值。
4. 访问"http://localhost:8080/web/views/dataBinding/bar/orange",返回到"webapp/WEB-INF/views/views/dataBinding.jsp"
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>Data Binding with URI Template Variables</title> <link href="<c:url value="/resources/form.css" />" rel="stylesheet" type="text/css" /> </head> <body> <div class="success"> <h3>javaBean.foo: ${javaBean.foo}</h3> <h3>javaBean.fruit: ${javaBean.fruit}</h3> </div> </body> </html>
dataBinding(@Valid JavaBean javaBean, Model model)的方法参数中有个自定义的Java类,SpringMVC会自动解析URL路径,并且感知foo和fruit是否为JavaBean的属性,如果是,则将它赋值给JavaBean,非常智能。JSP现在得到的就是一个JavaBean,因此需要使用${javaBean.foo}获取具体值。