PDA

View Full Version : Problem with Servlet Mapping



asmith120
Sep 23rd, 2010, 04:45 PM
First off, let me start by saying that I am new here and I am new to Spring, so forgive me for asking remedial questions. I have been going through the Spring Framework Tutorial that is provided by spring source on this site (http://static.springsource.org/docs/Spring-MVC-step-by-step/index.html). In going through this tutorial, I have come across some odd behavior regarding servlet mapping (or so I think). What is happening is that I am trying to map a "hello.htm" url to reference a specific class which then pulls the view from a hello.jsp file. Well, this is not working. When I try to look up http://localhost:8080/springapp/hello.htm Tomcat says the resource could not be found. When I try to pull up http://localhost:8080/springapp/hello.jsp, the page views as it should. However, in the tutorial, it specifically states that you are supposed to be able to view it through looking up the hello.htm URL. I have looked over and over all of the files in the project and cannot figure out what could be going on. Please help. I believe that all of the relevant config files are listed below...

/WEB-INF/springapp-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 springapp Dispatcher Servlet -->

<bean name="/hello.htm" class="springapp.web.HelloController"/>

</beans>

/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>

</web-app>

/WEB-INF/jsp/hello.jsp

<%@ include file="/WEB-INF/jsp/include.jsp" %>

<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}"/></p>
</body>
</html>

springapp.web.HelloController.java

package springapp.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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.Date;

public class HelloController implements Controller {

protected final Log logger = LogFactory.getLog(getClass());

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String now = (new Date()).toString();
logger.info("Returning hello view");

return new ModelAndView("WEB-INF/jsp/hello.jsp", "now", now);

}

}


If there are any other files that I should be looking at please let me know and thanks in advance for the help!

Grzegorz Grzybek
Sep 24th, 2010, 12:12 AM
Hi

After changing the JSP it all works for me. Please find attached full, working WAR (without libs). Please add these:
aopalliance-1.0.jar
jcl-over-slf4j-1.5.10.jar
jstl-1.2.jar
logback-classic-0.9.18.jar
logback-core-0.9.18.jar
slf4j-api-1.5.10.jar
spring-beans-2.5.6.SEC02.jar
spring-context-2.5.6.SEC02.jar
spring-context-support-2.5.6.SEC02.jar
spring-core-2.5.6.SEC02.jar
spring-web-2.5.6.SEC02.jar
spring-webmvc-2.5.6.SEC02.jar
to WEB-INF/lib and deploy the attached WAR.

Change JSP to:


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings, it is now <c:out value="${now}"/></p>
</body>
</html>


regards
Grzegorz Grzybek

asmith120
Sep 24th, 2010, 09:49 AM
Thanks for your help! It ended up being a different problem, but thanks for your help. The issue was that at one point (by accident) i had my web.xml named WEB.xml. I realized this a while ago and changed it in the eclipse project, but this specific change never got transfered over to Tomcat. as soon as I renamed the file in tomcat, everything worked perfectly. Thanks again for all of your help.