日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring 基于注解的控制器配置

發布時間:2024/4/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 基于注解的控制器配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://ttaale.iteye.com/blog/787586

spring 基于注解的控制器配置

  • 博客分類:
  • spring
SpringBeanServletMVCWeb

13.12.?基于注解的控制器配置

現時對于一些類型的配置數據有一個趨勢,就是偏愛注解方式而不是XML文件。為了方便實現,Spring現在(從2.5開始)提供了使用注解配置 MVC框架下的組件的支持。

Spring 2.5為MVC控制器引入了一種基于注解的編程模型,在其中使用諸如@RequestMapping 、@RequestParam 、@ModelAttribute ,等等。 這種注解支持在Servlet MVC和Portlet MVC中均可使用。通過這種方式實現的控制器不必由特定的基類繼承而來,或者實現特定的接口。 更進一步的,它們通常并不直接依賴于Servlet或Portlet API,雖然如果需要,它們可以方便的訪問Servlet或Portlet的功能。

提示

Spring發行版本附帶了PetClinic 示例,它是一個在簡單的表單處理的上下文中, 利用了本節中說明的注解支持的Web應用程序。 可以在“samples/petclinic ”目錄中找到PetClinic 應用程序。

另外一個建立在基于注解的Web MVC上的示例應用程序,請見imagedb 。 這個示例集中在無狀態的multi-action控制器,包括多段文件上傳的處理。 可以在“samples/imagedb ”目錄找到imagedb 應用程序。

下面的章節記錄了這些注解以及通常如何使用它們。

13.12.1.?建立dispatcher實現注解支持

只有對應的HandlerMapping (為了實現類型級別的注解)和/ 或HandlerAdapter (為了實現方法級別的注解)出現在 dispatcher中時, @RequestMapping 才會被處理。 這在DispatcherServlet 和DispatcherPortlet 中都是缺省的行為。

然而,如果是在定義自己的HandlerMappings 或HandlerAdapters , 就需要確保一個對應的自定義的DefaultAnnotationHandlerMapping 和 /或AnnotationMethodHandlerAdapter 同樣被定義——假設想要使用@RequestMapping 。

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean class="org.springframework.web.servlet.mvc.DefaultAnnotationHandlerMapping"/><bean class="org.springframework.web.servlet.mvc.AnnotationMethodHandlerAdapter"/>... (controller bean definitions) ...</beans>

如果你想要自定義映射策略,顯式的定義一個DefaultAnnotationHandlerMapping 和 /或AnnotationMethodHandlerAdapter 也有實際意義。 例如,指定一個自定義的PathMatcher 或者WebBindingInitializer (見下面)。

13.12.2.?使用@Controller 定義一個控制器

注解@Controller 指明一個特定的類承擔控制器 的職責, 而沒有擴展任何控制器基類或者引用Servlet API的必要。當然,如果需要還是可以引用特定Servlet功能。

注解@Controller 的基本目標是擔任所注解的類的原型的角色,指明它的職責。 Dispatcher將會在這樣被注解的類中掃描映射的方法,探測注解@RequestMapping (見下一節)。

所注解的控制器bean可以被顯式定義,這個過程是在dispatcher的上下文中使用一個標準的Spring bean定義完成的。 然而,@Controller 原型也允許自動探測,就像 Spring 2.5對探測組件的類以及為它們自動注冊bean定義的普遍支持一樣。

要實現對這樣的所注解的控制器的自動探測,必須要向配置中加入組件掃描的部分。 通過使用在下面的XML片段中所展示出的spring-context schema,這很容易實現:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="org.springframework.samples.petclinic.web"/>...</beans>

13.12.3.?使用@RequestMapping 映射請求

注解@RequestMapping 被用于映射如 “/editPet.do”這樣的URL到一個完整的類或者一個特定的處理方法。 典型的,頂層的注解映射一個特定的請求路徑(或者路徑模式)到一個表單控制器,另外的方法一級的注解可以縮小這個主要映射的范圍,包括對于一個特定的 HTTP請求方法(“GET/POST”)或者特定的HTTP請求參數。

提示

@RequestMapping 在類型一級也可以被用于Controller 接口的普通實現。 在這種情況下,請求處理的代碼會遵循傳統的handleRequest 模樣,而控制器的映射將會通過一個@RequestMapping 注解體現。 這對于預先構建的Controller 基類,諸如SimpleFormController ,也一樣有效。

