1、实现这样一份功能,列如用户在进行某些操作的时候,如果没有登陆重定向到登陆页面

首先:我们创建一个功能操作页面,用户准备在该页面执行某些操作

在index.jsp中

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<a href="login.jsp">登录</a><br>
<a href="mustLogin.action">访问受保护的页面</a><br>
</body>
</html>

然后点击之后访问mustLogin.action这个action

代码如下

package com.bjpowernode.struts2;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext; public class MustLoginAction implements Action { public String execute() throws Exception {
if (ActionContext.getContext().getSession().get("user") == null) {
//重定向到登录页面
return LOGIN;
}
return SUCCESS;
} }

在该操作中:获得请求的session中存在的user用户名,如果用户名为空,就重定向到登陆页面

ActionContext.getContext().getSession().get("user")

我们来看下struct.xml的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 会提供更加友好的提示信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="struts2" extends="struts-default"> <action name="mustLogin" class="com.bjpowernode.struts2.MustLoginAction"> <result name="login" type="redirect">/login.jsp</result>
</action>
</package>
</struts>

这里可以进行配置 result的type类型配置成redirect类型,表示重定向到/login.jsp

type的默认值为dispatcher,就是type="dispatcher,表示转发"
-->
<!--
<result name="login" type="dispatcher">/login.jsp</result>
-->

<!--
type="redirect",可以重定向到任何一个web资源,如:jsp或Action
如果要重定向到Action,需要写上后缀:xxxx.action
type="redirectAction",可以重定向到Action,不需要写后缀,此种方式更通用些
不会因为后缀的改变影响配置

上面重定向只是针对mustaction有效,我们要针对所有的action都有效,我们可以配置一个全局的重定向

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 会提供更加友好的提示信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 需要继承struts-default包,这样就拥有的最基本的功能 -->
<package name="struts2" extends="struts-default">
<!-- 全局result -->
<global-results>
<result name="login" type="redirect">/login.jsp</result>
</global-results> <action name="mustLogin" class="com.bjpowernode.struts2.MustLoginAction"> </action>
</package>
</struts>

总结:

Struts2的Action访问Servlet API

1、可以通过ActionContext访问Servlet API,此种方式没有侵入性

2、在Struts2中默认为转发,也就是<result>标签中的type="dispatcher",type的属性可以修改为重定向
Struts的重定向有两种:
type="redirect",可以重定向到任何一个web资源,如:jsp或Action
如果要重定向到Action,需要写上后缀:xxxx.action
type="redirectAction",可以重定向到Action,不需要写后缀,此种方式更通用些
不会因为后缀的改变影响配置

3、关于Struts2的type类型,也就是Result类型,他们都实现了共同的接口Result,都实现了execute方法
他们体现了策略模式,具体Result类型参见:struts-default.xml文件:
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
我们完全可以自己根据需求扩展Result类型

4、全局Result和局部Result

