[java]增删改查后给出操作提示后跳转到数据列表的小问题解决[简单实现]

(1) 数据列表 all.jsp                                                                                                                <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.7.1.min.js"></script>

  </head>
  
  <body>
  	数据列表<br/>
	<a href="${pageContext.request.contextPath }/test/add.jsp">添加</a>
  </body>

</html>


(2) 点击“添加”----> 添加数据页面 add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
  </head>
  <body>
	<a href="${pageContext.request.contextPath }/MyServlet?method=add">确定添加</a>
  </body>

</html>


(3) 确定添加----->Servlet处理添加,并保存提示信息

package com.test.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = request.getParameter("method");
		if ("add".equals(type)) {
			this.add(request, response);
		} else if ("all".equals(type)) {
			this.all(request, response);
		}
	}


	//添加
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("执行添加");
		request.getSession().setAttribute("userMessage", "操作成功!");//用session保存提示信息
		response.sendRedirect("./test/tip.jsp");//重定向到全查询
	}

	//全查询
	public void all(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("全查询");
		request.getRequestDispatcher("./test/all.jsp").forward(request,response);
	}
}


(4)执行添加后,到操作提示页面 tip.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.7.1.min.js"></script>
	
	<script type="text/javascript">
		var t="${userMessage}";
		$(function(){
			if(t!=null&&t!=""&&t!='null'&&t.length>1){
				$("#mydiv").fadeIn(1000);
				$("#mydiv").fadeOut(1000);
			}
		});
		window.setTimeout(back,2000);
		function back(){
			window.location.href="${pageContext.request.contextPath}/MyServlet?method=all";
		}
	</script>
	
  
  </head>
  
  <body>
	<div id="mydiv" style="position:absolute;top:50%;left:45%;margin:-100px 0 0 -100px; border:1 solid black;height:200;width:300;display:none;">${userMessage }</div>
	<%session.removeAttribute("userMessage"); %>
  </body>
</html>


注:结合jquery淡入淡出效果,先让提示信息淡入,再淡出,总共2000毫秒时间,也就是2秒,设置定时器2秒后跳到全查询。

(5)回到all.jsp,由于到all.jsp是重定向过来的,所以无论怎么刷新,数据是不会重复提交的。当执行完添加后回到全查询,此时不再手动刷新当前页面,点击添加,不做任何操作,再点浏览器的后退按钮,之前的提示信息也不会再显示出来,这样就避免了把提示信息设置在all.jsp上后退重复提示的问题。


最后,欢迎访问风格清新简洁的轻博客网站[指尖一刻]




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值