Spring3系列13-Controller和@RequestMapping

 

 

1、      Controller返回值,String或者ModelAndView

首先看一下spring的配置文件,以下web

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
        
    <context:component-scan base-package="com.lei.demo.controller" />
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/user/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>    
</beans>
 

 

第一種,返回類型爲String,Controller中的方法以下spring

    @RequestMapping(value="welcome",method=RequestMethod.GET)
    public String printMessage(ModelMap model) {
 
        model.addAttribute("message", "返回類型String");
        return "users";
    }

根據spring配置文件和以上controller,訪問「/welcome」時,對應的返回頁面爲「/WEB-INF/user/users.jsp」app

 

第二種,返回類型爲ModelAndView,Controller中的方法以下jsp

 
    @RequestMapping("/welcome")
    public ModelAndView printMessage (){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "返回類型ModelAndView ");
        mv.setViewName("users");
        return mv;
    }
 

 

兩種方法返回的頁面相同,不一樣的是第二種方法將model和view整合成ModelAndView實例,方法中不須要再加入model參數。函數

2、      @RequestMapping關聯url

@RequestMapping能夠是類級別和方法級別。url

1.        類級別,類前有@RequestMapping

 
@Controller
@RequestMapping("/user")
public class UserController {
    
    //訪問此方法的url爲「/user/manager」,url前要加入類級別的路徑/user
    @RequestMapping(value="/manager",method=RequestMethod.GET)
    public ModelAndView printMessage(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "MVC");
        mv.setViewName("users");
        return mv;
    }

//訪問此方法的url爲「/user/hello」,url前要加入類級別的路徑/user
    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "hello");
        mv.setViewName("users");
        return mv;
    }
}
 

 

2.        方法級別,類前沒有@RequestMapping

 
@Controller
public class UserController {
    
    //沒有類級別的@RequestMapping,訪問此方法的url爲「/manager」
    @RequestMapping(value="/manager",method=RequestMethod.GET)
    public ModelAndView printMessage(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "MVC");
        mv.setViewName("users");
        return mv;
    }

//沒有類級別的@RequestMapping,訪問此方法的url爲「/hello」
    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "hello");
        mv.setViewName("users");
        return mv;
    }
}
 

 

3.        @RequestMapping支持多個映射路徑映射到同一個controller

 
    @RequestMapping(value={"/hello","/foo"})
    public ModelAndView hello(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", "hello");
        mv.setViewName("users");
        return mv;
    }
 

以上「/hello」和「/foo」映射到同一個函數處理。code

 

3、      @RequestMapping的屬性

@RequestMapping有以下幾個屬性:value、method、params、headerscomponent

這幾個屬性用法以下xml

1.      @RequestMapping中的value屬性

經過value屬性,表達主要的映射,在Servlet環境中,映射路徑(如,/myPath.do),也支持Any風格的(如,/myPath/*.do)。在方法級別中的相對路徑須要類級別的主路徑支持。blog

@RequestMapping("/user")等同於@RequestMapping(value="/user")

 

2.      @RequestMapping中的method屬性

經過HTTP請求的method來縮小主映射的範圍。GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE。支持定義在類級別或方法級別。

3.      @RequestMapping中的params屬性

格式爲「paramname=paramvalue」 或 「paramname!=paramvalue」。不帶參數則表示paramvalue能夠爲任意值。

例如,params =  {"param1=1","param2!=2","param3"},表示對應的url必須包括param1,param2,param3三個參數,其中param1的值必須爲1,param2的值不能爲2,param3的值能夠爲任意值。

4.      @RequestMapping中的headers屬性

headers用來限定對應的reqeust請求的headers中必須包括的內容,例如

headers={"Connection=keep-alive"}, 表示請求頭中的connection的值必須爲keep-alive。

相關文章
相關標籤/搜索