Spring MVC MultiActionController example

In Spring MVC application, MultiActionController is used to group related actions into a single controller, the method handler have to follow below signature :

public (ModelAndView | Map | String | void) actionName(     HttpServletRequest, HttpServletResponse [,HttpSession] [,CommandObject]);

1. MultiActionController

See a MultiActionController example.
package com.mkyong.common.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class CustomerController extends MultiActionController{
public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("CustomerPage", "msg","add() method");
}

public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) throws Exception {                  
    return new ModelAndView("CustomerPage", "msg","delete() method");                       
}       

public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {                  
    return new ModelAndView("CustomerPage", "msg","update() method");                     
}       

public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception {                        
    return new ModelAndView("CustomerPage", "msg","list() method");                       
}   

}

With **`ControllerClassNameHandlerMapping`** configured.

<beans ...>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="com.mkyong.common.controller.CustomerController" />
</beans>

##2. Mapping Examples
Now, the reuqested URL will map to the method name in the following patterns :

CustomerController –> /customer/*
/customer/add.htm –> add()
/customer/delete.htm –> delete()
/customer/update.htm –> update()
/customer/list.htm –> list()

##3. InternalPathMethodNameResolver
The `InternalPathMethodNameResolver` is the default **`MultiActionController`** implementation to map URL to method name. But, you are still allow to add prefix or suffix to the method name :

<beans ...>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean class="com.mkyong.common.controller.CustomerController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
<property name="prefix" value="test" />
<property name="suffix" value="Customer" />
</bean>
</property>
</bean>
</beans>

Now, the URL will map to the method name in the following pattern :

CustomerController –> /customer/*
/customer/add.htm –> testaddCustomer()
/customer/delete.htm –> testdeleteCustomer()
/customer/update.htm –> testupdateCustomer()
/customer/list.htm –> testlistCustomer()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末丙猬,一起剝皮案震驚了整個濱河市嫁怀,隨后出現(xiàn)的幾起案子刃唐,更是在濱河造成了極大的恐慌,老刑警劉巖减宣,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異玩荠,居然都是意外死亡漆腌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進(jìn)店門阶冈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闷尿,“玉大人,你說我怎么就攤上這事女坑√罹撸” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵匆骗,是天一觀的道長劳景。 經(jīng)常有香客問我,道長碉就,這世上最難降的妖魔是什么盟广? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮瓮钥,結(jié)果婚禮上筋量,老公的妹妹穿的比我還像新娘烹吵。我一直安慰自己,他們只是感情好桨武,可當(dāng)我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布肋拔。 她就那樣靜靜地躺著,像睡著了一般呀酸。 火紅的嫁衣襯著肌膚如雪凉蜂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天七咧,我揣著相機(jī)與錄音跃惫,去河邊找鬼。 笑死艾栋,一個胖子當(dāng)著我的面吹牛爆存,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蝗砾,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼先较,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了悼粮?” 一聲冷哼從身側(cè)響起闲勺,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎扣猫,沒想到半個月后菜循,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡申尤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年癌幕,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片昧穿。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡勺远,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出时鸵,到底是詐尸還是另有隱情胶逢,我是刑警寧澤,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布饰潜,位于F島的核電站初坠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏囊拜。R本人自食惡果不足惜某筐,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冠跷。 院中可真熱鬧南誊,春花似錦身诺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至幔托,卻和暖如春穴亏,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背重挑。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工嗓化, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谬哀。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓刺覆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親史煎。 傳聞我的和親對象是個殘疾皇子谦屑,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法篇梭,內(nèi)部類的語法氢橙,繼承相關(guān)的語法,異常的語法恬偷,線程的語...
    子非魚_t_閱讀 31,625評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 3,810評論 0 11
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理悍手,服務(wù)發(fā)現(xiàn),斷路器袍患,智...
    卡卡羅2017閱讀 134,652評論 18 139
  • 從三月份找實習(xí)到現(xiàn)在谓苟,面了一些公司,掛了不少协怒,但最終還是拿到小米孕暇、百度妖滔、阿里、京東桶良、新浪座舍、CVTE、樂視家的研發(fā)崗...
    時芥藍(lán)閱讀 42,243評論 11 349
  • 一套完整的登陸注冊業(yè)務(wù)邏輯 準(zhǔn)備部分基礎(chǔ)工具類Basepackage com.jericho.tools;impo...
    JerichoPH閱讀 2,442評論 0 9