【发布时间】:2015-09-28 18:43:37
【问题描述】:
我正在尝试建立一个框架 Spring 3 MVC 项目,但我在渲染视图时遇到了困难。我已经按照 mvc-basic 示例项目和http://blog.springsource.com/2009/12/21/mvc-simplifications-in-spring-3-0/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Interface21TeamBlog+%28SpringSource+Team+Blog%29 中描述的结构来设置 web.xml、app-config.xml 和 mvc-config.xml 文件。控制器被调用,但是当它到达查找视图并渲染它的点时,我得到一个 404 错误。文件如下:
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">
<!-- Handles all requests into the application -->
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/app-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
app-config.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: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">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.myProject" />
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
<!-- Configures Spring MVC -->
<import resource="mvc-config.xml" />
</beans>
mvc-config.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<mvc:view-controller path="/app" view-name="welcome"/>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
<!-- Saves a locale change using a cookie -->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
在“Java 资源:src”-> com.myProject -> HelloWorldController.java 我有:
package com.myProject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/helloworld")
public class HelloWorldController {
@RequestMapping(method=RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView mav = new ModelAndView();
mav.setViewName("helloworld");
mav.addObject("message", "Hello World!");
return mav;
}
@RequestMapping(value="/Second", method = RequestMethod.GET)
public ModelAndView Second(){
ModelAndView mav = new ModelAndView();
mav.setViewName("Second");
mav.addObject("message", "Hello World!");
return mav;
}
}
在 WebContent/WEB-INF/views 我有:
WebContent (folder)
WEB-INF (folder)
views (folder)
helloworld (folder)
helloworld.jsp (.jsp view)
helloworld.jsp (.jsp view)
welcome.jsp (.jsp view)
视图中有简单的 html。当我请求 http://localhost:8080/projectname/app 时,我正确地获得了 views -> welcome.jsp 页面。但是,当我请求 http://localhost:8080/projectname/app/helloworld 或 http://localhost:8080/projectname/app/helloworld/ 执行命中正确控制器操作,但我得到 HTTP Status 404 - /projectname/WEB-INF/views/helloworld.jsp
谁能告诉我哪里出了问题?
谢谢
【问题讨论】:
-
控制器类 (
HelloWorldController) 和其中的方法 (Second()) 有不同的@RequestMapping值。你有没有试过把value="/helloworld"放到helloWorld()的@RequestMapping而不是类注解中? -
我一般同意@GaryF:URL 不匹配(“/app/helloworld”与“/helloworld”)。也许将注释重写为看起来像
@RequestMapping(value="/app/helloworld")然后使用UrlRewriteFilter更容易。或者你的类注解应该声明@RequestMapping(value="/app"),方法注解应该声明@RequestMapping(value="/helloworld")(相对映射,参见here)。 -
嗨@Matthew,我也遇到了同样的情况,你能帮我解决一下stackoverflow.com/questions/17697899/…
标签: spring spring-mvc