Spring Web MVC 404错误——以root身份部署在Tomcat服务器上



我快疯了。请帮助。

我是Spring的新手,我使用sts/eclipse开发了我的第一个Spring mvc web应用程序。它可以在eclipse的tomcat服务器中完美运行。

应用程序上下文是/realtyguide,我在eclipse中运行它在http://localhost:8080/realtyguide/

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
           http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>Realty Guide</display-name>
      <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
      </context-param>
      <!-- Creates the Spring Container shared by all Servlets and Filters -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- Handles Spring requests -->
      <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
</web-app>

这是我的root-context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans     
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <!-- Root Context: defines shared resources visible to all other web components -->
</beans>
下面是我的Spring应用程序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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
    <context:annotation-config />
    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <context:component-scan base-package="com.springproject.realtyguide" />
    <!-- Enables the Spring MVC @Controller programming model -->    
    <mvc:annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources -->
    <mvc:resources mapping="/resources/**" location="/resources/"/>   
    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static resource requests to the container's default Servlet -->
    <mvc:default-servlet-handler/>      

    <!-- Bean to provide Internationalization  -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/i18n/messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:META-INF/spring/database.properties" />
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:META-INF/hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- __________ BEAN ENTRIES FOR TILES 2 -->
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/layouts/tiles.xml</value>
            </list>
        </property>
    </bean>
    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
        <property name="order" value="0"/> 
        <property name="viewClass"> 
            <value>org.springframework.web.servlet.view.tiles2.TilesView </value>
        </property>
        <property name="requestContextAttribute" value="requestContext"/>
        <property name="viewNames" value="*.tiledef"/>
    </bean>
    <bean id="jstlViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="order" value="1"/> 
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix" value="/WEB-INF/views/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean> 
    <!-- __________ END OF BEAN ENTRIES FOR TILES 2 -->
    <!-- Resolves localized <theme_name>.properties files in the classpath to allow for theme support -->
    <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix" value="theme-" />
    </bean>
    <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">  
        <property name="defaultThemeName" value="standard" />
    </bean>
</beans>

这是根

的控制器处理程序方法
@RequestMapping(value = "/", method=RequestMethod.GET)
public String setupForm(
        @ModelAttribute("searchFormBean") SearchFormBean searchFormBean, 
        Model model) {
    model.addAttribute("searchFormBean", searchFormBean);
    // 'index' is a Tile definition in tiles.xml
    return "index.tiledef";
}

tomcat/conf/server.xml

我删除了大部分"注释掉"的东西,使它更短。

                <?xml version="1.0" encoding="UTF-8"?>
                <Server port="9200" shutdown="SHUTDOWN">
                  <!-- Comment these entries out to disable JMX MBeans support used for the 
                       administration web application -->
                  <Listener className="org.apache.catalina.core.AprLifecycleListener" />
                  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
                  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
                  <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
                  <!-- Global JNDI resources -->
                  <GlobalNamingResources>
                    <!-- Test entry for demonstration purposes -->
                    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
                    <!-- Editable user database that can also be used by
                         UserDatabaseRealm to authenticate users -->
                    <Resource name="UserDatabase" auth="Container"
                              type="org.apache.catalina.UserDatabase"
                       description="User database that can be updated and saved"
                           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
                          pathname="conf/tomcat-users.xml" readonly="true" />
                  </GlobalNamingResources>

                  <!-- Define the Tomcat Stand-Alone Service -->
                  <Service name="Catalina">

                    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
                    <Connector port="9201" maxHttpHeaderSize="8192"
                               maxThreads="10" minSpareThreads="5" maxSpareThreads="75"
                               enableLookups="false" redirectPort="8443" acceptCount="100"
                               connectionTimeout="20000" disableUploadTimeout="true" />
                    <!-- Define an AJP 1.3 Connector on port 8009 -->
                    <Connector port="9203" 
                               enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />

                    <!-- Define the top level container in our container hierarchy -->
                    <Engine name="Catalina" defaultHost="localhost">

                      <!-- This Realm uses the UserDatabase configured in the global JNDI
                           resources under the key "UserDatabase".  Any edits
                           that are performed against this UserDatabase are immediately
                           available for use by the Realm.  -->
                      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                             resourceName="UserDatabase"/>

                      <!-- Define the default virtual host
                           Note: XML Schema validation will not work with Xerces 2.2.
                       -->
                      <Host name="localhost" appBase="webapps"
                       unpackWARs="true" autoDeploy="true"
                       xmlValidation="false" xmlNamespaceAware="false">
                       </Host>
                    </Engine>
                  </Service>
                </Server>

我的目标

example.com处调出网站的索引页,在域名后去掉额外的/.../

我做了什么

作为虚拟主机的根节点部署在共享的web主机上:

我在stackoverflow上读到我需要作为根部署所以…

我将它打包在STS/Eclipse中:右键单击项目"realtyguide"> Run as> Maven包

Webhost服务器是私有的Tomcat 5.5。我在我的webhost上部署了war文件(解压缩它)作为虚拟主机的默认应用程序(在server.xml中使用上下文path = ""),这样我就可以访问example.com而不是example.com/realtyguide的网站。

成功。至少我是这么想的。我可以在http://example.com/

打开索引页。

访问从索引页引用的其他网页返回404错误:

请求的URL/realtyguide/page-name在此服务器上找不到

即使浏览器地址栏显示上下文路径与完整的URL http://www.example.com/realtyguide/page-name

我认为应用程序绕过了spring分派器servlet,因此无法识别请求URL中的上下文路径' realtyguide ' -导致404错误。

我不知道该如何处理这件事。

问题:

  1. 我该怎么做才能在不破坏应用程序的情况下将网站的索引页放在example.com而不是example.com/realtyguide/ ?我已经尝试了index.jsp文件(在WEB-INF)转发到上下文路径的索引,但它没有解决问题。也许我做错了。

  2. 是否有一些转发机制,我可以使用,以便网站访问者进入example.com,并立即转发到example.com/realtyguide/的索引页,以便所有url应由应用程序处理?

我思考问题的方式正确吗?你以前用过什么解决方案吗?

我非常感谢你的帮助。感谢您的宝贵时间。

您需要使您的web应用程序成为Tomcat的默认web应用程序。说明在这里:

http://wiki.apache.org/tomcat/HowTo How_do_I_make_my_web_application_be_the_Tomcat_default_application.3F

一旦完成,使用index.jsp转发到Spring应用程序的索引就很简单了。

WAR根目录下包含以下内容的index.jsp文件应该可以做到这一点:

<jsp:forward page="/realtyguide/" />

最新更新