JSP中如何把一个页面的信息传递到另一个页面来
前言:
在做项目的某些特定的情况下,我们需要把这个页面的数据传递到另外一个页面来,在到后端来进行操作;那又哪些方法来实现呢?哪个方法是最好的呢?
方法:
方法一: location后面加值:
这可能是我们最容易想到到,也算是最简单的;只需要在我们超链接的地址后面在加上我们需要的值就行了;但是:这个再地址栏后面添加信息的大小是有限制的,并且在另外一个页面取值的时候需要对地址栏的字符串信息进行多次分割,这是非常麻烦和繁琐的;
方法二:通过form表单的形式提交过去,再获取值(*推荐);
把form表单数据隐藏,全部一起传递到下一个页面,另一个页面可以通过jstl中的${param.属性}得到值;如果把前端信息放在了WEB-INF文件夹中的话,就不能直接访问,所以还需要把数据先传递到后台的contro层,在放在rquest/modle域中,在跳转到另一个页面,通过jstl拿到值;
a.jsp:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2021-04-17
Time: 10:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>员工信息表</title>
<script>
function update(id,name,eamil,birth,createTime,department) {
document.forms[0].elements[0].value=id;
document.forms[0].elements[1].value=name;
document.forms[0].elements[2].value=eamil;
document.forms[0].elements[3].value=birth;
document.forms[0].elements[4].value=createTime;
document.forms[0].elements[5].value=department;
document.forms[0].action='<c:url value="/emplo/upd"/>';
document.forms[0].submit();
}
</script>
</head>
<body>
<form method="post">
<input type="hidden" name="id">
<input type="hidden" name="lastName">
<input type="hidden" name="email">
<input type="hidden" name="birth">
<input type="hidden" name="createTime">
<input type="hidden" name="departmentName">
<table border="1">
<caption>员工列表</caption>
<tr>
<th>员工编号</th>
<th>姓名</th>
<th>邮件</th>
<th>出生日期</th>
<th>创建时间</th>
<th>部门</th>
<th>操作</th>
</tr>
<c:forEach items="${emploees}" var="s">
<tr>
<td>${s.id}</td>
<td>${s.lastName}</td>
<td>${s.email}</td>
<td><fmt:formatDate value="${s.birth}" pattern="yyyy-MM-dd"/></td>
<td><fmt:formatDate value="${s.createTime}" pattern="yyyy-MM-dd"/></td>
<td>${s.department.departmentName}</td>
<td><a href="/emplo/delete?id=${s.id}"> 删除</a> <a href="javascript:void(0)" onclick="update('${s.id}','${s.lastName}','${s.email}','${s.birth}','${s.createTime}','${s.department.departmentName}')"> 修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
controller层:
注:Emploee类是数据库对应的实体类
@RequestMapping("/upd")
public String upd(Model model,Emploee employee){
System.out.println(employee.getBirth() instanceof Date);
model.addAttribute("emploee",employee);
return "update";
}
b.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>Title</title>
<script>
</script>
</head>
<body>
<b>修改员工:</b>
<form method="get" action="/emplo/updat" >
<input type="hidden" value="${emploee.id}" name="id">
LastName:<input type="text" name="lastName" value="${emploee.lastName}"><br>
邮件:<input type="text" name="email" value="${emploee.email}"><br>
出生日期:<input type="date" name="birth" value="<fmt:formatDate value="${emploee.birth}" pattern="yyyy-MM-dd"/>"><br>
部门:<select name="departmentId" >
<option value="2" <c:if test="${emploee.department.departmentName}==财务部?selected:‘’"></c:if>>财务部</option>
<option value="1"<c:if test="${emploee.department.departmentName}==it部?selected:‘’"></c:if>>it部</option>
<option value="3" <c:if test="${emploee.department.departmentName}==市场部?selected:‘’"></c:if>>市场部</option>
<option value="4" <c:if test="${emploee.department.departmentName}==销售部?selected:‘’"></c:if>>销售部</option>
<option value="5" <c:if test="${emploee.department.departmentName}==运维部?selected:‘’"></c:if>>运维部</option>
</select>
<input type="submit" value="提交">
</form>
</body>
</html>