springmvcでモデルデータを処理する
springmvc処理モデルデータ
多くの場合、ページ上で多くのデータが必要で、単にページに戻るだけではだめです。どのようにこのページに戻りますか?
springmvcは4つの方法でモデルデータを出力します。 ModelAndView:処理の戻り値がModelAndViewの場合、このオブジェクトにデータモデル を追加することができます。 MapおよびModel:Model、ModelMapまたはMapに参加すると、処理方法が戻ってきたら、Mapのデータが自動的にモデルに追加されます。 @Session Attributes:モデルのある属性をHttpSessionに一時保存して、複数の要求間でデータを共有するために。 @ModelAttribute:メソッドはこの注釈を参照してください。パラメータの対象はデータモデルに入れられます。
ModelAndView
主に2つの重要な変数があります。
例:
クラスに@Session Attributesを追加すると、そのクラスが代表する経路下のsessionを共有することができます。
戻り値のない方法で
多くの場合、ページ上で多くのデータが必要で、単にページに戻るだけではだめです。どのようにこのページに戻りますか?
springmvcは4つの方法でモデルデータを出力します。
主に2つの重要な変数があります。
// ( ) View
private Object view;
// map
private ModelMap model;
表示に関する方法
//
public void setViewName(String viewName) {
this.view = viewName;
}
//
public String getViewName() {
return this.view instanceof String ? (String)this.view : null;
}
データモデル関連方法
//
protected Map<String, Object> getModelInternal() {
return this.model;
}
public ModelMap getModelMap() {
if (this.model == null) {
this.model = new ModelMap();
}
return this.model;
}
public Map<String, Object> getModel() {
return this.getModelMap();
}
//
public ModelAndView addObject(String attributeName, Object attributeValue) {
this.getModelMap().addAttribute(attributeName, attributeValue);
return this;
}
springmvcの最下層はrequest.set Attributeを使ってデータを要求に入れます。例:
@RequestMapping("/modelAndViewTest")
public ModelAndView modelAndViewTest(){
//
ModelAndView modelAndView = new ModelAndView("modelAndViewTest");
//
modelAndView.addObject("dateTime",new Date());
return modelAndView;
}
Map及びModel
@RequestMapping("/mapTest")
public String mapTest(Map<String,String> map){
System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap
map.put("name"," ");
return "hello";
}
@Session Attributesクラスに@Session Attributesを追加すると、そのクラスが代表する経路下のsessionを共有することができます。
@Controller
@RequestMapping("helloWorld")
// name
@SessionAttributes(value={"name"})
public class HelloWorldController {
@RequestMapping("/mapTest")
public String mapTest(Map<String,String> map){
System.out.println(map.getClass()); //class org.springframework.validation.support.BindingAwareModelMap
map.put("name"," ");
return "hello";
}
// name
@RequestMapping("/sessionAttributes")
public String sessionAttributes(HttpSession session){
System.out.println(session.getAttribute("name"));
return "hello";
}
}
@ModelAttribute戻り値のない方法で
package com.yiidian.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ModelAttributeController {
//
@ModelAttribute
public void myModel(@RequestParam(required = false) String name, Model model) {
model.addAttribute("name", name);
}
@RequestMapping(value = "/model")
public String model() {
return "success";
}
}
戻り値を持つ方法で
package com.yiidian.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ModelAttributeController {
/**
*
* @param name
*/
@ModelAttribute("name")
public String myModel(@RequestParam(required = false) String name) {
return name;
}
@RequestMapping(value = "/model")
public String model() {
return "success";
}
}
方法のパラメータに適用します。
@ModelAttribute("name")
public String myModel(@RequestParam(required = false) String name) {
return name;
}
//
@RequestMapping(value = "/model")
public String model(@ModelAttribute("name") String name) {
System.out.println("name="+name);
return "success";
}
以上はspringmvcを利用してモデルデータの詳細を処理します。springmvc処理モデルデータに関する資料は他の関連記事に注目してください。