Web.xml:

 

   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?> 
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
  6.     <display-name>display-name> 
  7.     <welcome-file-list> 
  8.         <welcome-file>index.jspwelcome-file> 
  9.     welcome-file-list> 
  10.      
  11.     <servlet> 
  12.         <servlet-name>springmvcservlet-name> 
  13.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> 
  14.         <init-param> 
  15.             <param-name>contextConfigLocationparam-name> 
  16.             <param-value>/WEB-INF/mvc-servlet.xmlparam-value> 
  17.         init-param> 
  18.         <load-on-startup>1load-on-startup> 
  19.     servlet> 
  20.     <servlet-mapping> 
  21.         <servlet-name>springmvcservlet-name> 
  22.         <url-pattern>*.dourl-pattern> 
  23.     servlet-mapping> 
  24. web-app> 

 

注意:

 

1、错误提示:org.springframework.web.servlet.DispatcherServlet noHandlerFound
警告: No mapping found for HTTP request with URI [/myspring/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'springmvc'

   
   
   
   
  1. <servlet-mapping>  
  2.  
  3. <servlet-name>springmvcservlet-name>  
  4.  
  5. <url-pattern>/*url-pattern>   
  6.  
  7. servlet-mapping> 

这是一种错误的写法,可以改为:

   
   
   
   
  1. <servlet-mapping>  
  2.  
  3. <servlet-name>springmvcservlet-name>  
  4.  
  5. <url-pattern>/url-pattern>  
  6.  
  7. servlet-mapping> 

2、默认情况:

servlet-name一般与项目名称一致;若不写:

   
   
   
   
  1. <init-param> 
  2.  
  3.          <param-name>contextConfigLocationparam-name> 
  4.  
  5.          <param-value>/WEB-INF/mvc-servlet.xmlparam-value> 
  6.  
  7. init-param> 
则默认是在 /WEB-INF/servlet-name-servlet.xml

mvc-servlet.xml:

 

   
   
   
   
  1. xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
  6.    
  7.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > 
  8.         <property name="mappings"> 
  9.             <props> 
  10.                 <prop key="hello.do">helloControllerprop> 
  11.             props> 
  12.         property> 
  13.     bean> 
  14.      
  15.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
  16.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
  17.         <property name="prefix" value="/WEB-INF/jsp/"/> 
  18.         <property name="suffix" value=".jsp"/> 
  19.     bean> 
  20.      
  21.     <bean id="helloController" name="/hello" class="com.mzsx.web.controller.HelloWorldController"/> 
  22. beans> 

处理器:HelloWorldController.java

 

   
   
   
   
  1. package com.mzsx.web.controller; 
  2.  
  3. import javax.servlet.http.HttpServletRequest; 
  4. import javax.servlet.http.HttpServletResponse; 
  5. import org.springframework.web.servlet.ModelAndView; 
  6. import org.springframework.web.servlet.mvc.Controller; 
  7. public class HelloWorldController implements Controller { 
  8.     @Override 
  9.     public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { 
  10.         //1、收集参数 
  11.         //2、绑定参数到命令对象 
  12.         //3、调用业务对象 
  13.         //4、选择下一个页面 
  14.         ModelAndView mv = new ModelAndView(); 
  15.         //添加模型数据 可以是任意的POJO对象 
  16.         mv.addObject("message""Hello World!"); 
  17.         //设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面 
  18.         mv.setViewName("hello"); 
  19.         System.out.println("执行Controller---------"); 
  20.         return mv; 
  21.     } 

JSP: /WEB-INF/jsp/hello.jsp

   
   
   
   
  1. <body> 
  2.   取得数据: 
  3.   ${message } 
  4. body>