spring MVC之註解開發控制器 三

2021-08-31 15:16:17 字數 2809 閱讀 9808

spring mvc之註解開發控制器(三)

開發表單控制器

在傳統的spring mvc開發方法中,是通過擴充套件******formcontroller類來建立簡單的表單控制器。這樣就定義了基本的表單處理流程,並允許通過覆蓋幾個生命週期方法來定製流程。在基於註解的spring mvc開發方法中,可以利用註解模擬表單處理流程.

package com.apress.springrecipes.court.web;

...import org.springframework.beans.factory.annotation.autowired;

...@controller

@sessionattributes("reservation")

public class reservationformcontroller

@initbinder

public void initbinder(webdatabinder binder)

@modelattribute("sporttypes")

public listpopulatesporttypes()

public string setupform(@requestparam(required=false,value="username")

string username,modelmap model)

public string processsubmit(@modelattribute("reservation")reservation reservation,

bindingresult result,sessionstatus status)else

}}

與http get方法相關聯的setupform()方法,對應於******formcontroller的formbackingobject()方法,它將初始化用於繫結的命令物件。在這個方法中,為了初始化命令物件,可以利用@requestparam註解獲取任何請求引數。然後,你親自建立命令物件,並將它儲存到模型屬性中。模型屬性的名稱實際上是可以在jsp檔案中訪問到的命令物件名稱。如果想要將命令物件儲存在會話中,就像啟用******formcontroller的sessionform屬性一樣,可以將@sessionattributes註解應用到控制器類中,並指定要儲存在會話中的模型屬性的名稱。

與http post方法相關聯的processsubmit()方法,對應於******formcontroller的onsubmit()方法。在這個方法中,可以利用@modelattribute註解,從模型中得到命令物件。在被應用到方法引數時,@modelattribute註解會將模型

屬性繫結到方法引數上。與******formcontroller的onsubmit()方法不同,你必須親自執行表單驗證,並決定是呈現

表單檢視琮是成功檢視。表單提交處理成功之後,呼叫sessionstatus的setcomplete()方法,從會話中清除命令物件.當被應用到像populatesporttypes()這樣的方法中時,@modelattribute註解會將引用資料填充到模型中。這樣做的效果與覆蓋******formcontroller的referencedata()方法相同。

新增了@initbinder註解的initbinder()方法將定製的屬性編輯器註冊到了繫結器物件中。它與******formcontroller

的ininbinder()方法相對應。

請注意,這個控制器的成功檢視是乙個重定向到預訂成功頁面的檢視。你可以為該檢視建立另乙個基於註解的控制器。 

package com.apress.springrecipes.court.web;

import org.springframework.stereotype.controller;

@controller

public class reservationsuccesscontroller

}

最後,在這個表單控制器生效之前,必須定義一具驗證器例項,讓spring進行自動裝配。

為了重用,可以從每個控制器類中提取繫結器初始化任務,形成繫結初始器。

package com.apress.springrecipes.court.web;

...import org.springframework.beans.factory.annotation.autowired;

import org.springframework.beans.propertyeditors.customdateeditor;

import org.springframework.web.bind.webdatabinder;

import org.springframework.web.bind.support.webbindinginitializer;

import org.springframework.web.context.request.webrequest;

public class reservationbindinginitializer implements webbindinginitializer

public void initbinder(webdatabinder binder,webrequest request)

}

然後為annotationmethodhandleradapter指定這個繫結初始器,以便所有的處理程式方法都可以共用相同的屬性編輯器。  

現在可以刪除reservationformcontroller中的initbinder()方法,因為繫結喊叫已經能在每個基於註解的控制器中共享了。 

springmvc註解開發

1.配置dispatcherservlet 2.在springmvc中配置三大元件 3.在spring容器中配置action 使用 controller 與此同型別的還有 service responsitory component 使用spring容器的元件掃瞄,自動掃瞄到action在sprin...

SpringMVC 註解開發和RESTful風格

在 springmvc 實際開發中,我們通常都會採用註解開發。web.xml 註冊 dispatcherservlet xmlns xsi xsi schemalocation version 4.0 springmvc org.springframework.web.servlet.dispatc...

Spring MVC註解開發及其執行流程

注釋後端控制器 spring和spring mvc整合 執行流程 配置listener 具體作用在執行流程說明。配置servlet標籤 包含主配檔案的位置和名稱,以及處理請求的型別。啟動註解 設定註解作用域 ps component scan預設掃瞄 controller service repos...