SpringMVC默认的参数类型
SpringMVC底层是Servlet
HttpServletRequest、HttpServletResponse、HttpSession、Model、Map、ModelMap
注意:Model 、Map、ModelMap、request一样,都使用请求作用域进行数据传递。必须使用请求转发
如有错误请指正,谢谢
通过作用域域传递数据
@Controller
public class DataForward {
@RequestMapping("/data")
public String dataF(HttpServletRequest request,
HttpServletResponse response,
HttpSession session,
Map map,
Model model,
ModelMap modelMap
){
User u1 =new User(01,"张三request");
User u2 =new User(01,"张三session");
User u3 =new User(01,"张三model");
User u4 =new User(01,"张三map");
User u5 =new User(01,"张三map");
//向作用域存储数据
request.setAttribute("user1",u1);
session.setAttribute("user2",u2);
model.addAttribute("user3",u3);
map.put("user4",u4);
modelMap.addAttribute("user5",u5);
return "dataPage";
}
}
dataPage.jsp
<%--//向作用域提取数据--%>
request:${user1.name}<br>
session:${user2.name}<br>
model:${user3.name}<br>
map:${user4.name}<br>
modelMap:${user5.name}<br>
Index.jsp -传递数据-> dataPage.jsp
index.jsp页面
<a href="${pageContext.request.contextPath}/data.action?name=老六">data</a>
dataPage.jsp页面
来自index.jsp页面的数据: ${param.name}
forEach标签案例
Servlet将集合放入request域
StaffDao mapper = sqlSessionAuto.getMapper(StaffDao.class);
List<Staff> staffs = mapper.selectALL();
//将查询结果放入请求域中
request.setAttribute("staffs",staffs);
Jsp页面
Foreach标签使用案例
<%--引入核心标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${staffs}" var="s">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
<td>${s.diploma}</td>
<td>${s.title}</td>
<td>${s.marriage}</td>
<td>${s.status}</td>
<td>${s.workTime}</td>
<td>
<a href="javascript:void(0)" onclick="delect(${s.id})">删除</a>
<a href="${pageContext.request.contextPath}/os/modify?id=${s.id}">修改</a>
<a href="${pageContext.request.contextPath}/os/Payslips?id=${s.id}">工资条</a>
</td>
</tr>
</c:forEach>