我使用Eclipse和Tomcat 8。我使用Spring创建了一个动态web项目。这是我的web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring Open Hospital</display-name>
<description>Spring Open Hospital sample application</description>
<!-- When using Spring JDBC, use the following: -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>jdbc</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/business-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- - Servlet that dispatches request to registered handlers (Controller
implementations). -->
<servlet>
<servlet-name>oh</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/mvc-core-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>oh</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- used to provide the ability to enter Chinese characters inside the
Owner Form -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- used so we can use forms of method type 'PUT' and 'DELETE' see here:
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#rest-method-conversion -->
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>oh</servlet-name>
</filter-mapping>
business-config.xml文件包含有关数据源定义和组件扫描存储库和服务bean的信息。
我的mvc-core-config.xml文件包含以下行:
<!-- uses WebJars so Javascript and CSS libs can be declared as Maven dependencies
(Bootstrap, jQuery...) -->
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
<mvc:view-controller path="/" view-name="welcome" />
<!-- serve static resources (*.html, ...) from src/main/webapp/ Required
when both servlet-mapping is '/' and static resources need to be served -->
<mvc:default-servlet-handler />
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- view name resolved using bean of type InternalResourceViewResolver
(declared in mvc-view-config.xml) -->
<property name="defaultErrorView" value="exception" />
<!-- results into 'WEB-INF/jsp/exception.jsp' -->
<property name="warnLogCategory" value="warn" />
<!-- needed otherwise exceptions won't be logged anywhere -->
</bean>
和mvc-view.config.xml文件包含:
<mvc:view-resolvers>
<mvc:content-negotiation use-not-acceptable="true">
<mvc:default-views>
<bean class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="" />
</bean>
</mvc:default-views>
</mvc:content-negotiation>
<mvc:jsp prefix="/WEB-INF/jsp/" suffix=".jsp" />
</mvc:view-resolvers>
因此,在运行tomcat服务器后,在localhost:8080/oh打开我的应用程序,我得到一个404页面,但我期待我的welcome.jsp页面。
在localhost:8080打开tomcat页面并列出已安装的应用程序,oh应用程序已正确部署。
我做错了什么?
如果我使用tomcat7 maven插件:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>tomcat-development-server</server>
<port>9966</port>
<path>/oh</path>
</configuration>
</plugin>
和运行:mvn tomcat7:run
后,打开web应用在url:http://localhost: 9966/哦/
欢迎页面正确显示
对于您的"oh" servlet,您指定了一个/
URL模式,因此您的欢迎页面的URL是localhost:8080/name_of_web_project
如果您希望您的servlet在localhost:8080/name_of_web_project/oh访问,您必须在servlet映射中给出/oh
路径:
<servlet-mapping>
<servlet-name>oh</servlet-name>
<url-pattern>/oh</url-pattern>
</servlet-mapping>
我不确定您是否也必须相应地修改<mvc:view-controller path="/" view-name="welcome" />
,因为我从未使用过,请让我知道。