PDA

View Full Version : basic web example not working


KENTOSI
Feb 8th, 2006, 10:16 PM
Hi All,

I'm following trying to get a very basic spring MVC webapp working, but I keep getting an error on my tomcat saying:

The requested resource () is not available.

Here is my web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


Here is my welcome-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<!--
- Application context definition for "example" DispatcherServlet.
-->
<beans>
<bean name="/index.html"
class="learning.spring.web.controller.SampleController"/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>


Here is my SampleController.java:

/*
* Spring framework demo
*/

package learning.spring.web.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractContro ller;

public class SampleController extends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("welcome");

if (logger.isDebugEnabled()) {
logger.debug("Adding date to modelAndView ...");
}
mav.addObject("date", new Date());

return mav;
}

private static final Log logger = LogFactory.getLog(SampleController.class);
}



And finally, here is WEB-INF/jsp/welcome.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html>
<head>
<title>Hello World!</title>
</head>

<body>
<h1>Hello World!</h1>

<p>
Right now the time is <c:out value="${date}"/>.
</p>
</body>
</html>


The url i am trying to access is: /<webapp-context>/index.html

Any ideas why this should not be working? I want to learn Spring MVC but this one issue's stopping me from getting started.

EndlessWinter
Feb 9th, 2006, 04:55 AM
1) You didn't specify application context xml file in web.xml:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/welcome-servlet.xml</param-value>
</init-param>
2) You should call /<webapp-context>/welcome.html for welcome.jsp with view resolver you configured

KENTOSI
Feb 9th, 2006, 06:09 PM
no, that didn't work either =(

basically, it works when i have the following:

in web.xml:
<url-pattern>/welcome/*</url-pattern>

in welcome-servlet.xml:
<bean name="/welcome.html" class="learning.spring.web.controller.SampleController"/>

Now the problem with the above case is that I have to call the webapp by the following url:
http://localhost:8081/<webapp-context>/welcome/welcome.html

I don't want the extra ".../welcome/..." in the url.

i just want to call:
http://localhost:8081/<webapp-context>/welcome.html

This is where I'm stuck.

dhewitt
Feb 9th, 2006, 06:12 PM
Try mapping your servlet to *.html.

This is similar to a discussion in this thread : http://forum.springframework.org/showthread.php?t=21840

I believe the /* mapping for your servlet is overriding the built-in servlet engine mapping for jsp files.

KENTOSI
Feb 9th, 2006, 06:41 PM
i think you're right dhewitt. seems like I have to map everything to *.action or *.html or something like that.

bummer.

that's cool though. shouldn't be too much of a hassle.

thanks for your help guys.