Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b87f30f

Browse files
Spring MVC ViewResolver
Veremos las distintas implementaciones de la interface ViewResolver.
1 parent e11615f commit b87f30f

File tree

10 files changed

+205
-0
lines changed

10 files changed

+205
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
17+
</properties>
18+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
19+
<config-files>
20+
<config-file>src/main/webapp/WEB-INF/views.xml</config-file>
21+
</config-files>
22+
<config-file-groups/>
23+
</spring-data>
24+
</project-shared-configuration>

‎tutorial_webmvc_ViewResolver/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_webmvc_viewresolver</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_webmvc_viewresolver</name>
9+
10+
<properties>
11+
<spring.version>4.3.7.RELEASE</spring.version>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-webmvc</artifactId>
21+
<version>${spring.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>javax</groupId>
25+
<artifactId>javaee-web-api</artifactId>
26+
<version>7.0</version>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<finalName>tutorial_webmvc_viewresolver</finalName>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-war-plugin</artifactId>
36+
<version>2.3</version>
37+
<configuration>
38+
<failOnMissingWebXml>false</failOnMissingWebXml>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
44+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package carmelo.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.View;
7+
import org.springframework.web.servlet.ViewResolver;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.view.BeanNameViewResolver;
12+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
13+
import org.springframework.web.servlet.view.JstlView;
14+
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
15+
import org.springframework.web.servlet.view.XmlViewResolver;
16+
17+
@EnableWebMvc
18+
@Configuration
19+
@ComponentScan(basePackages = {"carmelo.spring.controller"})
20+
public class WebAppConfig extends WebMvcConfigurerAdapter {
21+
22+
@Override
23+
public void configureViewResolvers(ViewResolverRegistry registry) {
24+
registry.jsp("/WEB-INF/views/", ".jsp");
25+
registry.order(9);
26+
}
27+
28+
@Bean
29+
public ViewResolver viewResolver() {
30+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
31+
resolver.setPrefix("/WEB-INF/views/");
32+
resolver.setSuffix(".jsp");
33+
resolver.setOrder(2);
34+
return resolver;
35+
}
36+
37+
@Bean
38+
public ViewResolver xmlViewResolver() {
39+
XmlViewResolver resolver = new XmlViewResolver();
40+
resolver.setOrder(1);
41+
return resolver;
42+
}
43+
//
44+
// @Bean
45+
// public ViewResolver rbViewResolver() {
46+
// ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();
47+
// resolver.setBasename("myviews");
48+
// return resolver;
49+
// }
50+
//
51+
// @Bean
52+
// public ViewResolver viewResolver() {
53+
// return new BeanNameViewResolver();
54+
// }
55+
//
56+
// @Bean(name = "home")
57+
// public View homex() {
58+
// JstlView view = new JstlView();
59+
// view.setUrl("/WEB-INF/views/home.jsp");
60+
// return view;
61+
// }
62+
//
63+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package carmelo.spring.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return null;
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{WebAppConfig.class};
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package carmelo.spring.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.servlet.ModelAndView;
6+
7+
@Controller
8+
public class HomeController {
9+
10+
@RequestMapping("/")
11+
public ModelAndView homePage() {
12+
return new ModelAndView("home", "msg", "Usando la interface ViewResolver.");
13+
}
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
home.(class)=org.springframework.web.servlet.view.JstlView
2+
home.url=/WEB-INF/views/home.jsp
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Context antiJARLocking="true" path="/ViewResolver"/>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
6+
7+
<bean id="home"
8+
class="org.springframework.web.servlet.view.JstlView">
9+
<property name="url" value="/WEB-INF/views/index.jsp"/>
10+
</bean>
11+
12+
</beans>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<title>Spring MVC ViewResolver</title>
6+
</head>
7+
<body>
8+
<h1>Home.jsp :: ViewResolver</h1>
9+
<p>${msg}</p>
10+
</body>
11+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6+
<title>Spring MVC ViewResolver</title>
7+
</head>
8+
<body>
9+
<h1>Index.jsp :: ViewResolver</h1>
10+
<p>${msg}</p>
11+
</body>
12+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /