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 937a2d0

Browse files
Spring MVC configurar el Servlet sin XML
Configuracion programatica de una aplicación web, eliminamos la necesidad de utilizar el archivos web.xml todo se hará desde código Java.
1 parent 42a5323 commit 937a2d0

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-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+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/webapp/WEB-INF/springmvc-servlet.xml</config-file>
12+
</config-files>
13+
<config-file-groups/>
14+
</spring-data>
15+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
16+
<!--
17+
Properties that influence various parts of the IDE, especially code formatting and the like.
18+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
19+
That way multiple projects can share the same settings (useful for formatting rules for example).
20+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
21+
-->
22+
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
23+
</properties>
24+
</project-shared-configuration>

‎tutorial_webmvc_noxml/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_web_mvc_noxml</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_webmvc_noxml</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_noxml</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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.InternalResourceViewResolver;
7+
8+
@Configuration
9+
@ComponentScan(basePackages = {"carmelo.spring.webmvc"})
10+
public class AppConfig {
11+
12+
@Bean
13+
public InternalResourceViewResolver getInternalResourceViewResolver() {
14+
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
15+
resolver.setPrefix("/WEB-INF/views/");
16+
resolver.setSuffix(".jsp");
17+
return resolver;
18+
}
19+
}
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 MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return null;
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{AppConfig.class};
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carmelo.spring.webmvc;
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 HelloController {
9+
10+
@RequestMapping(value = "/hello")
11+
public ModelAndView saluda() {
12+
ModelAndView mv = new ModelAndView();
13+
mv.addObject("titulo", "Tutoriales Spring MVC");
14+
mv.addObject("mensaje", "Mi Primera Aplicacion Web Spring MVC");
15+
mv.setViewName("hello");
16+
17+
return mv;
18+
}
19+
}
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 Web MVC</title>
6+
</head>
7+
<body>
8+
<h1>${titulo}</h1>
9+
<p>${mensaje}</p>
10+
</body>
11+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h2>Hello World!</h2>
4+
</body>
5+
</html>

0 commit comments

Comments
(0)

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