在下面的討論中,我們將會關注基于通過注解實現的處理方法的控制器。

下面是一個使用了這種注解的表單控制器的例子,它選自PetClinic:

@Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm {private final Clinic clinic;@Autowiredpublic EditPetForm(Clinic clinic) {this.clinic = clinic;}@ModelAttribute("types")public Collection<PetType> populatePetTypes() {return this.clinic.getPetTypes();}@RequestMapping(method = RequestMethod.GET) public String setupForm(@RequestParam("petId") int petId, ModelMap model) {Pet pet = this.clinic.loadPet(petId);model.addAttribute("pet", pet);return "petForm";}@RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {new PetValidator().validate(pet, result);if (result.hasErrors()) {return "petForm";}else {this.clinic.storePet(pet);status.setComplete();return "redirect:owner.do?ownerId=" + pet.getOwner().getId();}}}

對于一個傳統的multi-action控制器,由于控制器會響應多個URL,URL就通常被直接映射到方法上。 下面是一個使用了@RequestMapping 的multi- action控制器的例子,它選自PetClinic:

@Controller public class ClinicController {private final Clinic clinic;@Autowiredpublic ClinicController(Clinic clinic) {this.clinic = clinic;}/*** Custom handler for the welcome view.* Note that this handler relies on the RequestToViewNameTranslator to* determine the logical view name based on the request URL: "/welcome.do"* -> "welcome".*/@RequestMapping("/welcome.do") public void welcomeHandler() {}/*** Custom handler for displaying vets.* Note that this handler returns a plain {@link ModelMap} object instead of* a ModelAndView, thus leveraging convention-based model attribute names.* It relies on the RequestToViewNameTranslator to determine the logical* view name based on the request URL: "/vets.do" -> "vets".** @return a ModelMap with the model attributes for the view*/@RequestMapping("/vets.do") public ModelMap vetsHandler() {return new ModelMap(this.clinic.getVets());}/*** Custom handler for displaying an owner.* Note that this handler returns a plain {@link ModelMap} object instead of* a ModelAndView, thus leveraging convention-based model attribute names.* It relies on the RequestToViewNameTranslator to determine the logical* view name based on the request URL: "/owner.do" -> "owner".** @param ownerId the ID of the owner to display* @return a ModelMap with the model attributes for the view*/@RequestMapping("/owner.do") public ModelMap ownerHandler(@RequestParam("ownerId") int ownerId) {return new ModelMap(this.clinic.loadOwner(ownerId));}}

使用@RequestMapping 注解的處理器方法允許具有非常靈活的外觀。 它們可以擁有下面類型的參數,在任意的順序下(除非是對于驗證結果,它需要緊跟在對應的命令對象后面,如果需要):

  • 請求和/或響應對象(Servlet API或者Portlet API)。 可以選擇任何特定的請求/響應類型,例如,ServletRequest/HttpServletRequest或者PortletRequest /ActionRequest/RenderRequest。 注意那個Portlet的例子里,一個被顯式聲明了的action/render參數被用于映射特定的請求類型到一個處理方法(在沒有提供其他信息來區分 action和render requests的情況下)。

  • 會話對象(Servlet API或者Portlet API):不管是HttpSession還是PortletSession。 一個此種類型的參數將會保證出現一個對應的會話。這樣就造成,這樣一個參數永遠也不可以是null 。 注意會話訪問可以并不是線程安全的,特別是在Servlet環境中:如果允許多個請求同時訪問一個會話,就考慮把AnnotationMethodHandlerAdapter 的 “synchronizeOnSession”旗標置為“true”

  • org.springframework.web.context.request.WebRequest 或org.springframework.web.context.request.NativeWebRequest 。 允許像訪問請求/會話屬性一樣的訪問一般的請求參數,而不是鎖定在原生的Servlet/Portlet API上。

  • java.util.Locale 用于當前請求區域屬性(由可用的最接近的區域屬性解析器決定,也就是, 在Servlet環境中配置好的LocaleResolver 以及在Portlet環境中的portal locale)。

  • java.io.InputStream /java.io.Reader 用于訪問請求的內容。 這將是Servlet/Portlet API暴露出的天然的InputStream/Reader。

  • java.io.OutputStream /java.io.Writer 用于生成響應的內容。 這將是Servlet/Portlet API暴露出的天然的OutputStream/Writer。

  • 以@RequestParam 注解的參數用于訪問特定的 Servlet/Portlet請求參數。 參數的值將被轉換為聲明的方法參數類型。

  • java.util.Map /org.springframework.ui.Model /org.springframework.ui.ModelMap 用于充實將被暴露到Web視圖的隱含模型。

  • 綁定參數到的命令/表單對象:帶有自定義的類型轉換的bean屬性或者域,依賴于@InitBinder 方法和/或HandlerAdapter配置——參見AnnotationMethodHandlerAdapter 的“webBindingInitializer ”屬性。 這樣的命令對象,包括它們的驗證結果,將會暴露為模型屬性,默認的會在屬性注解中使用非限定的命令類名(例如,對于類型 “mypackage.OrderAddress”使用“orderAddress”)。 為聲明一個特定的模型屬性名稱指定一個參數級別的ModelAttribute 注解。

  • org.springframework.validation.Errors /org.springframework.validation.BindingResult 驗證結果用于前面的一個命令/表單對象(前面緊接的參數)。

  • org.springframework.web.bind.support.SessionStatus 狀態處理用于把表單處理過程標記為已完成(觸發會話屬性的清理,這些會話屬性是在句柄類型級別由@SessionAttributes 注解指示出的)。

