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’m using WebLogic 9.2
- I converted the directory tree of my webapp to mimic the layout that Spring webapps use
- I copied spring.jar and spring-mvc.jar into my WEB-INF/lib
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.
The issue is probably with the
<url-pattern>in yourweb.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>inweb.xmlto something else (such as*.htm), and see if you can pull up your test URL with that extension.