Skip to content

Commit d01d8c0

Browse files
committed
ContentNegotiatingResourceBundle Example
1 parent d321f21 commit d01d8c0

30 files changed

+757
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="test" value="true"/>
17+
<attribute name="optional" value="true"/>
18+
<attribute name="maven.pomderived" value="true"/>
19+
</attributes>
20+
</classpathentry>
21+
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
25+
</attributes>
26+
</classpathentry>
27+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
28+
<attributes>
29+
<attribute name="owner.project.facets" value="java"/>
30+
</attributes>
31+
</classpathentry>
32+
<classpathentry kind="output" path="target/classes"/>
33+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>springmvc-ViewResolver-ContentNegotiating</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.validation.validationbuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
31+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
32+
<nature>org.eclipse.jdt.core.javanature</nature>
33+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
34+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
35+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
36+
</natures>
37+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry excluding="**/bower_components/*|**/node_modules/*|**/*.min.js" kind="src" path="src/main/webapp"/>
4+
<classpathentry kind="src" path="target/m2e-wtp/web-resources"/>
5+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
7+
<attributes>
8+
<attribute name="hide" value="true"/>
9+
</attributes>
10+
</classpathentry>
11+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
12+
<classpathentry kind="output" path=""/>
13+
</classpath>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/main/resources=UTF-8
4+
encoding//src/test/java=UTF-8
5+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
8+
org.eclipse.jdt.core.compiler.release=disabled
9+
org.eclipse.jdt.core.compiler.source=1.8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
2+
<wb-module deploy-name="spring5mvc-view-resolver">
3+
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
4+
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
5+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
6+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
7+
<property name="java-output-path" value="/springmvc-ViewResolver-ContentNegotiating/target/classes"/>
8+
<property name="context-root" value="spring5mvc-view-resolver"/>
9+
</wb-module>
10+
</project-modules>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<fixed facet="wst.jsdt.web"/>
4+
<installed facet="jst.web" version="2.3"/>
5+
<installed facet="wst.jsdt.web" version="1.0"/>
6+
<installed facet="java" version="1.8"/>
7+
</faceted-project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Window
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
disabled=06target
2+
eclipse.preferences.version=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/META-INF/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>jbr.springmvc</groupId>
6+
<artifactId>springmvc-ViewResolver-ContentNegotiating</artifactId>
7+
<packaging>war</packaging>
8+
<version>0.1</version>
9+
<name>springmvc-ViewResolver-ContentNegotiating Maven Webapp</name>
10+
<url>http://maven.apache.org</url>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java.version>1.8</java.version>
15+
<mvn.compiler.plugin.version>3.5.1</mvn.compiler.plugin.version>
16+
<mvn.war.plugin.version>2.4</mvn.war.plugin.version>
17+
<junit.version>4.12</junit.version>
18+
19+
<springframework.version>5.1.0.RELEASE</springframework.version>
20+
<hibernate.validator.version>5.1.2.Final</hibernate.validator.version>
21+
<javax.validation.version>1.1.0.Final</javax.validation.version>
22+
23+
<jstl.api.version>1.2.1</jstl.api.version>
24+
<taglib.std.version>1.1.2</taglib.std.version>
25+
<servlet.api.version>3.1.0</servlet.api.version>
26+
<jsp.api.version>2.3.1</jsp.api.version>
27+
28+
<jackson.version>2.9.4</jackson.version>
29+
<apach.poi.version>4.0.1</apach.poi.version>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>${junit.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
<!-- Spring Dependency -->
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-core</artifactId>
44+
<version>${springframework.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-web</artifactId>
49+
<version>${springframework.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework</groupId>
53+
<artifactId>spring-webmvc</artifactId>
54+
<version>${springframework.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework</groupId>
58+
<artifactId>spring-oxm</artifactId>
59+
<version>${springframework.version}</version>
60+
</dependency>
61+
62+
<!-- JSTL Dependency -->
63+
<dependency>
64+
<groupId>javax.servlet.jsp.jstl</groupId>
65+
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
66+
<version>${jstl.api.version}</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>taglibs</groupId>
70+
<artifactId>standard</artifactId>
71+
<version>${taglib.std.version}</version>
72+
</dependency>
73+
74+
<!-- Servlet Dependency -->
75+
<dependency>
76+
<groupId>javax.servlet</groupId>
77+
<artifactId>javax.servlet-api</artifactId>
78+
<version>${servlet.api.version}</version>
79+
<scope>provided</scope>
80+
</dependency>
81+
82+
<!-- JSP Dependency -->
83+
<dependency>
84+
<groupId>javax.servlet.jsp</groupId>
85+
<artifactId>javax.servlet.jsp-api</artifactId>
86+
<version>${jsp.api.version}</version>
87+
<scope>provided</scope>
88+
</dependency>
89+
90+
<!-- Javax Validation Dependency -->
91+
<dependency>
92+
<groupId>javax.validation</groupId>
93+
<artifactId>validation-api</artifactId>
94+
<version>${javax.validation.version}</version>
95+
</dependency>
96+
97+
<!-- For JSOn View -->
98+
<dependency>
99+
<groupId>com.fasterxml.jackson.core</groupId>
100+
<artifactId>jackson-databind</artifactId>
101+
<version>${jackson.version}</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>com.fasterxml.jackson.core</groupId>
105+
<artifactId>jackson-core</artifactId>
106+
<version>${jackson.version}</version>
107+
</dependency>
108+
<dependency>
109+
<groupId>com.fasterxml.jackson.core</groupId>
110+
<artifactId>jackson-annotations</artifactId>
111+
<version>${jackson.version}</version>
112+
</dependency>
113+
114+
<!-- For PDF View -->
115+
<dependency>
116+
<groupId>com.lowagie</groupId>
117+
<artifactId>itext</artifactId>
118+
<version>2.1.7</version>
119+
</dependency>
120+
121+
<!-- For XLS/XLSX View -->
122+
<dependency>
123+
<groupId>org.apache.poi</groupId>
124+
<artifactId>poi</artifactId>
125+
<version>${apach.poi.version}</version>
126+
</dependency>
127+
128+
<dependency>
129+
<groupId>org.apache.poi</groupId>
130+
<artifactId>poi-ooxml</artifactId>
131+
<version>${apach.poi.version}</version>
132+
</dependency>
133+
134+
</dependencies>
135+
<build>
136+
<finalName>springmvc-ViewResolver-ContentNegotiating</finalName>
137+
<plugins>
138+
<plugin>
139+
<groupId>org.apache.maven.plugins</groupId>
140+
<artifactId>maven-compiler-plugin</artifactId>
141+
<version>${mvn.compiler.plugin.version}</version>
142+
<configuration>
143+
<source>${java.version}</source>
144+
<target>${java.version}</target>
145+
</configuration>
146+
</plugin>
147+
<plugin>
148+
<groupId>org.apache.maven.plugins</groupId>
149+
<artifactId>maven-war-plugin</artifactId>
150+
<version>${mvn.war.plugin.version}</version>
151+
<configuration>
152+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
153+
<warName>spring5mvc-view-resolver</warName>
154+
<failOnMissingWebXml>false</failOnMissingWebXml>
155+
</configuration>
156+
</plugin>
157+
</plugins>
158+
</build>
159+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package jbr.springmvc.config;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
11+
import org.springframework.web.accept.ContentNegotiationManager;
12+
import org.springframework.web.servlet.ViewResolver;
13+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
14+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
15+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
16+
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
17+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
18+
import org.springframework.web.servlet.view.JstlView;
19+
20+
import jbr.springmvc.model.Person;
21+
import jbr.springmvc.view.ExcelViewResolver;
22+
import jbr.springmvc.view.Jaxb2MarshallingXmlViewResolver;
23+
import jbr.springmvc.view.JsonViewResolver;
24+
import jbr.springmvc.view.PdfViewResolver;
25+
26+
@Configuration
27+
@EnableWebMvc
28+
@ComponentScan(basePackages = { "jbr.springmvc" })
29+
public class AppConfig extends WebMvcConfigurationSupport {
30+
31+
@Override
32+
public void configureContentNegotiation(ContentNegotiationConfigurer negotiationConfigurer) {
33+
negotiationConfigurer.ignoreAcceptHeader(true).defaultContentType(MediaType.TEXT_HTML);
34+
}
35+
36+
@Bean
37+
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager negotiationManager) {
38+
ContentNegotiatingViewResolver negotiatingViewResolver = new ContentNegotiatingViewResolver();
39+
negotiatingViewResolver.setContentNegotiationManager(negotiationManager);
40+
41+
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
42+
43+
viewResolvers.add(viewResolver()); // default
44+
45+
viewResolvers.add(jaxb2MarshallingXmlViewResolver());
46+
viewResolvers.add(jsonViewResolver());
47+
viewResolvers.add(pdfViewResolver());
48+
viewResolvers.add(excelViewResolver());
49+
50+
negotiatingViewResolver.setViewResolvers(viewResolvers);
51+
52+
return negotiatingViewResolver;
53+
}
54+
55+
@Bean
56+
public ViewResolver jaxb2MarshallingXmlViewResolver() {
57+
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
58+
jaxb2Marshaller.setClassesToBeBound(Person.class);
59+
60+
return new Jaxb2MarshallingXmlViewResolver(jaxb2Marshaller);
61+
}
62+
63+
@Bean
64+
public ViewResolver jsonViewResolver() {
65+
return new JsonViewResolver();
66+
}
67+
68+
@Bean
69+
public ViewResolver pdfViewResolver() {
70+
return new PdfViewResolver();
71+
}
72+
73+
@Bean
74+
public ViewResolver excelViewResolver() {
75+
return new ExcelViewResolver();
76+
}
77+
78+
@Bean
79+
public ViewResolver viewResolver() {
80+
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
81+
viewResolver.setViewClass(JstlView.class);
82+
viewResolver.setPrefix("/WEB-INF/views/");
83+
viewResolver.setSuffix(".jsp");
84+
85+
return viewResolver;
86+
}
87+
88+
}

0 commit comments

Comments
 (0)