Reputation: 2818
I'm new to Spring. I worked through the sections of this tutorial that cover web flow. My ultimate goal is to use Spring to implement new features in a legacy servlet webapp and then gradually replace the existing code making the servlet webapp into a Spring webabb.
So, to that end, I decided to go through the web flow part of the tutorial again, changing names to make my own first "hello world" screen with Spring within the development copy of the legacy servlet application.
My problem is that when I put the servlet mappings for Spring into my web.xml, I get 404s trying to get to my landing page.
I made a simplified version of my web.xml, with just one legacy servlet in it ( for the landing page ) and Spring. It works with the Spring stuff commented out, but not otherwise. Here it is, my WEB-INF/web.xml for the "abc" webapp
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LogIn</servlet-name>
<servlet-class>
com.utilities.LogIn
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogIn</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is my WEB-INF/abc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- the application context definition for the NSD webapp DispatcherServlet -->
<beans name = "/hello.htm" class = "com.somecompany.web.HelloController"/>
</beans>
Here is the code for my elementary controller:
package com.somecompany.web;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.apache.log4j.Logger;
public class HelloController implements Controller {
protected static final Logger logger = Logger.getLogger(HelloController.class);
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning view for CBS Search ....");
return new ModelAndView("hello.jsp");
}
}// end class
Again, my problem isn't with the controller or the view (jsp)...yet. Right now, when I include the Spring servlet mappings in my web.xml, I can't get to my landing page, I get a 404. When I yank the Spring servlet mappings, that problem goes away.
I'm a raw beginner with Spring, so I am not sure where to look.
Upvotes: 2
Views: 1795
Reputation: 2659
Try a forward slash on your ModelAndView view name:
return new ModelAndView("/hello.jsp");
Upvotes: 0
Reputation: 1866
As mentioned by others its better to use Spring 3.0 which is more easier to configure. Since you have started with this i am giving some of my thoughts. You need to use something like SimpleUrlHandlerMapping which takes care of resolving your url pattern to appropriate controllers. Take a look at this spring reference documentation
http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html.
Hope this helps.
Upvotes: 0
Reputation: 8969
What is your landing page? /hello.htm
? your have just mapped everything with htm
extension to a dispatcher, but you only have a controller which deals with hello.htm
. You should get 404
if you are trying to access /index.htm
. Change your mapping or create a controller for other urls.
However, if I were you, I would use Spring annotations in Spring 3.0 to configure the web application. It is much simpler.
Upvotes: 1
Reputation: 11880
The issue is probably with the <url-pattern>
in your web.xml
. You cannot use ".jsp
" as your mapped extension.
There is some pretty detailed discussion about the low-level reasons in this thread... but the long and short of it is that the "*.jsp
" file extension is special to the underlying Java servlet specification. You're trying to make Spring hijack that. This is why the tutorial you linked to is using "*.htm
" instead.
Trying changing your <url-pattern>
in web.xml
to something else (such as *.htm
), and see if you can pull up your test URL with that extension.
Upvotes: 1