[英]Spring MVC: Controller RequestMapping working, but return always gives a 404
我的控制器方法中有幾個 SYSOUT,它們出現在控制台日志中...為我驗證所有 @RequestMapping 是否按預期運行。 環境 bean 的 @Autowiring 也可以正常工作(也可以由 SYSOUT 正確顯示)。
但是,方法的返回(我使用的是返回 String 類型的方法)只會導致 404。 未找到 *.jsps。 項目正在使用Maven; IDE 是 Eclipse 開普勒,FWIW。
我的 ViewResolver 是沼澤標准。 看不到斷線。
我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>BluPrint</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>groupId</param-name>
<param-value>${project.groupId}</param-value>
</context-param>
<context-param>
<param-name>artifactId</param-name>
<param-value>${project.artifactId}</param-value>
</context-param>
</web-app>
我的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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xjp="http://www.corpabc.com/schema/xjp"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.corpabc.com/schema/xjp http://www.corpabc.com/schema/xjp/beans.xsd">
<context:component-scan base-package="com.corpabc.bluprint" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<import resource="classpath:corpabc/xjp/configuration/properties.xml" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSourceDB2" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/BluPrint" />
<property name="resourceRef" value="true" />
</bean>
<xjp:environment artifactId="${artifactId}" groupId="${groupId}" />
</beans>
我的控制器:
package com.corpabc.bluprint.controllers;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import corpabc.xjp.configuration.env.Environment;
/**
*
* Handles requests for the application.
*/
@Controller
@RequestMapping("/*")
public class BluPrintController {
@Autowired
private Environment xjpEnvironment;
@RequestMapping("/init")
protected String catchInit(Map<String, Object> model) {
System.out.println("Got into init method. XJP Environment: "+xjpEnvironment);
model.put("xjp", this.xjpEnvironment);
return "envtest";
}
@RequestMapping("/*")
protected String catchAllOthers(Map<String, Object> model) {
System.out.println("Got into catch-all method: ");
return "defaultPage";
}
}
我的envtest.jsp
在/WEB-INF/jsp/
...但輸入 URL ~localhost:8080/bluprint/init
... 我得到 404。
我的defaultPage.jsp
不存在......我希望這里有一個 not found 條件,這就是我輸入~localhost:8080/bluprint/
。 不確定它是否應該是 404,具體來說,但無論如何,這就是我得到的。
我懷疑您的問題出在您的 servlet 映射中。 /* 將通過您的調度程序 servlet 強制執行所有操作,包括 jsps。 嘗試丟失 *. 我會在 servlet 規范中找到相關部分並更新....
從 servlet 規范:
12.2 映射規范在Web 應用程序部署描述符中,以下語法用於定義映射: 以'/' 字符開頭並以'/*' 后綴結尾的字符串用於路徑映射。
以 '*.' 開頭的字符串前綴用作擴展映射。
空字符串 ("") 是一種特殊的 URL 模式,它精確地映射到應用程序的上下文根,即表單的請求。 在這種情況下,路徑信息是“/”,servlet 路徑和上下文路徑是空字符串(“”)。
僅包含“/”字符的字符串表示應用程序的“默認”servlet。 在這種情況下,servlet 路徑是請求 URI 減去上下文路徑,路徑信息為空。
所有其他字符串僅用於精確匹配。
因此,如果您指定 /* 覆蓋 *.jsp 映射,那么 jsp 請求將被路由回您的調度程序 servlet 而不是命中 jsp。
從 servlet-maping 中刪除 *,因為您使用 * 表示任何請求,它將開始占用任何頁面。 因此刪除 *
來自
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
到
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.