frame页面跳转和信息提示页面

在web应用中经常需要判断用户是否已经登录,如果没有登录,那么跳转到登录页面。一般我们在后台页面中都会使用frame来划分功能区。这种方法比较实用,但是随之而来就有一个小问题,就是当用户session超时之后,当用户再在后台进行操作时,我们需要重新让用户登录。一般我会选择使用一个Filter来控制用户的访问权限,当用户没有登录的时候重定向到login.jsp。那么我们可以直接使用request.sendRedirect()方法来实现。但是由于是在frame中,所以这种方式会将login.jsp显示在当前的frame中,这并不是我们想要的效果。sendRedirect放方法中并没有能<A>中的target属性。这里我们可以采用javascript来实现这一功能。

PrintWriter out =resp.getWriter();

out.write("<html>");

out.write("<script>");

out.write("window.open ('login.jsp','_top')");

out.write("</script>");

out.write("</html>");

return;

当然,如果浏览器禁用了javascript,那么这种方法就没用了。不过有多少人没事将javascript禁用调用呢。如果有更好的方案(不使用js),麻烦告知我一下..

还有一个问题就是,当用户登录失败或者操作执行之后总是需要给出一些提示信息吧。如果总是使用js弹窗来做提示的话,用户体验不好。最好还是专门做一个提示页面,当需要显示提示信息的时候,我们可以呈现该页面,并将一些参数传递过来,比如提示内容,要调转的页面等。在使用struts2时,我不太喜欢使用自带的一些提示功能(也不喜欢用它的标签),小的网站应用根本不用考虑国际化之类的问题,直接把提示硬编码在代码中效率更高,谁没事三天两头的去修改代码…

message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath()
            + request.getAttribute("url");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>

        <title>提示信息</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="Refresh" content="4; url=<%=path%>" />
    </head>

    <body>
        <table border="0" align="center" cellpadding="5" cellspacing="1"
            style="font-size: 14px; color: #333333; margin-top: 100px; background: #70afd3">
            <tr style="color: #FFFFFF">
                <th>
                    提示信息
                </th>
            </tr>
            <tr>
                <td height="100" style="font-size: 12px; background: #FFFFFF">
                    <div style="font-size: 14px; font-weight: bold; margin: 10px;">
                        ${message}
                    </div>
                    <div style="margin: 10px;">
                        系统将在 
                        <span id="countDownSec" style="color: blue; font-weight: bold"></span> 秒后自动跳转,如果不想等待,直接
                        <a href=<%=path%> style="color: #069;">点击这里</a>
                    </div>
                </td>
            </tr>
        </table>
<script language="javascript" type="text/javascript">
var countDown = function(timer,eleId,interType){
    document.getElementById(eleId).innerHTML = timer;
    var interval = interType=='s'?1000:(interType=='m'?1000*60:1000*60*60);
    window.setInterval(function(){
        timer--;
        if (timer > 0) {
            document.getElementById(eleId).innerHTML = timer;
        }
    },interval);
};

countDown(4,'countDownSec','s');

</script>
    </body>
</html>

Action呈现提示页面:

setMessage("登录失败:用户名或密码不正确!","/admin/login.jsp");

return MESSAGE;

效果图:

以上是小弟个人看法,如有不妥之处还望前辈指正!


原文地址:https://www.cnblogs.com/jdluojing/p/3212419.html