Skip to content

Commit 6038c51

Browse files
authored
Merge pull request eugenp#865 from dineshmadhwal/master
BAEL-417 Spring HandlerAdapters
2 parents cddeb95 + c98bba7 commit 6038c51

9 files changed

Lines changed: 221 additions & 0 deletions

‎spring-mvc-handlers/pom.xml‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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>com.baeldung</groupId>
5+
<artifactId>SpringMVCHandlers</artifactId>
6+
<packaging>war</packaging>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>SpringMVCHandlers Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<springframework.version>4.3.4.RELEASE</springframework.version>
13+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
14+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
<version>3.1.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>commons-logging</groupId>
25+
<artifactId>commons-logging</artifactId>
26+
<version>1.2</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-webmvc</artifactId>
31+
<version>${springframework.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework</groupId>
35+
<artifactId>spring-context</artifactId>
36+
<version>${springframework.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-core</artifactId>
41+
<version>${springframework.version}</version>
42+
</dependency>
43+
</dependencies>
44+
<build>
45+
<pluginManagement>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>${maven-compiler-plugin.version}</version>
51+
<configuration>
52+
<source>1.7</source>
53+
<target>1.7</target>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-war-plugin</artifactId>
59+
<version>${maven-war-plugin.version}</version>
60+
<configuration>
61+
<warSourceDirectory>src/main/webapp</warSourceDirectory>
62+
<warName>springMVCHandlers</warName>
63+
<failOnMissingWebXml>false</failOnMissingWebXml>
64+
</configuration>
65+
</plugin>
66+
</plugins>
67+
</pluginManagement>
68+
<finalName>springMVCHandlers</finalName>
69+
</build>
70+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.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 AnnotationMethodHandlerAdapterExample {
9+
@RequestMapping("/annotedName")
10+
public ModelAndView getEmployeeName() {
11+
ModelAndView model = new ModelAndView("Greeting");
12+
model.addObject("message", "Dinesh");
13+
return model;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.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 RequestMappingHandlerAdapterExample {
9+
@RequestMapping("/requestName")
10+
public ModelAndView getEmployeeName() {
11+
ModelAndView model = new ModelAndView("Greeting");
12+
model.addObject("message", "Madhwal");
13+
return model;
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.spring.controller;
2+
3+
import javax.servlet.http.HttpServletRequest;
4+
import javax.servlet.http.HttpServletResponse;
5+
6+
import org.springframework.web.servlet.ModelAndView;
7+
import org.springframework.web.servlet.mvc.AbstractController;
8+
9+
public class SimpleControllerHandlerAdapterExample extends
10+
AbstractController {
11+
@Override
12+
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
13+
HttpServletResponse arg1) throws Exception {
14+
ModelAndView model = new ModelAndView("Greeting");
15+
model.addObject("message", "Dinesh Madhwal");
16+
return model;
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xmlns:context="http://www.springframework.org/schema/context"
7+
xsi:schemaLocation=
8+
"http://www.springframework.org/schema/mvc
9+
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
10+
http://www.springframework.org/schema/tx
11+
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
12+
http://www.springframework.org/schema/beans
13+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
14+
http://www.springframework.org/schema/context
15+
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
16+
<context:component-scan base-package="com.baeldung.spring.controller" />
17+
<bean
18+
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
19+
<bean
20+
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
21+
<bean id="viewResolver"
22+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
23+
<property name="prefix" value="/WEB-INF/" />
24+
<property name="suffix" value=".jsp" />
25+
</bean>
26+
</beans>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xmlns:context="http://www.springframework.org/schema/context"
7+
xsi:schemaLocation=
8+
"http://www.springframework.org/schema/mvc
9+
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
10+
http://www.springframework.org/schema/tx
11+
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
12+
http://www.springframework.org/schema/beans
13+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
14+
http://www.springframework.org/schema/context
15+
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
16+
<context:component-scan base-package="com.baeldung.spring.controller" />
17+
<bean
18+
class=
19+
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
20+
<bean
21+
class=
22+
"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
23+
<bean id="viewResolver"
24+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25+
<property name="prefix" value="/WEB-INF/" />
26+
<property name="suffix" value=".jsp" />
27+
</bean>
28+
</beans>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
xmlns:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:tx="http://www.springframework.org/schema/tx"
6+
xmlns:context="http://www.springframework.org/schema/context"
7+
xsi:schemaLocation=
8+
"http://www.springframework.org/schema/mvc
9+
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
10+
http://www.springframework.org/schema/tx
11+
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
12+
http://www.springframework.org/schema/beans
13+
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
14+
http://www.springframework.org/schema/context
15+
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
16+
<bean id="HandlerMapping"
17+
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
18+
<bean name="/greeting.html"
19+
class="com.baeldung.spring.controller.SimpleControllerHandlerAdapterExample"/>
20+
<bean id="viewResolver"
21+
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22+
<property name="prefix" value="/WEB-INF/" />
23+
<property name="suffix" value=".jsp" />
24+
</bean>
25+
</beans>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h1>Hello ${message}</h1>
4+
</body>
5+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5+
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6+
<servlet>
7+
<servlet-name>spring</servlet-name>
8+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9+
<init-param>
10+
<param-name>contextConfigLocation</param-name>
11+
<param-value>classpath*:spring-servlet_RequestMappingHandlerAdapter.xml</param-value>
12+
</init-param>
13+
<load-on-startup>1</load-on-startup>
14+
</servlet>
15+
<servlet-mapping>
16+
<servlet-name>spring</servlet-name>
17+
<url-pattern>/</url-pattern>
18+
</servlet-mapping>
19+
</web-app>

0 commit comments

Comments
 (0)