Spring 3 MVC - 调用Controller但未找到视图

时间:2010-10-24 07:06:25

标签: spring spring-mvc

我正在尝试设置一个骨架 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 和< strong> 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>

应用-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 Resources:src”中 - &gt; com.myProject - &gt; 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 时,我正确地获取视图 - &gt; welcome.jsp 页面。但是,当我请求 http:// localhost:8080 / projectname / app / helloworld http:// localhost:8080 / projectname / app / helloworld / 执行命中正确时控制器操作,但我得到 HTTP状态404 - /projectname/WEB-INF/views/helloworld.jsp

有人可以提出什么问题吗?

由于

6 个答案:

答案 0 :(得分:1)

有一些问题。第一个是你只在你的web.xml中将/ app / * urls发送到Spring:

<url-pattern>/app/*</url-pattern>

如果请求映射是/ app / helloworld,那很好,但意味着/ helloworld甚至不会出现问题。您可能想要做的是使用urlrewrite过滤器将请求映射到/ app / *空间。

依赖于tuckey,然后将其添加到您的web.xml:

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

然后在WEB-INF目录中添加一个名为urlrewrite.xml的文件,其中包含:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
    <rule>
        <from>/images/**</from>
        <to>/images/$1</to>
    </rule>
    <rule>
        <from>/scripts/**</from>
        <to>/scripts/$1</to>
    </rule>
    <rule>
        <from>/styles/**</from>
        <to>/styles/$1</to>
    </rule>
    <rule>
        <from>/**</from>
        <to>/app/$1</to>
    </rule>
    <outbound-rule>
        <from>/app/**</from>
        <to>/$1</to>
    </outbound-rule>    
</urlrewrite>

之后,对/ helloworld的请求应该转到正确的位置。您可能也想要更改根视图控制器:

<mvc:view-controller path="/" view-name="welcome"/>

答案 1 :(得分:0)

看起来是因为您没有指定viewClass属性。

你可以试试吗

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
</bean>

由于这个

,它适用于欢迎页面
<mvc:view-controller path="/app" view-name="welcome"/>

答案 2 :(得分:0)

我认为视图需要在views / helloworld.jsp中,而不是在views / helloworld / helloworld.jsp中

答案 3 :(得分:0)

您应将视图名称设置为

mav.setViewName("/helloWorld/helloWorld");

答案 4 :(得分:0)

您应该将“mvc-config”更改为yourname-servlet.xml“yourname应该与ur web.xml文件中的servlet名称相匹配,在大小写中应该是myservlet-servlet.xml,因为spring dispatcher servlet需要查找WEB-INF文件夹中带有“-servlet”扩展名文件名的文件。 在你的myservlet-servlet.xml文件中为后缀部分,你应该这样说:“WEB-INF / yourfoldername(例如视图)”,直到你的控制器查找你在你的控制器方法中提到的你的视图名称(例如ModelAndView(“你好“))。

答案 5 :(得分:0)

您有以下配置:

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

并且您的控制器返回干净的视图名称。

运行应用程序时,验证 / WEB-INF / views / 中的视图文件是否存在。