`

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

 
阅读更多

在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}获取具体值。

 

分享到:
评论

相关推荐

    Pro Spring MVC---Apress-2012

    web applications using version 3.1 of the Spring Framework. Topics include but are not limited to: • The building blocks of the Spring MVC components • Configuring your development environment • ...

    Pro Spring MVC With Web Flow

    Pro Spring MVC provides in-depth coverage of Spring MVC and Spring Web Flow, two highly customizable and powerful web frameworks brought to you by the developers and community of the Spring Framework....

    Pivotal Certified Spring Web Application Developer Exam(Apress,2015)

    Spring MVC programming model essentials, Spring MVC views and form processing, Spring Web Flow essentials, and Spring Web Flow actions and configuration. The Pivotal Certified Spring Web Application ...

    spring-mvc-validation:用于注释的高级验证技术的Spring MVC Web项目

    在这里,我们提供了一个用于理解Spring MVC Web应用程序的简单项目,并介绍了Spring-mvc-validation批注的高级概念。 在这里,通过带有批注的高级验证技术的详细概念,我们还显示了以下功能: 模型-视图-控制器...

    Spring.MVC.Beginner's.Guide.2nd.Edition.2016.7.pdf

    Spring MVC helps you build flexible and loosely coupled web applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly ...

    精通Spring.MVC

    Spring MVC is a modern web application framework built upon the Spring Framework, and Spring Web Flow is a project that complements Spring MVC for building reusable web controller modules that ...

    spring MVC 最新教程

     Chapter 11: Building Applications with Spring Web Flow .................................373  Chapter 12: Advanced Spring Web Flow.............................................................429 ...

    zframe-ssi:【已停止维护,仅供学习】基于Spring MVC + Spring + Mybatis整合的快速开发框架

    本框架基于SpringMVC+Spring+MyBatis3整合,提供了一些快速开发特性,具体如下描述: 导入方式:Myeclipse-&gt;Import-&gt;Maven4MyEclipse-&gt;Exsit Maven Project ###1.针对前端页面快速开发 说明:前台页面采用JSP作为...

    springmvc无法访问/WEB-INF/views下的jsp的解决方法

    本篇文章主要介绍了springmvc无法访问/WEB-INF/views下的jsp的解决方法,非常具有实用价值,需要的朋友可以参考下

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    7.1. Alternate Spring MVC configuration 7.1.1. Customizing DispatcherServlet configuration 7.1.2. Adding additional servlets and filters 7.1.3. Declaring DispatcherServlet in web.xml 7.2. Processing ...

    Spring.Cookbook.1783985801.epub

    Build full-featured web applications such as Spring MVC applications efficiently that will get you up and running with Spring web development Learn dependency injection and aspect-oriented programming...

    spring-framework-reference-4.1.2

    3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:proxool.properties ...

    tutoriel-web-spring

    api的依赖在/ web-spring-tutorial / src / main / webapp / views中创建hello.jsp 在pom.xml中添加spring-webmvc依赖项修改/WEB-INF/web.xml:添加contextConfigLocation和ContextLoaderListener 创建/WEB-INF/...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;artifactId&gt;spring-webmvc &lt;version&gt;3.1.2.RELEASE &lt;type&gt;jar &lt;scope&gt;compile &lt;groupId&gt;aspectj &lt;artifactId&gt;aspectjweaver &lt;version&gt;1.5.4 &lt;scope&gt;compile &lt;groupId&gt;javax.servlet ...

    HiFiShopee:Devops在线购物

    创建MVC结构-&gt; com.HiFiShopee.controller,web-inf / views / index.jsp [bootstrap 3]创建web.xml创建dispatcher-servlet.xml第2天:-创建了maven快速入门后端项目添加了依赖项在后端pom.xml {spring-core,...

    spring-framework-reference4.1.4

    3.7. General Web Improvements ............................................................................... 19 3.8. WebSocket, SockJS, and STOMP Messaging ..............................................

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    由于Struts能充分满足应用开发的需求,简单易用,敏捷迅速。它使用服务层框架可以将JavaBeans从Jsp/Servlet中分离出来,而使用表现层框架则可以将Jsp中剩余的JavaBeans完全分离,这部分JavaBeans主要负责显示相关...

    Usermanagement2:SpringMVC

    通过小而简单的用户管理示例演示 Spring MVC Web 框架的功能。 在此展示中,您将看到以下内容: The simplest possible @Controller Mapping Requests Obtaining Request Data Generating Responses Message ...

Global site tag (gtag.js) - Google Analytics