如何配置Spring MVC和Tomcat的映射

问题描述:

我尝试配置用SpringMVC的项目,我必须在编制如何配置Spring MVC和Tomcat的映射

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/test/home.jsp] in DispatcherServlet with name 'appServlet' 

我有一个jsp页面在这里错误:/测试/ src目录/主/ web应用/ WEB-INF /意见/回到Home.jsp

这是我的代码: /test/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/root-context.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <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> 
    <filter> 
     <filter-name>charsetFilter</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>charsetFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

/测试/ src目录/主/ web应用/ WEB-INF /spring/appServlet/servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <annotation-driven /> 
    <resources mapping="/resources/**" location="/resources/" /> 
    <beans:bean 
     name="test" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 
    <beans:import resource="controllers.xml" /> 
</beans:beans> 

不使用JSP扩展其直接不extention映射为HTML例如

<display-name>appServlet</display-name> 
    <servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

,然后在你的控制器映射文件:

@RequestMapping(value="/home.html", method=RequestMethod.GET) 
    public ModelAndView indexView(){ 
     ModelAndView mv = new ModelAndView("test/home"); 
     return mv; 
    } 
在这个例子中

尽快用户点击/home.html分派器servlet将解析它并检索/test/home.jsp

希望它有帮助。