I am just started Spring MVC framerwork . I am trying do helloworld
example but every tutorials i checked out , all have some problem that I do not know thats mean ! for example this project has 404 error when i click on sayhello
: I use eclipse Kepler and tomcat 7.0 and Spring 4.0 ; all Spring .jar files added in build path . controller :
package net.spring3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
@Controller
public class HelloWorldController {
@RequestMapping(value = "/hello")
public ModelAndView helloWorld() {
String message = "Hello World";
return new ModelAndView("hello", "message", message);
}
}
spring-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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="net.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<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>
</web-app>
WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring</title>
</head>
<body>${message}
</body>
</html>
index.jsp :
<html>
<head>
<title></title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
Change
@RequestMapping(value = "/hello")
to
@RequestMapping(value = "/hello.html")
The default DefaultAnnotationHandlerMapping
that is registered by the DispatcherServlet
does not handle path extension mappings.
You could, alternatively, specify
<mvc:annotation-driven />
in your XML configuration, with the appropriate namespace. This will register a RequestMappingHandlerMapping
which maps your handlers to match extension mappings like *.html
.
This tutorial seems bugged.
I had same problem before, if your server lead you to correct path WEB-INF/jsp/hello.jsp but that page is 404, it must tomcat blocked you page.
So what I did is move that page to /webapp/WebContent/jsp/hello.jsp and change your path for internalResuorceViewResolver to /WebContent/jsp
That should do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.