5、struct2使用登陆的时候重定向功能,如果没有登陆,重定向到登陆页面的更多相关文章

  1. 为什么国内的好多具备 HTTPS 的网站却没有使用 HTTPS 重定向功能

    为什么国内的好多具备 HTTPS 的网站却没有使用 HTTPS 重定向功能 HTTPS 重定向 good demos ️ HTTPS http://www.xgqfrms.xyz/ https://w ...

  2. apache 网页301重定向、自定义400/403/404/500错误页面

    首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...

  3. android退出登陆后,清空之前所有的activity,进入登陆主界面

    如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...

  4. Python第十天 print >> f,和fd.write()的区别 stdout的buffer 标准输入 标准输出 从控制台重定向到文件 标准错误 重定向 输出流和输入流 捕获sys.exit()调用 optparse argparse

    Python第十天   print >> f,和fd.write()的区别    stdout的buffer  标准输入 标准输出  从控制台重定向到文件  标准错误   重定向 输出流和 ...

  5. Discuz!用户注册,登陆,生成帖子功能实现

    <?php /* * Disucz!部分功能使用说明: */ /***************************************************************** ...

  6. Apache Rewrite url重定向功能的简单配置

    http://www.jb51.net/article/24435.htm 1.Apache Rewrite的主要功能 就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我 ...

  7. 安天透过北美DDoS事件解读IoT设备安全——Mirai的主要感染对象是linux物联网设备,包括:路由器、网络摄像头、DVR设备,入侵主要通过telnet端口进行流行密码档暴力破解,或默认密码登陆,下载DDoS功能的bot,运行控制物联网设备

    安天透过北美DDoS事件解读IoT设备安全 安天安全研究与应急处理中心(安天CERT)在北京时间10月22日下午启动高等级分析流程,针对美国东海岸DNS服务商Dyn遭遇DDoS攻击事件进行了跟进分析. ...

  8. Delphi 禁用x64位系统文件重定向功能

    在X64系统里面,一些特殊的目录和特殊的注册表键被分为2个独立的部分.对于文件系统来说,      %systemroot%\system32 目录被保留给64位文件使用,而32位文件会被重定向到%s ...

  9. Windows Azure功能更新:Oracle软件正式登陆Azure了

    今天,Windows Azure国际版发布了新的功能:全面支持Oracle软件,包括Oracle Linux, Oracle 12c数据库,Weblogic 11g和12c,Oracle JDK 6和 ...

随机推荐

  1. 【转】eclipse找不到或无法加载主类

    Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法! 今天启动项目,又遇到找不到或无法加载主类的情况,清除项目后无法编译,bin文件夹下没有.class文件,至少遇到3次这个问题了 ...

  2. IT笑话十则(二)

    一.女程序员征婚 女程序员是这么征婚的: SELECT * FROM 男人们 WHERE 未婚=true and 同性恋=false and 有房=true and 有车=true and 条件 in ...

  3. AVIRIS反射率数据简介

    Surface Reflectance 高光谱图像 ↑ AVIRIS高光谱成像光谱仪采集得到的原始图像为辐亮度图像,经过校正后的L1级产品为地表辐亮度信息.但是许多时候,我们更希望知道地面目标物的反射 ...

  4. MySQL 高级—— Join 、索引 、优化

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Join 查询 1.SQL执行顺序(一般情况下) 1.1 手写顺序: SELECT DISTINCT ...

  5. Java实现 蓝桥杯VIP 算法提高 铺地毯

    算法提高 铺地毯 时间限制:1.0s 内存限制:256.0MB 问题描述 为了准备一个学生节,组织者在会场的一片矩形区域(可看做是平面直角坐标 系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从 ...

  6. 第五届蓝桥杯JavaB组省赛真题

    解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论 题目1.武功秘籍 小明到X山洞探险,捡到一本有破损的武功秘籍(2000多页!当然是伪造的).他注意到:书的第10页和第11页在同一张纸上,但第 ...

  7. SQL server 导入数据 (excel导入到SQL server数据库)

    打开数据库SQL server ,右键数据库,任务,导入数据 点击下一步 选择数据源类型 选择路径,点击下一步 选择将要生成的类型 选择登陆方式 选中,点击下一步 点击编辑映射可以修改将要生成的表,点 ...

  8. java实现 洛谷 P1540 机器

    import java.util.LinkedList; import java.util.Scanner; public class Main { private static Scanner ci ...

  9. java实现第四届蓝桥杯空白格式化

    空白格式化 本次大赛采用了全自动机器测评系统. 如果你的答案与标准答案相差了一个空格,很可能无法得分,所以要加倍谨慎! 但也不必过于惊慌.因为在有些情况下,测评系统会把你的答案进行"空白格式 ...

  10. 【Jquery】判断宽度跳转

    $(window).resize(function(){ var wWidth = screen.width; if( wWidth < 788 ){ window.location.href= ...