基于Spring MVC的Web应用开发(11) - Views

在FileUpload一文中,我们初步了解了SpringMVC中View的用法,在例子中,通过给Model添加一个属性(model.addAttribute()),View对应的JSP就可以获取该值。本文再介绍一些View对应JSP取值的方式。

增加一个Controller,ViewsController:

 

Java代码   收藏代码
  1. package org.springframework.samples.mvc.views;  
  2.   
  3. import javax.validation.Valid;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.ui.Model;  
  7. import org.springframework.web.bind.annotation.PathVariable;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.RequestMethod;  
  10.   
  11. @Controller  
  12. @RequestMapping("/views/*")  
  13. public class ViewsController {  
  14.   
  15.     @RequestMapping(value="html", method=RequestMethod.GET)  
  16.     public String prepare(Model model) {  
  17.         model.addAttribute("foo""bar");  
  18.         model.addAttribute("fruit""apple");  
  19.         return "views/html";  
  20.     }  
  21.       
  22.     @RequestMapping(value="/viewName", method=RequestMethod.GET)  
  23.     public void usingRequestToViewNameTranslator(Model model) {  
  24.         model.addAttribute("foo""bar");  
  25.         model.addAttribute("fruit""apple");  
  26.     }  
  27.   
  28.     @RequestMapping(value="pathVariables/{foo}/{fruit}", method=RequestMethod.GET)  
  29.     public String pathVars(@PathVariable String foo, @PathVariable String fruit) {  
  30.         // No need to add @PathVariables "foo" and "fruit" to the model  
  31.         // They will be merged in the model before rendering  
  32.         return "views/html";  
  33.     }  
  34.   
  35.     @RequestMapping(value="dataBinding/{foo}/{fruit}", method=RequestMethod.GET)  
  36.     public String dataBinding(@Valid JavaBean javaBean, Model model) {  
  37.         // JavaBean "foo" and "fruit" properties populated from URI variables   
  38.         return "views/dataBinding";  
  39.     }  
  40.   
  41. }  

 

 

1. 访问"http://localhost:8080/web/views/html",返回到"webapp/WEB-INF/views/views/html.jsp":

 

Html代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>My HTML View</title>  
  6.     <link href="<c:url value="/resources/form.css" />rel="stylesheet"  type="text/css" />       
  7. </head>  
  8. <body>  
  9. <div class="success">  
  10.     <h3>foo: "${foo}"</h3>  
  11.     <h3>fruit: "${fruit}"</h3>  
  12. </div>  
  13. </body>  
  14. </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"

 

Html代码   收藏代码
  1. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  2. <%@ page session="false" %>  
  3. <html>  
  4. <head>  
  5.     <title>Data Binding with URI Template Variables</title>  
  6.     <link href="<c:url value="/resources/form.css" />rel="stylesheet"  type="text/css" />       
  7. </head>  
  8. <body>  
  9. <div class="success">  
  10.     <h3>javaBean.foo: ${javaBean.foo}</h3>  
  11.     <h3>javaBean.fruit: ${javaBean.fruit}</h3>  
  12. </div>  
  13. </body>  
  14. </html>  

 

 dataBinding(@Valid JavaBean javaBean, Model model)的方法参数中有个自定义的Java类,SpringMVC会自动解析URL路径,并且感知foo和fruit是否为JavaBean的属性,如果是,则将它赋值给JavaBean,非常智能。JSP现在得到的就是一个JavaBean,因此需要使用${javaBean.foo}获取具体值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值