jsp中js里怎么接收后台传来的值 10
2016-03-10 · 百度知道合伙人官方认证企业
前台页面:js中 ${password},这样js就可以接受从后台中传过来的password值了。
EL表达式是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,EL表达式提供了在 JSP 中简化表达式的方法。
先将数据库的数据获取,用session将数据存起来。
HttpSession session = request.getSession();
session.setAttribute("userDetail", userDetailDao.userDetail());
[plain] view plain copy
public class UserDetailAction extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserDetailAction() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDetailDao userDetailDao = new UserDetailDao();
HttpSession session = request.getSession();
session.setAttribute("userDetail", userDetailDao.userDetail());
response.sendRedirect("managerForUserDetail.jsp");
}
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
</taglib>
</jsp-config>
<span style="color:#333333;"><%@ page language="java" contentType="text/html; charset=GBK"
import="java.util.*,com.qingzheng.entity.User,com.qingzheng.servlet.UserDetailAction,
com.qingzheng.dao.UserDetailDao"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!-- jstl的核心标签 -->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
th,td{
width:200px;
}
</style>
</head>
<body>
<h1>User Infomation</h1>
<table class="table table-striped">
<thead>
<tr>
<th>UserAccount</th>
<th>User name</th>
<th>Password</th>
<th>Sex</th>
<th>Telephone Number</th>
<th>Operation</th>
</tr>
</thead>
</table>
<table class="table table-striped">
<%
/* ArrayList<User> users = (ArrayList<User>) session.getAttribute("userDetail");
for(User user:users){
int userid = user.getUserid();
String username = user.getUsername();
String password= user.getPassword();
String sex = user.getSex();
String tel = user.getTel();*/
%>
</span><span style="color:#ff0000;"><c:forEach var="user" items="${userDetail}"></span><span style="color:#333333;">
<tr>
<td><c:out </span><span style="color:#ff0000;">value="${user.userid}"</span><span style="color:#333333;">></c:out> </td>
<td><c:out value="${user.username}"></c:out></td>
<td><c:out value="${user.password}"></c:out></td>
<td><c:out value="${user.sex}"></c:out></td>
<td><c:out value="${user.tel}"></c:out></td>
<td>
<a href="ManagerCheckUserAllDetailtAction?userid=${user.userid}">详情</a>
<a href="ManagerChangeUserDetailAction?userid=${user.userid}">修改</a>
<a href="">删除</a>
</td>
</tr>
</span><span style="color:#ff0000;"></c:forEach>
</span><span style="color:#333333;">
<%
// }
%>
</table>
</body>
</html></span>
用jstl先导入两个jar包(standard.jar和jstl.jar),放在lib文件夹下并add build path
在web.xml配置(来自菜鸟)
[html] view plain copy
在jsp中引用核心标签库的语法引用核心标签库的语法
注释部分是没有使用jstl时,在jsp页面添加Java代码,
使用jstl的c:forEach是迭代一个集合中的对象,var表示变量名,items是将被循环的集合。
[html] view plain copy
方法还是蛮多的,先给你说两个:
java代码:
<% String aaa = request.getParameter("aaa");%>
<script> var aaa = "<%=aaa%>"</script>
EL表达式
EL表达式等效于java代码 <script> var aaa = ${param.aaa}
2015-06-25