我正在使用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页面并列出已安装的应用程序,哦应用程序已正确部署。
我做错了什么?
如果我使用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
后,在网址上打开网络应用程序:
http://localhost:9966/oh/
欢迎页面正确显示。