SpringMVC多视图技术

简介:

SpringMVC多视图技术很强大,举例来说,你访问同一个控制器的方法,可以根据请求方式不同,返回不同的格式的数据。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version= "1.0"  encoding= "UTF-8" ?>
<web-app version= "2.5"  xmlns= "http://java.sun.com/xml/ns/javaee"
          xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http: //java.sun.com/xml/ns/javaee
     http: //java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <display-name>Lrtech_framework</display-name>
     <servlet>
         <servlet-name>springMVC</servlet-name>
         <servlet- class >org.springframework.web.servlet.DispatcherServlet</servlet- class >
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:/conf/springdemo-servlet.xml</param-value>
         </init-param>
         <load-on-startup> 1 </load-on-startup>
     </servlet>
     <servlet-mapping>
         <servlet-name>springMVC</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>
     <filter>
         <filter-name>encodingFilter</filter-name>
         <filter- class >org.springframework.web.filter.CharacterEncodingFilter</filter- class >
         <init-param>
             <param-name>encoding</param-name>
             <param-value>UTF- 8 </param-value>
         </init-param>
         <init-param>
             <param-name>forceEncoding</param-name>
             <param-value> true </param-value>
         </init-param>
     </filter>
     <filter-mapping>
         <filter-name>encodingFilter</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
</web-app>



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version= "1.0"  encoding= "UTF-8" ?>
<!--suppress XmlUnboundNsPrefix -->
<beans xmlns= "http://www.springframework.org/schema/beans"
        xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context= "http://www.springframework.org/schema/context"
        xmlns:mvc= "http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http: //www.springframework.org/schema/beans
         http: //www.springframework.org/schema/beans/spring-beans.xsd
         http: //www.springframework.org/schema/context
         http: //www.springframework.org/schema/context/spring-context.xsd
         http: //www.springframework.org/schema/mvc
         http: //www.springframework.org/schema/mvc/spring-mvc.xsd">
     <!-- 开启注解驱动 -->
     <mvc:annotation-driven/>
     <context:component-scan base- package = "com.lavasoft.demo.web.controller" />
     <mvc:resources mapping= "/ui/**"  location= "/ui/" />
     <bean  class = "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" >
         <property name= "order"  value= "1"  />
         <property name= "mediaTypes" >
             <map>
                 <entry key= "json"  value= "application/json"  />
             </map>
         </property>
         <property name= "defaultViews" >
             <list>
                 <bean  class = "org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
             </list>
         </property>
         <property name= "ignoreAcceptHeader"  value= "true"  />
     </bean>
     <bean  class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
         <property name= "prefix"  value= "/demo/" />
         <property name= "suffix"  value= ".jsp" />
     </bean>
</beans>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package  com.lavasoft.demo.web.controller.lsh.ch5;
import  org.springframework.stereotype.Controller;
import  org.springframework.ui.ModelMap;
import  org.springframework.web.bind.annotation.RequestMapping;
/**
  * Created by Administrator on 14-4-9.
  *
  * @author leizhimin 14-4-9 上午10:55
  */
@Controller
@RequestMapping( "/demo/lsh/ch5" )
public  class  MultViewController {
     @RequestMapping( "/show" )
     public  String  toShow(ModelMap model) {
         User user =  new  User();
         user.setUserName( "testuname" );
         user.setAge( "23" );
         model.put( "user" , user);
         return  "/lsh/ch5/show" ;
     }
     @RequestMapping( "/demo/lsh/ch5/test" )
     public  String  test(){
         return  "/lsh/ch5/test" ;
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%--
   Created by IntelliJ IDEA.
   User: leizhimin  14 - 4 - 9  上午 10 : 57
--%>
<%@ page contentType= "text/html;charset=UTF-8"  language= "java"  %>
<html>
<head>
     <title></title>
</head>
<body>
<h1>SpringMVC多视图</h1>
     ${user.userName} <br />
     ${user.age}
</body>
</html>


wKiom1NFAQuDnfNsAADlOI2v4eg041.jpg

wKioL1NFAOPi_eo_AAEBJbUGSxk945.jpg



本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/1392889,如需转载请自行联系原作者

相关文章
|
22天前
|
前端开发 Java Spring
Spring MVC 是如何对对象参数进行校验的
【6月更文挑战第4天】对象参数校验是使用 SpringMVC 时常用的功能,这篇文章尝试分析了,Spring 是如何实现这一功能的。
31 5
|
5天前
|
JSON 前端开发 Java
spring mvc Rest风格
spring mvc Rest风格
10 0
|
6天前
|
安全 前端开发 Java
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
12 1
|
9天前
|
设计模式 前端开发 Java
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
【Spring MVC】快速学习使用Spring MVC的注解及三层架构
14 1
|
9天前
|
前端开发 Dubbo Java
spring面试题_spring mvc面试题_springboot面试题库
spring面试题_spring mvc面试题_springboot面试题库
|
11天前
|
JSON 前端开发 Java
【JavaEE进阶】 关于Spring MVC 响应
【JavaEE进阶】 关于Spring MVC 响应
17 3
|
12天前
|
缓存 NoSQL Java
在 SSM 架构(Spring + SpringMVC + MyBatis)中,可以通过 Spring 的注解式缓存来实现 Redis 缓存功能
【6月更文挑战第18天】在SSM(Spring+SpringMVC+MyBatis)中集成Redis缓存,涉及以下步骤:添加Spring Boot的`spring-boot-starter-data-redis`依赖;配置Redis连接池(如JedisPoolConfig)和连接工厂;在Service层使用`@Cacheable`注解标记缓存方法,指定缓存名和键生成策略;最后,在主配置类启用缓存注解。通过这些步骤,可以利用Spring的注解实现Redis缓存。
37 2
|
20天前
|
JSON 前端开发 Java
Spring MVC 级联对象参数校验
【6月更文挑战第6天】在 Spring MVC 的使用过程中,我们会发现很多非常符合直觉的功能特性,但往往我们会习惯这种「被照顾得很好」的开发方式,依靠直觉去判断很多功能特性的用法。
19 1
|
23天前
|
前端开发 Java 关系型数据库
在Spring3 MVC中五步配置集成注解方式Hibernate3
在Spring3 MVC中五步配置集成注解方式Hibernate3
26 3
|
23天前
|
JSON 前端开发 API
Apache HttpClient调用Spring3 MVC Restful Web API演示
Apache HttpClient调用Spring3 MVC Restful Web API演示
18 1