Spring MVC 的三种映射控制器

在spring mvc 框架中我们可以使用三种url mapping的控制器

(1)BeanNameUrlHandlerMapping 默认控制器,方式是将表现层的url和bean name绑定在一起
(2)SimpleUrlHandlerMapping  通过properties形式配置url mapping和controller的映射
(3)CommonsHandlerMappingy 使用元数据的方式

我们的action

 

package  Action;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.springframework.web.servlet.ModelAndView;
import  org.springframework.web.servlet.mvc.Controller;
import  org.springframework.web.servlet.DispatcherServlet;


public   class  HomeController  implements  Controller  ... {
    
private String greeting;
    
    
public String getGreeting() ...{
        
return greeting;
    }


    
public void setGreeting(String greeting) ...{
        
this.greeting = greeting;
    }


    
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception ...{    
        
return new ModelAndView("home","message",greeting);
    }

    
}
  

 

JSP页面 命名为home.jsp因为ModelAndView对象中的视图逻辑名为home

 

<% ... @ page language="java" import="java.util.*" pageEncoding="GB18030" %>
<% ...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
  
< head >
    
< base  href ="<%=basePath%>" >
    
    
< title > My JSP 'index.jsp' starting page </ title >
    
< meta  http-equiv ="pragma"  content ="no-cache" >
    
< meta  http-equiv ="cache-control"  content ="no-cache" >
    
< meta  http-equiv ="expires"  content ="0" >     
    
< meta  http-equiv ="keywords"  content ="keyword1,keyword2,keyword3" >
    
< meta  http-equiv ="description"  content ="This is my page" >
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->
  
</ head >
  
  
< body >
    ${message}
< br >
  
</ body >
</ html >

 

web.xml

 

<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.4"  
    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"
>
  
< context-param >
    
< param-name > contextConfigLocation </ param-name >
    
< param-value > /WEB-INF/train-service.xml,/WEB-INF/train-data.xml,/WEB-INF/train-servlet.xml </ param-value >
  
</ context-param >
  
< servlet >
    
< servlet-name > train </ servlet-name >
    
< servlet-class > org.springframework.web.servlet.DispatcherServlet </ servlet-class >
    
< load-on-startup > 0 </ load-on-startup >
  
</ servlet >
  
< servlet-mapping >
     
< servlet-name > train </ servlet-name >
     
< url-pattern > *.mvc </ url-pattern >
  
</ servlet-mapping >
  
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
  
</ welcome-file-list >
</ web-app >

 

train-servlet.xml  前缀为train,因为web.xml中servlet-name为train

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"  >
< beans >
<!--  使用BeanNameUrlHandlerMapping控制器
<bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/home.mvc" class="Action.HomeController">
  <property name="greeting">
    <value>wlcome to spring mvc</value>
  </property>
</bean>
 
-->

<!--  使用SimpleUrlHandlerMapping控制器  -->
< bean  id ="HomeController"  class ="Action.HomeController" >
  
< property  name ="greeting" >
    
< value > wlcome to spring mvc </ value >
  
</ property >
</ bean >
< bean  id ="simpleUrlMapping"  class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
 
< property  name ="mappings" >
   
< props >
     
< prop  key ="/home.mvc" > HomeController </ prop >
   
</ props >
 
</ property >
</ bean >

<!--  使用commonsHandlerMappingy元数据控制器,需要jakarta common attributes包  -->
< bean  id ="urlMapping"  class ="org.springframework.web.servlet.handler.metadata.CommonsPathMapHandlerMapping" />
< bean  id ="viewResolver"  class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
  
< property  name ="prefix" >
    
< value > /jsp/ </ value >
  
</ property >
  
< property  name ="suffix" >
    
< value > .jsp </ value >
  
</ property >
</ bean >
</ beans >

 

需要注意的是,如果我们是用元数据方式,需要更改我们的action为

 

package  Action;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.springframework.validation.BindException;
import  org.springframework.web.servlet.ModelAndView;
import  org.springframework.web.servlet.mvc.AbstractCommandController;


/** */ /**
 * @@org.springframework.web.servlet.handler.commonsattributes.PathMap("/home.mvc");
 
*/

public   class  HomeController  extends  AbstractCommandController  ... {
    
private String greeting;
    
    
public String getGreeting() ...{
        
return greeting;
    }


    
public void setGreeting(String greeting) ...{
        
this.greeting = greeting;
    }



    
protected ModelAndView handle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception ...{
        
// TODO 自动生成方法存根
        return null;
    }


    
protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception ...{
        
return new ModelAndView("home","message",greeting);
    }



    
}
  


你可能感兴趣的:(spring,Web,mvc,bean,servlet)