SpringMvc-傳值過程
1.ModelMap
2.Model
3.ModelAndView
4.@SessionAttributes註解
5.@SessionAttribute註解
6.@ModelAttributed
代碼示例:
前端jsp:
<%--使用modelAndView這種方式來傳遞參數--%>
<a href="${pageContext.request.contextPath}/testModelandview">測試testModelAndView</a>
<hr>
<br>
<%--使用model這種方式來傳遞參數--%>
<a href="${pageContext.request.contextPath}/testModel">測試testModel</a>
<hr>
<br>
<%--使用map集合來傳遞參數--%>
<a href="${pageContext.request.contextPath}/testMap">測試testMap</a>
<hr>
<br>
<%--@SessionAttributes--%>
<a href="${pageContext.request.contextPath}/testSession">測試testSession</a>
<hr>
<br>
<%--@SessionAttribute--%>
<a href="${pageContext.request.contextPath}/testSession2">測試testSession2</a>
<hr>
<br>
<%--@ModelAttribute--%>
<form action="${pageContext.request.contextPath}/testModelAttribute">
名稱:<input type="text" name="name"><br>
價格:<input type="text" nmae="price"><br>
<input type="submit" value="提交">
</form>
前端控制器:
package com.helong.web.controller;
import com.helong.domain.Goods;
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.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Controller
@SessionAttributes(value={"name","name2"})//將model裏面key爲name和name2屬性的這個值存放到session裏面去
//@SessionAttributes(types = String.class)//將類中所有的String類型的key值全部存放到session域當中
public class MyController {
/*使用ModelAndView方式來傳遞參數*/
@RequestMapping("testModelandview")
public ModelAndView show(){
ModelAndView modelAndView = new ModelAndView();
//把數據寫到request域當中
modelAndView.addObject("name","helong");
modelAndView.setViewName("/result.jsp");
return modelAndView;
}
/*使用Model來向頁面傳遞參數*/
@RequestMapping("testModel")
public String testModel(Model model){
//把數據寫到request域當中
/*model方法*/
model.addAttribute("name","helong");
/*model.asMap() 將值自動轉換爲Map*/
System.out.println(model.asMap());
/*向model中添加對象 將對象轉化爲Map對象 並且把類名的首字母小寫當做了Map的key值了(以屬性的類型首字母小寫爲key)*/
Goods goods1 = new Goods();
goods1.setName("T恤");
goods1.setPrice("35");
model.addAttribute(goods1);
System.out.println(model.asMap());
/*addAllAttributes(Map)
* 將Map中的內容複製到當前的model中
* 如果當前model存在相同key,會被覆蓋
* */
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("name","helllllll");
hashMap.put("hot",1000);
model.addAllAttributes(hashMap);
System.out.println(model.asMap());
/*
*以集合中的類型爲key
* 將集合當中元素的類作爲key加入到map當中
* 如果有同類型會存在覆蓋現象
* */
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add("hexinlin");
arrayList.add(100);
arrayList.add("hhhh");//這個會覆蓋上面的字符串類型
model.addAllAttributes(arrayList);
System.out.println(model.asMap());
/*containsAttribute這個方法判斷model中是否含有這個字段的屬性*/
System.out.println(model.containsAttribute("name"));
return "/result.jsp";
}
/*使用Map來傳遞參數*/
@RequestMapping("testMap")
public String testMap(Map map){
/*使用map將值存放到request域*/
map.put("key1","value1");
map.put("key2","value2");
return "/result.jsp";
}
/*
* @SessionAttributes註解
* 將模型中的某個屬性站暫存到HttpSession中,以便多個請求之間可以共享這個屬性
*value:@SessionAttributes(value={"name1","name2"})將類裏面屬性爲name1和name2的數據寫到session域當中去
* type:@SessionAttributes(type = String.class) 將類裏面屬性類型爲String的數據寫到session域當中去
* */
@RequestMapping("testSession")
public String testSession(Model model){
//將數據存到request域當中
model.addAttribute("name","hems");
model.addAttribute("name2","hems");
return "result2.jsp";
}
/*
* @SessionAttribute註解
* @SessionAttribute("name") String name 將session域當中屬性名爲name的值取出
* 如果域當中沒有這個屬性的話,那麼就會報錯
* */
@RequestMapping("testSession2")
public String testSession2(@SessionAttribute("name") String name){
System.out.println(name);
return "result3.jsp";
}
/*
* @ModelAttribute
* 會自動將創建的模型放到model中
* 指定存放到Map中的key的名稱
* */
@RequestMapping("testModelAttribute")
public String testModelAttribute(@ModelAttribute("hlgoods") Goods goods, Model model){
System.out.println(goods);
System.out.println(model.asMap());
return "result3.jsp";
}
//此方法就會在映射方法(requestMapping)執行之前執行這個方法,這個方法會在前面這個方法執行之前自動調用
//並且會把model提前傳到對應的方法之中
@ModelAttribute
public void testModelAttrbute(Model model){
System.out.println("testModelAttrbute執行了");
model.addAttribute("name","hhhhhhhh1");
}
}
接收參數界面:
<%--從request域當中獲取數據的方法--%>
<h2>來了老弟---${requestScope.name}</h2>
<h2>來了老弟---${name}</h2>
<%--接收 model.addAttribute(goods1)存放的Map對象--%>
<h2>goodsName=${goods.name}</h2>
<h2>goodsPrice=${goods.price}</h2>
<%--接收Map中傳遞的數據--%>
<h2>Map----key1=${key1}</h2>
<h2>Map----key2=${key2}</h2>
<%--@SessionAttributes註解的使用--%>
<h1>request=${requestScope.name}</h1>
<h1>session=${sessionScope.name}</h1>
<h1>session=${sessionScope.name2}</h1>
<hr>
<br>
<h1>result3---name=${sessionScope.name}</h1>