jsp中js里怎么接收后台传来的数据值?

 我来答
美好时光海苔69
推荐于2019-09-11 · TA获得超过7644个赞
知道小有建树答主
回答量:20
采纳率:87%
帮助的人:2743
展开全部

先将数据库的数据获取,用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");           

  • }  

  • 用jstl先导入两个jar包(standard.jar和jstl.jar),放在lib文件夹下并add build path

    在web.xml配置(来自菜鸟)

    [html] view plain copy

  • <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>  


  • 在jsp中引用核心标签库的语法引用核心标签库的语法

    注释部分是没有使用jstl时,在jsp页面添加Java代码,

    使用jstl的c:forEach是迭代一个集合中的对象,var表示变量名,items是将被循环的集合。

    [html] view plain copy

  • <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>  

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式