[英]Spring MVC Controller Not Found
這可能是一個簡單的,但我想我錯過了一些東西。 問題歸結為:我正在嘗試使用HelloController來顯示“/WEB-INF/hello.jsp”。 不幸的是,我在嘗試訪問http://example.com/app/hello時獲得了404
這是代碼。 可能很容易解決。
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>app</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/" p:suffix=".jsp" />
</beans>
HelloController.java:
@Controller
public class HelloController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView mv = new ModelAndView();
mv.setViewName("hello");
return mv;
}
}
為hello.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello</title>
</head>
<body>
<p>Hello</p>
</body>
</html>
更新:每個請求添加了錯誤消息。
錯誤404 - 從RFC 2068超文本傳輸協議找不到 - HTTP / 1.1:10.4.5 404未找到
服務器未找到與Request-URI匹配的任何內容。 沒有說明該病症是暫時的還是永久性的。
如果服務器不希望將此信息提供給客戶端,則可以使用狀態代碼403(禁止)。 如果服務器通過一些內部可配置的機制知道舊資源永久不可用且沒有轉發地址,則應該使用410(Gone)狀態代碼。
這個問題(在web.xml
):
<url-pattern>/*</url-pattern>
這會將所有請求重定向到Spring servlet, 包括從控制器到JSP的請求。 從本質上講,控制流程將從控制器循環回到Spring。 您需要縮小范圍,以便JSP的請求直接轉到底層容器,而不是Spring。
嘗試將其更改為
<url-pattern>/app*</url-pattern>
然后再試一次。 您可能需要使用前導和尾隨slash來使其工作(例如<url-pattern>/app*</url-pattern>
或@RequestMapping("hello")
等)
請檢查您的所有控制器類/子包或其他類是否與您在以下行中提到的包相同:
<context:annotation-config />
<context:component-scan base-package="com.kfs" />
正如您在評論中提到的那樣,日志不會為您提供Controller映射到特定URL的信息。 所以我認為prblem在控制器中。
確保,控制器在“mil.army.retain.web.controller”包中並打開annotation-config:
<context:annotation-config />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.