Spring MVC学習ノート7つのcontrollerの他の利用可能なannotation


@InitBinder
  controllerにCstomer protperty editorを登録して、requestにおけるパラメータを解析し、date bind機構を通じてhandler methodにおけるパラメータと結合します。
 
    @InitBinder
    public void initBinder(WebDataBinder binder) {
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
       dateFormat.setLenient(false);
       binder.registerCustomEditor(Date.class, new CustomDateEditor(
              dateFormat, false));
    }
Handler methodコードは以下の通りです。

   @RequestMapping("/databind1")
    public ModelAndView databind1(Date date) {
      …   
   }
訪問コースhttp://localhost:8080/springmvc/databind1.action?date=2000-01-02
initbinderに登録されたcustome DateEditorタイプにより、自動的に2000-01-02を日付タイプに変換します。
@ResonseStatus
指定されたhttp reponse状態コードを返します。
例えば
    
    @ResponseStatus(reason="no reason",value=HttpStatus.BAD_REQUEST)
    @RequestMapping("/responsestatus")
    public void responseStatusTest(){
      
    }
Http error 400を返します。
@Session Attributes
  以前のrequiresSession属性に相当し、この属性を設定すると、handler methodに対応するパラメータがsessionから取得され、存在しない場合はSession RequiredExceptionを抛り出し、前に述べたSession Station.setComple()と一緒に使用し、言及が成功したらリフレッシュボタンを押して繰り返し提出することを避けることができます。
@Exception Handler
  
   @RequestMapping("/exception")
    public void ExceptionTest() throws Exception{
       throw new Exception("i don't know");
    }  
    @ExceptionHandler
    public String handleException(Exception e,HttpServletRequest request){
       System.out.println(e.getMessage());
       return "helloworld";
    }
その中/exceptionは一つの異常を投げましたが、handleExceptionはこの異常を捕まえて処理します。