1 回答

TA貢獻1836條經(jīng)驗 獲得超4個贊
首先,你需要在一個Controller中往session中存內(nèi)容(取的名字必須含有大寫字母,雖然博主也不清楚為什么,如果有知道的人請一定要通知博主,博主將感激不盡):
[java] view plain copy
@Controller
@SessionAttributes("Save") //①將ModelMap中屬性名為currUser的屬性
//放到Session屬性列表中,以便這個屬性可以跨請求訪問
public class Test1Controller {
@RequestMapping("save")
public String save(Integer save,ModelMap model) {
model.addAttribute("Save",save); //②向ModelMap中添加一個屬性
return "save";
}
}
這個save數(shù)字將自動存儲在session中,name為"Save"。
如果是要在同一個controller中調(diào)用,可以這么做:
[java] view plain copy
@Controller
@SessionAttributes("Save") //①將ModelMap中屬性名為currUser的屬性
//放到Session屬性列表中,以便這個屬性可以跨請求訪問
public class <span style="font-family: Arial, Helvetica, sans-serif;">Test1Controller </span>{
@RequestMapping("save")
public String save(Integer save,ModelMap model) {
model.addAttribute("Save",save); //②向ModelMap中添加一個屬性
return "save";
}
@RequestMapping("get")
public String get(@ModelAttribute("Save") Integer save,ModelMap model) {
System.out.println("save:"+save); //modelMap中的Save將自動綁定在save上
return "get";
}
}
我們即可在get中獲得了保存在session中的Save對象
如果是要在其他controller中調(diào)用,可以這么做
[java] view plain copy
@Controller
@SessionAttributes("Save") //①將ModelMap中屬性名為currUser的屬性
//放到Session屬性列表中,以便這個屬性可以跨請求訪問
public class <span style="font-family: Arial, Helvetica, sans-serif;">Test2Controller </span>{
@RequestMapping("get")
public String get(@ModelAttribute("Save") Integer save,ModelMap model) {
System.out.println("save:"+save); //modelMap中的Save將自動綁定在save上
return "get";
}
}
方式相同,但需記住,即使你不使用modelMap,仍需注入該對象,方法中仍需保留該參數(shù),切記??!
- 1 回答
- 0 關(guān)注
- 1543 瀏覽
添加回答
舉報