【问题标题】:Spring Web MVC 404 error – deployed as root on a webhost Tomcat serverSpring Web MVC 404 错误 – 在 webhost Tomcat 服务器上以 root 身份部署
【发布时间】:2011-12-24 19:26:01
【问题描述】:

这快把我逼疯了。请帮忙。

我是 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 上看到我需要以 root 身份部署,所以...

我在 STS/Eclipse 中通过以下方式将其打包:右键单击项目“realtyguide”> Run as > Maven package

Webhost 服务器是私有的 Tomcat 5.5。我在我的虚拟主机上部署了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?

我是否以正确的方式思考问题?您以前使用过任何解决方案吗?

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

【问题讨论】:

  • 您是否有理由不发布任何代码?如果您不向我们提供一些有帮助的了解问题所在...,则可能无法为您提供帮助
  • 我的错。请使用我的配置文件和控制器处理程序方法查看已编辑的帖子。
  • @nicholas.hauschild 编辑了我的帖子以包含我的虚拟主机的 tomcat/conf/server.xml 文件

标签: java spring model-view-controller http-status-code-404 root


【解决方案1】:

您需要使您的 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/" />

【讨论】:

  • 根据您提供的链接上的说明,我将在 /webapps/ROOT 目录下部署目录和文件结构。 index.jsp 文件的 WAR 根是什么意思?你的意思是'/webapps/ROOT'或'/webapps/ROOT/WEB-INF'的目录?我只是想确定我做对了。感谢您的帮助。
  • 我编辑了我的帖子以包含我的 webhost 的 conf/server.xml,并且我还按照链接上的说明进行操作,除了最后一部分需要编辑 web.xml 以包含“我的第一个”的 servlet 映射小服务程序'。我假设转发 index.jsp 文件将替换它。但我收到“请求的资源 (/realtyguide/) 不可用”的 404 错误。 index.jsp 文件真的需要 servlet 映射吗?
  • index.jsp 文件应该在您的 WAR 文件的子目录中。如果您的 WAR 文件被分解(或者如果您解压缩它),它应该直接位于“realtyguide”目录中(假设您的 WAR 文件名为 realtyguide.war)。在您的情况下,它将直接位于“webapps/ROOT”下。
  • 我按照您的指示部署在 /tomcat/webapps/ROOT 目录下。我还在 ROOT 目录下使用了包含 的 index.jsp。但问题仍然存在。索引页面出现,但访问索引页面引用的其他网页仍然返回 404 错误:“在此服务器上找不到 URL /realtyguide/。” 我还能做些什么来以root身份部署应用程序时访问spring的应用程序上下文'realtyguide'?你有其他建议我可以尝试吗?谢谢。
  • 由于您将 WAR 内容移至 ROOT 目录,因此 /realtyguide/ 不再提供它。它在“/”处可用。如果您希望站点在 /realtyguide/ 可用,您需要执行以下操作:(1) 将 WAR 移回 webapps/realtyguide 目录,(2) 将 ROOT 目录恢复到默认的 Tomcat webapp,以及 (3 ) 修改 webapps/ROOT/index.jsp 文件,使其转发到您的应用程序目录(即 /realtyguide/index.jsp 或任何您的登录页面)。
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 2015-05-27
  • 2018-02-27
  • 2018-05-02
  • 1970-01-01
相关资源
最近更新 更多