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>