controller

controller level

method-level

package spittr.web;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller 
public class HomeController {
  @RequestMapping(value="/", method=GET)
  public String home() {
    return "home"; 
  }
}

class-level

@Controller
@RequestMapping("/")  //@RequestMapping({"/", "/homepage"})
public class HomeController {
  @RequestMapping(method=GET)
  public String home() {
    return "home";
    } 
}

passing model data to the view

  • 數(shù)據(jù)流: controller --> view resolver --> view
  • 數(shù)據(jù)的類型: Model宾符,本質(zhì)上是一個(gè)map
  • controller函數(shù)中始終返回的是view 名稱匠抗,Model 數(shù)據(jù)的傳遞由框架來(lái)實(shí)現(xiàn)
  • 在前端的jsp中,可以通過(guò)key來(lái)訪問(wèn)數(shù)據(jù)

傳遞模型數(shù)據(jù)的幾種形式

返回view name隙咸, 數(shù)據(jù)保存在model中

  • 返回的view名稱"spittles", 傳遞的數(shù)據(jù)保存在Model(map)中
  • Model 不明確指定key,默認(rèn)自動(dòng)推導(dǎo)可以為"spittleList"(List<Spittle> 推導(dǎo)而來(lái))
@Controller
@RequestMapping("/spittles")
public class SpittleController {
    private SpittleRepository spittleRepository;
    @Autowired
    public SpittleController(
    SpittleRepository spittleRepository) {
        this.spittleRepository = spittleRepository;
    }
    @RequestMapping(method=RequestMethod.GET)
    public String spittles(Model model) {
      model.addAttribute(spittleRepository.findSpittles(Long.MAX_VALUE, 20));//model.addAttribute("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
      return "spittles";
    } 
}

返回view name, 數(shù)據(jù)使用java.util.Map保存

@RequestMapping(method=RequestMethod.GET)
public String spittles(Map model) {
  model.put("spittleList",
          spittleRepository.findSpittles(Long.MAX_VALUE, 20));
  return "spittles";
}

返回?cái)?shù)據(jù)成洗,view name讓框架自動(dòng)推導(dǎo)(最好別這么做)

@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles() {
  return spittleRepository.findSpittles(Long.MAX_VALUE, 20));
}
  • 返回的數(shù)據(jù)會(huì)保存到Model中五督,Model key也是自動(dòng)推導(dǎo)
  • view name 根據(jù)request path推到而來(lái), 比例:http://xxx/spittles, view name就是spittles

request input

獲取參數(shù)

@RequestMapping(method=RequestMethod.GET)
public List<Spittle> spittles(
  @RequestParam(value="max", defaultValue=MAX_LONG_AS_STRING) long max,
  @RequestParam(value="count", defaultValue="20") int count) {
  return spittleRepository.findSpittles(max, count);
}

resource-orientation 資源化的url

  @RequestMapping(value="/{spittleId}", method=RequestMethod.GET)
  public String spittle(
      @PathVariable("spittleId") long spittleId, 
      Model model) {
    model.addAttribute(spittleRepository.findOne(spittleId));
    return "spittle";
  }
  • @PathVariable 可以省略參數(shù)瓶殃,默認(rèn)和函數(shù)的參數(shù)相同充包, 即spittleId (long spittleId)

processing forms

form controller

@RequestMapping(value="/register", method=POST)
public String processRegistration(Spitter spitter) {
    spitterRepository.save(spitter);
    return "redirect:/spitter/" +
           spitter.getUsername();
}

validating forms

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class SpittleForm {

  @NotNull
  @Size(min=1, max=140)
  private String message;
  
  @Min(-180)
  @Max(180)
  private Double longitude;
  
  @Min(-90)
  @Max(90)
  private Double latitude;
  .....
}
  • controller 對(duì)應(yīng)的處理函數(shù)中必須增加一個(gè)org.springframework.validation.Errors 參數(shù)
  @RequestMapping(value="/register", method=POST)
  public String processRegistration(
      @Valid Spitter spitter, 
      Errors errors) {
    if (errors.hasErrors()) {
      return "registerForm";
    }
    
    spitterRepository.save(spitter);
    return "redirect:/spitter/" + spitter.getUsername();
  }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子基矮,更是在濱河造成了極大的恐慌淆储,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件家浇,死亡現(xiàn)場(chǎng)離奇詭異本砰,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)钢悲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門点额,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人譬巫,你說(shuō)我怎么就攤上這事咖楣。” “怎么了芦昔?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵诱贿,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我咕缎,道長(zhǎng)珠十,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任凭豪,我火速辦了婚禮焙蹭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嫂伞。我一直安慰自己孔厉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布帖努。 她就那樣靜靜地躺著撰豺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪拼余。 梳的紋絲不亂的頭發(fā)上污桦,一...
    開(kāi)封第一講書(shū)人閱讀 51,258評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音匙监,去河邊找鬼凡橱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛亭姥,可吹牛的內(nèi)容都是我干的稼钩。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼达罗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼变抽!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起氮块,我...
    開(kāi)封第一講書(shū)人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤绍载,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后滔蝉,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體击儡,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年蝠引,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了阳谍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡螃概,死狀恐怖矫夯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吊洼,我是刑警寧澤训貌,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站冒窍,受9級(jí)特大地震影響递沪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜综液,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一款慨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谬莹,春花似錦檩奠、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至士葫,卻和暖如春乞而,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慢显。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工爪模, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人荚藻。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓屋灌,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親应狱。 傳聞我的和親對(duì)象是個(gè)殘疾皇子共郭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

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