0

我已经在 Apache Tomcat 上设置了 Spring MVC 3.0 和 Hibernate,并让应用程序在没有任何错误的情况下启动。

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


这是应该发生的:

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

  2. 我的家庭控制器 ( /home) 返回index位于WEB-INF/views


这是我的:我通过注释web.xml映射了家庭控制器( ),但是仍然没有找到它。/Homeweb.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" /

>


这是我的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控制器返回索引视图或任何其他视图。

4

1 回答 1

0

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

于 2012-05-25T19:24:49.173 回答