关于在jsp页面中取得request中的值并alert出来
如题,我在servlet中对于是否删除成功都进行了setAttribute并转发到jsp页面,在jsp页面中取出msg的值后如何将这个值alert出来呢?我自己尝试写了一下不成功,请老师指点一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | package com.imooc.jdbc.servlet; import com.imooc.jdbc.bean.Message; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.service.MessageService; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Objects; public class MyMessageListServlet extends HttpServlet { private MessageService messageService; @Override public void init() throws ServletException { super .init(); messageService = new MessageService(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pathName = request.getServletPath(); if (Objects.equals( "/my/message/list.do" ,pathName)){ String pageStr = request.getParameter( "page" ); //当前是第几页 User user = (User)request.getSession().getAttribute( "user" ); String username = user.getName(); int page = 1 ; //当拿不到数据时,默认值是1 //把当前页码赋值给page if ( null != pageStr && (! "" .equals(pageStr))){ try { page = Integer.parseInt(pageStr); } catch (NumberFormatException e) { //可能会出现问题,进行try/catch //catch到异常了不管,默认第一页 e.printStackTrace(); } } List<Message> messages = messageService.getMyMessages(username,page, 5 ); //分页查询全部留言 int count = messageService.countMyMessages(username); System.out.println(count); //最后一页是第几页 int last = count % 5 == 0 ? (count/ 5 ) : ((count/ 5 ) + 1 ); request.setAttribute( "last" ,last); request.setAttribute( "messages" ,messages); request.setAttribute( "page" ,page); request.getRequestDispatcher( "/WEB-INF/views/biz/my_message_list.jsp" ).forward(request,response); } else if (Objects.equals( "/delete.do" ,pathName)) { String id = request.getParameter( "id" ); int result = messageService.deleteMsgById(id); if (result == 0 ){ request.setAttribute( "msg" , "删除留言失败!" ); request.getRequestDispatcher( "/my/message/list.do" ).forward(request,response); } else { request.setAttribute( "msg" , "删除留言成功!" ); request.getRequestDispatcher( "/my/message/list.do" ).forward(request,response); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> < html > < head > < meta charset = "UTF-8" > < title >留言板</ title > < link rel = "stylesheet" href = "../../../css/index.css" > < script type = "text/javascript" > function submitMessageForm(flag) { if ('first' == flag) { document.getElementById('page').value = 1; } else if ('pre' == flag) { var current = Number(document.getElementById('page').value); if (current > 1) { document.getElementById('page').value = current - 1; } } else if ('next' == flag) { var current = Number(document.getElementById('page').value); var last = Number(document.getElementById('last').value); if (current < last) { document.getElementById('page').value = current + 1; } } else if ('last' == flag) { var last = Number(document.getElementById('last').value); document.getElementById('page').value = last < 1 ? 1 : last; } document.getElementById('messageForm').submit(); } </ script > </ head > < body > < header > < div class = "container" > <% if (null != request.getAttribute("msg")){ String msg = (String)request.getAttribute("msg"); %> < script > var msg = '<%=msg%>' $(function () { alert(msg); }) </ script > <% } %> <% if (null != request.getSession().getAttribute("user")) {%> < nav > < a href = "/my/message/list.do" >我的留言</ a > </ nav > < nav > < a href = "/userInfo.do" >我的信息</ a > </ nav > <%} else { %> < nav > < a href = "/login.do" >登录</ a > < a href = "/regPrompt.do" >注册</ a > </ nav > <% } %> </ div > </ header > < section class = "banner" > < div class = "container" > < div > < h1 >慕课网留言板</ h1 > < p >慕课网是垂直的互联网IT技能免费学习网站。以独家视频教程、在线编程工具、学习计划、问答社区为核心特色。在这里,你可以找到最好的互联网技术牛人,也可以通过免费的在线公开视频课程学习国内领先的互联网IT技术。 </ p > </ div > </ div > </ section > < section class = "main" > < div class = "container" > < c:forEach items = "${messages}" var = "msg" > < div class = "alt-item" > < div class = "alt-head" > < div class = "alt-info" > < span >作者:< a href = "" >${msg.username}</ a ></ span > < span >时间:< fmt:formatDate pattern = "yyyy-MM-dd HH:mm:ss" value = "${msg.createTime}" /></ span > </ div > </ div > < div class = "alt-content" > < h3 >${msg.title}</ h3 > < p >${msg.content}</ p > </ div > < div id = "update" > < a href = "#" >< button >修改</ button ></ a > < a href = "/delete.do?id=${msg.id}" >< button >删除</ button ></ a > </ div > </ div > </ c:forEach > </ div > </ section > < section class = "page" > < div class = "container" > <% if (null != request.getSession().getAttribute("user")) {%> < div id = "fatie" > < a href = "/addMessagePrompt.do" >< button >点我留言</ button ></ a > </ div > <%} else { %> < div id = "fatie" > 请< a href = "/login.do" >< button >登录</ button ></ a >后留言 </ div > <% } %> < div id = "pagefy" > < ul > < form id = "messageForm" action = "/my/message/list.do" method = "post" > < input type = "hidden" id = "page" name = "page" value = "${page}" > < input type = "hidden" id = "last" name = "last" value = "${last}" > < li >< a href = "javascript:void(0)" onclick = "submitMessageForm('first')" >首页</ a ></ li > < li >< a href = "javascript:void(0)" onclick = "submitMessageForm('pre')" >上一页</ a ></ li > < li >< a href = "javascript:void(0)" >当前第${page}页</ a ></ li > < li >< a href = "javascript:void(0)" onclick = "submitMessageForm('next')" >下一页</ a ></ li > < li >< a href = "javascript:void(0)" onclick = "submitMessageForm('last')" >尾页</ a ></ li > </ form > </ ul > </ div > </ div > </ section > < footer > copy@慕课网 </ footer > </ body > </ html > |
0
收起
正在回答 回答被采纳积分+1
2回答
慕数据7156690
2019-04-01 21:44:50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <% if (null != request.getAttribute("msg")){ String msg = (String)request.getAttribute("msg"); %> < script type = "text/javascript" src = "../../../js/jquery-3.3.1.js" > var msg = '<%=msg%>' $(function () { alert(msg); }) </ script > <% } %> |
js文件和src=""都加上了,不过还是无法成功alert,请老师看看。
1 2 3 4 | src="../../../js/jquery-3.3.1.js" src="js/jquery-3.3.1.js" src="/js/jquery-3.3.1.js" 都试过,都没用。正确路径应该是怎么写? |
相似问题
登录后可查看更多问答,登录/注册
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