這可能很簡單,但我錯過了一些我猜的東西。問題歸結爲:我試圖使用HelloController來顯示「/WEB-INF/hello.jsp」。不幸的是,當我試圖訪問時,我得到了一個404條目。http://example.com/app/hello未找到彈簧MVC控制器
這是代碼。可能是一個簡單的修復。
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)狀態代碼。
你用' '試過了嗎? –
2012-03-21 14:32:42
我之前在那裏沒有運氣,我只是把它放回到applicationContext.xml中,沒有運氣 – Jason 2012-03-21 14:36:55
在應用程序啓動時你看到了嗎?事端如: 'INFO:org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping - 映射的URL路徑[/ hello/*]到處理程序'helloController''上? – vacuum 2012-03-21 15:19:00