13.12.4.?使用@RequestParam 綁定請求參數到方法參數

@RequestParam 注解是用于在控制器中綁定請求參數到方法參數。

下面取自PetClinic實例程序的代碼片段說明了這種用法:

@Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm {// ... @RequestMapping(method = RequestMethod.GET)public String setupForm(@RequestParam("petId") int petId , ModelMap model) {Pet pet = this.clinic.loadPet(petId);model.addAttribute("pet", pet);return "petForm";}// ...

使用這個注解的參數默認是必需的,但是可以把@RequestParam 的required 屬性置為false 從而讓這個參數可選(例如,@RequestParam(value="id", required="false") )。

13.12.5.?使用@ModelAttribute 提供一個從模型到數據的鏈接

@ModelAttribute 在控制器中有兩種使用場景。 當作為一個方法參數時,@ModelAttribute 用于映射一個模型屬性到特定的注解的方法參數(見下面的processSubmit() 方法)。 這是控制器獲得持有表單數據的對象引用的方法。另外,這個參數也可以被聲明為特定類型的表單支持對象,而不是一般的java.lang.Object ,這就增加了類型安全性。

@ModelAttribute 也用于在方法級別為模型提供引用數據 (見下面的populatePetTypes() 方法)。 在這種用法中,方法編寫可以包含與上面描述的@RequestMapping 注解相同的類型。

注意: 使用@ModelAttribute 注解的方法將會在選定的使用@RequestMapping 注解的方法之前 執行。 它們有效的使用特定的屬性預先填充隱含的模型,這些屬性常常來自一個數據庫。 這樣一個屬性也就可以通過在選定的方法中使用@ModelAttribute 注解的句柄方法參數來訪問了,潛在的可以應用綁定和驗證。

下面的代碼片段展示了此注解的這兩種用法:

@Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm {// ... @ModelAttribute("types") public Collection<PetType> populatePetTypes() {return this.clinic.getPetTypes();}@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute("pet") Pet pet , BindingResult result,SessionStatus status) {new PetValidator().validate(pet, result);if (result.hasErrors()) {return "petForm";}else {this.clinic.storePet(pet);status.setComplete();return "redirect:owner.do?ownerId=" + pet.getOwner().getId();}}}

13.12.6.?使用@SessionAttributes 指定存儲在會話中的屬性

類型級別的@SessionAttributes 注解使用一個特定的句柄聲明會話屬性。 這通常會列出模型屬性的名稱,這些屬性應被透明的保存在會話或者對話存儲中,用于在后續的請求之間作為表單支持beans。

下面的代碼片段展示了此注解的這種用法:

@Controller @RequestMapping("/editPet.do") @SessionAttributes("pet") public class EditPetForm {// ... }

13.12.7.?自定義WebDataBinder 初始化

為了通過Spring的WebDataBinder 使用 PropertyEditors等自定義請求參數綁定,可以或者使用@InitBinder —— 在控制器之內的注解的方法, 或者通過提供一個定制的WebBindingInitializer 把配置具體化。

13.12.7.1.?使用@InitBinder 自定義數據綁定

使用@InitBinder 注解控制器方法,可以在控制器類內部直接配置 Web數據綁定。 @InitBinder 指定初始化WebDataBinder 的方法, 后者被用于填充注解的句柄方法的命令和表單對象參數。

這個init-binder方法支持@RequestMapping 支持的全部參數,除了命令/表單對象和對應的驗證結果對象。 Init-binder方法必須沒有返回值。因此,它們常被聲明為void 。 典型的參數,包括 WebDataBinder 以及WebRequest 或者java.util.Locale ,允許代碼注冊上下文特定的編輯器。

下面的例子說明了@InitBinder 的用法,為所有的java.util.Date 表單屬性配置一個CustomDateEditor 。

@Controller public class MyFormController {@InitBinder public void initBinder(WebDataBinder binder) {SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");dateFormat.setLenient(false);binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));}// ... }

13.12.7.2.?配置一個定制的WebBindingInitializer

為了外化數據綁定初始化的過程,可以提供一個WebBindingInitializer 接口的自定義實現。 通過為一個AnnotationMethodHandlerAdapter 提供一個定制的bean配置可以使它啟用,這樣就覆蓋了默認配置。

下面取自PetClinic應用的例子展示了一個使用WebBindingInitializer 接口的自定義實現的配置——org.springframework.samples.petclinic.web.ClinicBindingInitializer , 完成多個PetClinic控制器都需要的PropertyEditors的配置。

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="cacheSeconds" value="0" /><property name="webBindingInitializer"><bean class="org.springframework.samples.petclinic.web.ClinicBindingInitializer" /></property> </bean> ================ http://blog.csdn.net/sanshiqiduer/article/details/4791779

基于注解風格的Spring-MVC的攔截器

分類: Java 4753人閱讀 評論(3) 收藏 舉報

轉自? http://www.blogjava.net/atealxt/archive/2009/09/20/spring_mvc_annotation_interceptor_gae.html

?

Spring-MVC如何使用攔截器,官方文檔只給出了非注解風格的例子。那么基于注解風格如何使用攔截器呢?基于注解基本上有2個可使用的定義類,分別是DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter

Xml代碼
  • < bean ? class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> ??
  • < bean ? class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> ??
  • [xml] view plaincopyprint?
  • <bean?class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>?<bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>??
  • <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    ?

    1、DefaultAnnotationHandlerMapping
    DefaultAnnotationHandlerMapping本身支持自定義攔截器,只需按如下進行配置:

    Xml代碼
  • < bean ? class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" > ??
  • ?????< property ? name = "interceptors" > ??
  • ?????????< list > ??
  • ????????????????< bean ? class = "packageName.XXXInterceptor" ? /> ??
  • ?????????</ list > ??
  • ?????</ property > ??
  • ?</ bean > ??
  • [xml] view plaincopyprint?
  • <bean?class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">?<property?name="interceptors">?<list>?<bean?class="packageName.XXXInterceptor"?/>?</list>?</property>?</bean>??
  • <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <bean class="packageName.XXXInterceptor" /> </list> </property> </bean>

    ?

    Interceptor的定義為:

    Java代碼
  • public ? class ?XXXInterceptor? extends ?HandlerInterceptorAdapter?{??
  • ??????@Override ??
  • ??????public ? boolean ?preHandle(HttpServletRequest?req,?HttpServletResponse?resp,?Object?handler)?{??
  • ?????
  • ??????????String?className?=?handler.getClass().getName();//?packageName.ClassName ??
  • ??????????if ?(Error)?{??
  • ??????????????return ? false ;??
  • ??????????}??
  • ??????????return ? true ;??
  • ?????}??
  • ?}??
  • [java] view plaincopyprint?
  • public?class?XXXInterceptor?extends?HandlerInterceptorAdapter?{?@Override?public?boolean?preHandle(HttpServletRequest?req,?HttpServletResponse?resp,?Object?handler)?{?String?className?=?handler.getClass().getName();//?packageName.ClassName?if?(Error)?{?return?false;?}?return?true;?}?}??
  • public class XXXInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) { String className = handler.getClass().getName();// packageName.ClassName if (Error) { return false; } return true; } }

    ?

    2、AnnotationMethodHandlerAdapter
    目前,筆者沒找到如何給AnnotationMethodHandlerAdapter配置自定義Interceptor的方法,但是有個customArgumentResolver可以利用一下,來充當Interceptor。

    Xml代碼
  • < bean ? class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > ??
  • ?????< property ? name = "customArgumentResolver" > ??
  • ?????????< bean ? class = "packageName.XXXResolver" /> ??
  • ?????</ property > ??
  • ?</ bean > ??
  • [xml] view plaincopyprint?
  • <bean?class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">?<property?name="customArgumentResolver">?<bean?class="packageName.XXXResolver"/>?</property>?</bean>??
  • <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="customArgumentResolver"> <bean class="packageName.XXXResolver"/> </property> </bean>

    ?

    Resolver的定義為:

    Java代碼
  • public ? class ?XXXResolver? implements ?WebArgumentResolver?{??
  • ?????
  • ??????@Override ??
  • ??????public ?Object?resolveArgument(MethodParameter?methodParameter,?NativeWebRequest?webRequest)? throws ?Exception?{??
  • ?????
  • ??????????String?className?=?methodParameter.getMethod().getDeclaringClass().getName();//?packageName.ClassName ??
  • ????
  • ??????????//?如何取得Response和Request ??
  • ??????????HttpServletResponse?resp?=?(HttpServletResponse)?webRequest.getNativeResponse();??
  • ????????HttpServletRequest?req?=?(HttpServletRequest)?webRequest.getNativeRequest();??
  • ????
  • ????????if ?(Error)?{??
  • ????????????if ?(!resp.isCommitted())?resp.sendError(ERROR_STATUS);??
  • ????????}??
  • ????????return ?UNRESOLVED;??????
  • ?????}??
  • ?}??
  • [java] view plaincopyprint?
  • public?class?XXXResolver?implements?WebArgumentResolver?{?@Override?public?Object?resolveArgument(MethodParameter?methodParameter,?NativeWebRequest?webRequest)?throws?Exception?{?String?className?=?methodParameter.getMethod().getDeclaringClass().getName();//?packageName.ClassName?//?如何取得Response和Request?HttpServletResponse?resp?=?(HttpServletResponse)?webRequest.getNativeResponse();?HttpServletRequest?req?=?(HttpServletRequest)?webRequest.getNativeRequest();?if?(Error)?{?if?(!resp.isCommitted())?resp.sendError(ERROR_STATUS);?}?return?UNRESOLVED;?}?}??
  • public class XXXResolver implements WebArgumentResolver { @Override public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception { String className = methodParameter.getMethod().getDeclaringClass().getName();// packageName.ClassName // 如何取得Response和Request HttpServletResponse resp = (HttpServletResponse) webRequest.getNativeResponse(); HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest(); if (Error) { if (!resp.isCommitted()) resp.sendError(ERROR_STATUS); } return UNRESOLVED; } }

    ?

    仔細的人會看出,第二種方法其實根本不是攔截。其實第二種只是在映射Controller,調用方法的時候,給每一個方法的參數增加了一個切點。
    上例在出錯的時候往HttpServletResponse 寫錯誤狀態,來通知web容器進行錯誤重定向,達到了攔截器的作用。
    這么做有一個缺點,就是每個參數都有自己的切點,比如方法有3個參數就會調3次resolveArgument。為了避免出錯,需要判斷一下 resp.isCommitted 。

    customArgumentResolver的初衷不是用來做Interceptor的,但有些環境卻不得不使用它,比如部署在GAE上。
    GAE 是不支持DefaultAnnotationHandlerMapping的,因為此類用到了 org.springframework.beans.BeanUtils.findEditorByConvention,這個方法會調用 java.lang.ClassLoader.getSystemClassLoader,而這正是GAE所不允許的。

    PS:
    文中提到軟件的版本:
    spring - 2.5.X
    google app engine - 1.2.5

    參考資料
    http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors
    http://groups.google.com/group/google-appengine-java/browse_thread/thread/54aadf8164be7bf0

    總結

    以上是生活随笔為你收集整理的spring 基于注解的控制器配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。