【发布时间】:2011-12-29 02:23:03
【问题描述】:
我是 Spring 新手,我使用 sts/eclipse 开发了我的第一个 spring mvc web 应用程序。
对于部署到我的虚拟主机后出现的此错误,我需要您的帮助。
请注意,这个小应用程序在 eclipse 的 tomcat 服务器中运行良好。
spring 应用的上下文路径(应用上下文)是'realtyguide'。
Webhost 服务器是私有的 Tomcat 5.5。该应用程序被打包为一个 Maven 战争包。我将war文件(解压缩)部署在我的网络主机上作为虚拟主机(在server.xml中使用上下文路径=“/ realtyguide”)。
通过http://mysite.com/realtyguide/访问网站索引
索引页面正常,但是当访问索引页面引用的其他网页时,浏览器返回错误:
“在此服务器上找不到请求的 URL /realtyguide/page-name。”
我验证了索引页面检测到了我的应用程序上下文,因为我的链接使用了 c:url 和 spring:url 并且索引页面的页面源在我的链接中显示了上下文路径,例如 href="/realtyguide/resources/styles/standard.css” 和
<a href="/realtyguide/testpage" >Test - next page</a>
控制器似乎没有检测到映射的 URL。这可能是导致此错误的原因吗?
这里是 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>
这里是根上下文.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配置(servlet-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"
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);
return "index.tiledef"; // 'index' is a Tile definition in tiles.xml,
}
@RequestMapping(value = "/", method=RequestMethod.POST)
public String submitForm(@ModelAttribute("searchFormBean") SearchFormBean searchFormBean,
BindingResult result, SessionStatus status, Model model) {
searchFormValidator.validate(searchFormBean, result);
if (result.hasErrors()) {
model.addAttribute("searchFormBean", searchFormBean);
return "index.tiledef";
} else {
String searchTerm = searchFormBean.getSearchTerm();
searchTerm = searchTerm.replaceAll(" ","_");
return "redirect:/propertylistings/" + searchTerm + "/1"; // If no errors - display the listings page
}
}
@RequestMapping("/testpage")
public String gotoNextPage() {
return "nextpage.tiledef";
}
这是 webhosts 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>
<Host name="mysite.com" appBase="/home/tsoriano/public_html">
<Alias>www.mysite.com</Alias>
<Alias>mysite.com</Alias>
<Context path="/realtyguide" reloadable="true" docBase="realtyguide" debug="0"/>
<Context path="/manager" debug="0" privileged="true"
docBase="/usr/local/jakarta/tomcat/server/webapps/manager">
</Context>
</Host>
</Engine>
</Service>
</Server>
这个小应用程序在 eclipse/springsource 工具套件中运行良好。
找不到此 URL 错误的解决方案是什么?请帮帮我。
问题已解决
我怀疑我的虚拟主机服务器可能配置错误,但我无法检查,因为我有一个共享主机计划。为了验证问题不在于我的应用程序,我将应用程序部署在笔记本电脑中 eclipse 之外的本地 Tomcat 中。它运行完美。
所以我再次向技术支持报告“Tomcat servlet 未加载”。我坚持要技术支持,问题出在他们头上。他们的apache配置可能有问题。 Apache 与 Tomcat 的对话不正确。
他们终于修复了它,这是我从他们那里收到的确认信息 - “我们更改了服务器配置文件中的一些设置,以便对您的域的所有请求都将由 tomcat 而不是 Apache 处理。”
我希望这对使用共享主机计划并遇到相同错误的人有所帮助。如果您觉得您的 Tomcat 服务器设置正确,但仍然不断收到 URL 未找到错误,请坚持让您的技术支持人员查看他们的服务器配置。或者,如果您负担得起的话,可以购买一台专用服务器,这样您就可以完全控制您的 apache 服务器和 tomcat。
【问题讨论】:
标签: java spring model-view-controller url tomcat