ITeye博客

SpringMVC配置JSON、JSP、FreeMark多视图解析器配置

lcpstyle · 2015-04-25 21:58 · springmvc · 编程语言 · Spring · MVC · Markdown

Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还可以是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和POI。Spring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。Spring M...

SpringMVC配置SpringMVC配置JSON、JSP、FreeMarkSpringMVC多视图配置 

 

1.web.xml内容:

?

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

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID"version="3.0">

 

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:beans.xml</param-value>

</context-param>

 

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--处理因使用内省API而导致的内存泄漏问题-->

<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

 

<filter>

<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

<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: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>

 

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

 

2.Servlet.xml内容:

?

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

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

 

<!--组件自动扫描-->

<context:component-scanbase-package="com.tliu.case2.web"/>

 

<!--主要作用于@Controller激活该模式下面是一种简写形式

它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,

是springMVC为@Controllers分发请求所必须的-->

<mvc:annotation-driven/>

 

<!--配置JSON视图-->

<beanid="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

<propertyname="supportedMediaTypes">

<list>

<value>application/json;charset=UTF-8</value>

</list>

</property>

<propertyname="objectMapper">

<beanclass="org.codehaus.jackson.map.ObjectMapper">

<propertyname="dateFormat">

<beanclass="java.text.SimpleDateFormat">

<constructor-argindex="0"type="java.lang.String"value="yyyy-MM-ddHH:mm:ss"/>

</bean>

</property>

</bean>

</property>

</bean>

<beanid="stringHttpMessageConverter"class="org.springframework.http.converter.StringHttpMessageConverter"/>

<beanid="requestMappingHandlerAdapter"class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">

<propertyname="messageConverters">

<list>

<refbean="mappingJacksonHttpMessageConverter"/>

<refbean="stringHttpMessageConverter"/>

</list>

</property>

</bean>

 

<!--配置JSP视图-->

<beanid="internalResourceViewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>

<propertyname="prefix"value="/WEB-INF/jsp/"/>

<propertyname="suffix"value=".jsp"/>

<propertyname="contentType"value="text/html;charset=UTF-8"/>

<propertyname="order"value="1"/>

</bean>

 

<!--配置FreeMark视图-->

<beanid="freeMarkerViewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

<propertyname="contentType"value="text/html;charset=UTF-8"/>

<propertyname="viewClass"value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>

<propertyname="suffix"value=".ftl"/>

<propertyname="cache"value="true"/>

<propertyname="exposeSessionAttributes"value="true"/>

<propertyname="exposeRequestAttributes"value="true"/>

<propertyname="exposeSpringMacroHelpers"value="true"/>

<!--在页面中使用${rc.contextPath}就可获得contextPath-->

<propertyname="requestContextAttribute"value="rc"/>

<propertyname="order"value="0"/>

</bean>

 

<beanid="freemarkConfig"class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<propertyname="location"value="classpath:freemark.properties"/>

</bean>

 

<beanid="fmXmlEscape"class="freemarker.template.utility.XmlEscape"/>

 

<beanid="FreeMarkerConfigurer"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

<propertyname="templateLoaderPath"value="/WEB-INF/ftl/"/>

<propertyname="defaultEncoding"value="UTF-8"/>

<propertyname="freemarkerSettings"ref="freemarkConfig"/>

<propertyname="freemarkerVariables">

<map>

<entrykey="xml_escape"value-ref="fmXmlEscape"/>

</map>

</property>

</bean>

 

<!--文件上传配置注意:这里申明的id必须为multipartResolver-->

<beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<propertyname="maxUploadSize"value="500000"/>

</bean>

 

<!--简单的异常处理-->

<beanid="exceptionResolver"class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

<propertyname="exceptionMappings">

<props>

<!--映射目录为/WEB-INF/jsp/error/upload_error.jsp-->

<propkey="org.springframework.web.multipart.MaxUploadSizeExceededException">/error/upload_error</prop>

</props>

</property>

</bean>

 

<!--对静态资源文件的访问-->

<mvc:resourcesmapping="/images/**"location="/images/"cache-period="31556926"/>

 

<mvc:resourcesmapping="/js/**"location="/js/"cache-period="31556926"/>

 

<mvc:resourcesmapping="/css/**"location="/css/"cache-period="31556926"/>

 

<mvc:resourcesmapping="/upload/**"location="/upload/"cache-period="31556926"/>

</beans>

转自:http://my.oschina.net/u/1859292/blog/309812

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。