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 d720fe9

Browse files
Spring MVC conociendo los controladores
Usando los controladores con las anotaciones @controller, @RequestMapping, @PathVariable, @RequestParam, @responsebody.
1 parent 937a2d0 commit d720fe9

File tree

9 files changed

+219
-0
lines changed

9 files changed

+219
-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_controller/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_controller</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_webmvc_controller</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+
<type>jar</type>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<finalName>tutorial_webmvc_controller</finalName>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-war-plugin</artifactId>
37+
<version>2.3</version>
38+
<configuration>
39+
<failOnMissingWebXml>false</failOnMissingWebXml>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</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.controller"})
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package carmelo.spring.controller;
2+
3+
import java.time.LocalDate;
4+
import java.time.format.DateTimeFormatter;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.Model;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
import org.springframework.web.servlet.ModelAndView;
10+
11+
@Controller
12+
@RequestMapping(value = "/")
13+
public class HelloController {
14+
15+
@RequestMapping(value = "/fecha")
16+
@ResponseBody
17+
public String fecha() {
18+
return "Hoy es: " + LocalDate.now()
19+
.format(DateTimeFormatter.ISO_DATE);
20+
}
21+
22+
@RequestMapping(value = "/hola")
23+
public ModelAndView hola() {
24+
ModelAndView mv = new ModelAndView();
25+
mv.addObject("titulo", "Tutoriales Spring MVC :: HelloController");
26+
mv.addObject("mensaje", "saludo desde el metodo: <br /> "
27+
+ "<code>public ModelAndView hola() {...}</code>");
28+
29+
mv.setViewName("hello");
30+
return mv;
31+
}
32+
33+
@RequestMapping(value = "/hello")
34+
public String hello(Model model) {
35+
model.addAttribute("titulo", "Tutoriales Spring MVC :: HelloController");
36+
model.addAttribute("mensaje", "saludo desde el metodo: <br /> "
37+
+ "<code>public String hello(Model) {...}</code>");
38+
39+
return "hello";
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package carmelo.spring.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.ResponseBody;
9+
10+
@Controller
11+
public class VentasController {
12+
13+
@RequestMapping("/ventas")
14+
public String porVendedor(
15+
@RequestParam("nombre") String nombre,
16+
@RequestParam("mes") Integer mes,
17+
Model model) {
18+
19+
model.addAttribute("vendedor", nombre);
20+
model.addAttribute("ventas", "Listado de ventas para el mes: " + mes);
21+
22+
return "ventas";
23+
}
24+
25+
@ResponseBody
26+
@RequestMapping("/vendedor/{id}")
27+
public String vendedor(@PathVariable("id") Long id) {
28+
return "Buscar vendor con ID = " + id;
29+
}
30+
31+
@ResponseBody
32+
@RequestMapping("/depto/{dep}/vendedor/{id}")
33+
public String departamentoVendedor(
34+
@PathVariable("id") Long id,
35+
@PathVariable("dep") String dep) {
36+
37+
String res = "Este es el departamento de: %s, y el vendedor ID: %d";
38+
39+
return String.format(res, dep, id);
40+
}
41+
}
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: 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>Tutoriales Spring MVC</title>
7+
</head>
8+
<body>
9+
<h1>Estas son la ventas de: ${vendedor}</h1>
10+
<p>${ventas}<p>
11+
</body>
12+
</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 によって変換されたページ (->オリジナル) /