[英]404 error in simple Spring project
我刚刚开始Spring MVC框架。 我正在尝试helloworld
示例,但是我签出的每个教程都存在一些我不知道那是什么意思的问题! 例如,当我单击sayhello
时,此项目出现404错误:我使用eclipse Kepler和tomcat 7.0和Spring 4.0。 在构建路径中添加的所有Spring .jar文件。 控制器:
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>
更改
@RequestMapping(value = "/hello")
至
@RequestMapping(value = "/hello.html")
DispatcherServlet
注册的默认DefaultAnnotationHandlerMapping
不处理路径扩展映射。
您也可以指定
<mvc:annotation-driven />
在XML配置中,并带有适当的名称空间。 这将注册一个RequestMappingHandlerMapping
,它映射您的处理程序以匹配*.html
类的扩展名映射。
本教程似乎有问题。
以前我也遇到过同样的问题,如果您的服务器引导您更正了路径WEB-INF / jsp / hello.jsp,但是该页面是404,则它必须tomcat阻止了您的页面。
所以我要做的就是将该页面移至/webapp/WebContent/jsp/hello.jsp并将您的internalResuorceViewResolver路径更改为/ WebContent / jsp
这应该够了吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.