我試圖設置一個框架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錯誤。該文件如下:春天3 MVC - 控制器被調用,但沒有發現意見
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 /意見我有:
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我正確得到了意見 - > welcome.jsp頁面。但是,當我請求http:// localhost:8080/projectname/app/helloworld或http:// localhost:8080/projectname/app/helloworld/執行命中正確的控制器操作,但我得到HTTP狀態404 - /projectname/WEB-INF/views/helloworld.jsp
任何人都可以告訴什麼是錯的?
感謝
對於控制器類('HelloWorldController')和其中的一個方法('Second()'),您有不同的'@ RequestMapping'值。你是否試圖將'value =「/ helloworld」'放入'helloWorld()'的'@ RequestMapping'而不是類註解? – Jan 2010-10-25 08:53:03
我一般我同意@GaryF:在URLs(「/ app/helloworld」v.s.「/ helloworld」)中存在不匹配。也許將註釋重寫爲'@RequestMapping(value =「/ app/helloworld」)'然後使用'UrlRewriteFilter'會更容易。或者你的類註解應該聲明'@RequestMapping(value =「/ app」)'方法註解應該聲明'@RequestMapping(value =「/ helloworld」)'(相對映射,參見[here](http:// static。 springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping))。 – 2011-11-17 08:56:08
嗨@Matthew我跑到相同的情況很糟糕,你能幫我擺脫http://stackoverflow.com/questions/17697899/how-to-map-dynamic-url-prj-noticeopen-2-in-spring -mvc-controller – 2013-07-18 04:20:37