Reputation: 1653
hi I am going through my first lessons with spring 3.I created a dynamic web app in eclipse with the following structure.
spring3mvc \src\my.spring.HelloWorldController.java
\WebContent
|
|-----WEB-INF\jsp\hello.jsp
|-----index.jsp
|-----WEB-INF\web.xml
|-----WEB-INF\spring-servlet.xml
|-----WEB-INF\lib\...*.jar files
I created the spring-servlet.xml as below
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/context
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="my.spring" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
<property name="contentType" value="text/html; charset=utf-8" />
</bean>
</beans>
and coded the controller
package my.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
index.jsp has a link to hello view
<html>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
finally in hello.jsp I have put
<html>
<body>
${message}
</body>
</html>
My web.xml has
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
When I run the app on Tomcat6 server( thru eclipse),I can see the index page at http://localhost:8080/Spring3MVC/ .It displays the link to hello page.When I click on it(http://localhost:8080/Spring3MVC/hello.html),I get a 404 error.
message /Spring3MVC/hello.html
description The requested resource (/Spring3MVC/hello.html) is not available.
Any idea how I can solve this?
thanks
mark.
Upvotes: 2
Views: 5757
Reputation: 240860
You need to configure ViewResolver
.
Here is sample configuration:
<bean id="viewResolver"
class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
When you do return new ModelAndView("hello", "message", message);
from above conf. it will try loading
prefix value + view name + suffix value
which will be required jsp.
Also you need to map your servlet in web.xml following way
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Upvotes: 2
Reputation: 809
I had the same issue.
Steps to Recreate
Expected Result
The browser should launch and display the Hello World home page as defined by the HomeController > home action and the home.jsp view page.
Actual Result
404 Error
The Solution
1. Open up HomeController.java
2. Add a space anywhere in the file. Yeah. A space. Hit the spacebar. This is the solution. LOL
3. Run the project again as you did before.
This gives you the expected result.
My Hypothesis
The HomeController class isn't compiling initially, so the component-scan method isn't finding it; making it necessary to make a change to the file, and save it. This forces the class to be compiled, and therefore makes the home controller and the /home action discoverable.
I messed around with configuration files until I was pulling my hair out. I can't even tell you what made me think to try this. ;-) Glad I got it figured out though. Hopefully the Spring MVC 3 team will look into this, as it could easily create a serious barrier to entry to an otherwise cool framework.
Upvotes: 0
Reputation: 120771
I belive the problem is not the View resolver (it would print other exceptions).
Read the error message carfully, it tells what the problem is:
message /Spring3MVC/hello.html description
The requested resource (/Spring3MVC/hello.html) is not available.
It is that the hello .html (handler) can not be found, not the jsp. -- But I don't know what the exact problem it. -- I tryed to reproduce the error, but I did not get exact the same error message.
added -- find the problem
When you start the Server it prints all mappings to the controller in the log file. In your case there must by something like
INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/hallo] onto handler 'halloController'
If you don't have such a statement, then something is wrong with your context scan, or you have forget to enable the annotation driven MVC @Controller programming model. This can be enabled by adding:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
Upvotes: 1