5、struct2使用登陆的时候重定向功能,如果没有登陆,重定向到登陆页面

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

posted on   luzhouxiaoshuai  阅读(1008)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示