[英]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.