404错误:“请求的资源/控制器不可用”

时间:2012-05-25 19:04:56

标签: java spring tomcat spring-mvc http-status-code-404

我已经设置了Spring MVC 3.0&在Apache Tomcat上进行Hibernate并使应用程序无错误地启动。

但是,我可以将请求从我的(欢迎文件)redirect.jsp路由到家庭控制器(/Home)。


这是应该发生的事情:

  1. 欢迎文件redirect.jsp使用<%response.sendRedirect(/Home)%>

  2. 发送请求
  3. 我的家庭控制器(/home)返回index

  4. 中的视图WEB-INF/views
      

    这是我的web.xml:我通过注释映射了家庭控制器(/Home),但在web.xml中仍未找到它。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>app</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>app</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>Home</servlet-name>
            <servlet-class>com.app.controller.spring.HomeController</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>Home</servlet-name>
            <url-pattern>/Home</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    

    这是我的application-context.xml代码段:

    <!--bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />-->
    
    <!-- Activates various annotations to be detected in bean classes --> 
    <context:annotation-config />
    <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.   For example @Controller and @Service. Make sure to set the correct base-package--> 
    <context:component-scan base-package="com.yourmarketnet.mvc" />    
    <!-- Configures the annotation-driven Spring MVC Controller programming model.  Note that, with Spring 3.0, this tag works in Servlet MVC only!  --> 
    <mvc:annotation-driven />      
    <!-- mapping of static resources-->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <import resource="hibernate-context.xml" /
    

    &GT;


    这是我的app-servlet

    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
    
        <!--
        Most controllers will use the ControllerClassNameHandlerMapping above, but
        for the index controller we are using ParameterizableViewController, so we must
        define an explicit mapping for it.
        -->
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="index.htm">indexController</prop>
                </props>
            </property>
        </bean>
    
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"     
            p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:viewClass="org.springframework.web.servlet.view.JstlView" />
        <!--
        The index controller.
        -->
        <bean name="indexController"
              class="org.springframework.web.servlet.mvc.ParameterizableViewController"
              p:viewName="index" />
    
    </beans>
    

    但是我收到404错误,

      

    请求的资源(/Home)不可用。


    我真正想要做的是删除redirect.jsp,让应用程序转到\Home上的launch/startup控制器,\Home控制器返回索引视图或任何其他观点。

1 个答案:

答案 0 :(得分:0)

我注意到的第一件事是,在您的web.xml中,您调用了配置位置applicationContext.xml,但后来将其描述为application-context.xml。确保它实际上名为applicationContext.xml。此外,您可以从web.xml中删除“Home”servlet;这将由Spring通过您的dispatcherServlet处理。最后,在SimpleUrlHandlerMapping中,您将映射定义为index.html,但如果您希望Home处理Home请求,则还需要定义indexController }}。希望有所帮助。

相关问题