I have develop this example basing uppon the Spring MVC Showcase example.
The result I want to achieve is as follows: a web application that manage 2 kinds of HTTP Request:
FIRST HTTP Request type:: HTTP request towards resources having a structure [/b]/folder[/b]
SECOND HTTP Request type: HTTP request towards resources having a structure [/b]/file.html[/b]
The showcase example manage by default the first type of HTTP Request, so I have add an other DispatcherServlet in my web.xml configuration file to manage the HTTP Request towards *.html pattern but I am finding some problem to handle the second type of request
So this is my web.xml code:
As you can see in this web.xml fine I have defined 2 DispatcherServlet: the first one that manage the first type of HTTP Request and the second one that manage the second type.Code:<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet> <servlet-name>appServlet2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet2-context.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appServlet2</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list> </web-app>
Ok, the DispatcherServlet that manage the HTTP Request towards resources having form: file.html have servlet-name=appServlet2 and it is configured by the servlet2-context.xml configuration file.
This is the code of servlet2-context.xml:
this is a "copy" of the servlet-context.xml file that configure the original DispatcherServlet of the example, the only difference, made by me, is that I import a different file: controller2.xmlCode:<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven conversion-service="conversionService"> <argument-resolvers> <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/> </argument-resolvers> <async-support default-timeout="3000"> <callable-interceptors> <beans:bean class="org.springframework.samples.mvc.async.TimeoutCallableProcessingInterceptor" /> </callable-interceptors> </async-support> </annotation-driven> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <!-- Only needed because we install custom converters to support the examples in the org.springframewok.samples.mvc.convert package --> <beans:bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <beans:property name="formatters"> <beans:bean class="org.springframework.samples.mvc.convert.MaskFormatAnnotationFormatterFactory" /> </beans:property> </beans:bean> <!-- Only needed because we require fileupload in the org.springframework.samples.mvc.fileupload package --> <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> <!-- Imports user-defined @Controller beans that process client requests --> <beans:import resource="controllers2.xml" /> <task:annotation-driven /> </beans:beans>
This file simply specify the package in wich are actived the component scan managed by this servlet:
So, for this servlet is actived the component scan that search annotation in the following package: org.springframework.samples.mvc2.simpleCode:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Maps '/' requests to the 'home' view --> <mvc:view-controller path="/" view-name="home"/> <context:component-scan base-package="org.springframework.samples.mvc2.simple" /> </beans>
Ok...In this package I have created the following controller class:
As you can see the sayHello method is annoted by @RequestMapping("/hello") annotation and, in theory, have to manage HTTP Request towards /hello.html file !!!Code:package org.springframework.samples.mvc2.simple; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/hello") public @ResponseBody String sayHello() { System.out.println("I am inside HelloController class, sayHello() method"); return "My Hello world!"; } }
But don't work well !!!
Adding in my home.jsp view I add these links:
When I click on these links I have the following error message:Code:<li> <a id="simpleLink" class="textLink" href="<c:url value="hello.html" />">GET hello.html</a> </li> <li> <a href="hello.html">Say Hello</a> </li>
HTTP Status 404
....
- description The requested resource is not available.
And this is my stacktrace:
So it is clear that Spring have loaded the appServlet2 DispatcherServlet (the DispatcherServlet that is configured to manage HTTP Request toward filename.html resources) but don't find a mapping for the hello.html resources...Code:21:01:47 [tomcat-http--28] DispatcherServlet - DispatcherServlet with name 'appServlet2' processing GET request for [/spring-mvc-showcase/hello.html] 21:01:47 [tomcat-http--28] RequestMappingHandlerMapping - Looking up handler method for path /hello.html 21:01:47 [tomcat-http--28] RequestMappingHandlerMapping - Did not find handler method for [/hello.html] 21:01:47 [tomcat-http--28] PageNotFound - No mapping found for HTTP request with URI [/spring-mvc-showcase/hello.html] in DispatcherServlet with name 'appServlet2' 21:01:47 [tomcat-http--28] DispatcherServlet - Successfully completed request


Reply With Quote
