zoukankan      html  css  js  c++  java
  • Struts2学习-拦截器2

    1、做一个登陆页面(loginView.jsp,才用Action来访问),
    2、登陆成功后,可以跳转到系统的首页(index.jsp),
    3、首页有一个链接(testOtherAction访问其它的功能模块)
    4、testOtherAction要检验用户是否登陆,如果没有登陆就跳回到重新登陆的Action中去
    (userAction_loginView)

    模拟自己是组长
    1、 建立一个空项目
    2、 托管到远程的代码服务器(例如码云)
    3、 在项目管理器中,添加一个module,选择Maven,勾上create from archetype,选中
    Maven…webapp
    4、 在项目管理器中,在main下面添加java目录用于放java代码,
    于main平行(选中src)添加一个test目录
    5、 添加依赖,在POM.xml,
    在dependencies节点添加多一个struts2核心包(struts2-core)的dependency

    <dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.5.13</version>
    </dependency>

    6、配置web.xml配置struts(复制粘贴)

    注意检查External Libraries是否已经有了struts2的jar下载

    7、在resources目录,右键选择New-->>XML Configuration File-->>Struts Config

    取名(struts.xml)

    下面的内容没有绝对的顺序,因为MVC框架本来就是模块化的思想

    8、先UI的jsp文件建立起来

    在webappWEB-INF建立一个jsp目录,放以下文件:

    error.jsp

    index.jsp (在这里使用el表达式,显示${office.name})

    otherFunction.jsp

    loginView.jsp

    9、根据需求,抽象出一个一个的实体,建立实体类(User,Office)

    在java目录下,建立一个包com.nf.entity,然后建立2个POJO的java

    10、先建立首页的Action(IndexAction),以及其他功能的Action(OtherFunctionAction)

    在java目录下,建立com.nf.action包,

    a.建立IndexAction类

    b.extends ActionSupport

    c.添加1个execute方法,模拟从数据库取出来的值,

    赋值给Office实例,用于前端jsp显示

    d.在Struts.xml中添加一个Action(indexAction)

    e.建立OtherFunctionAction类

    f.extends ActionSupport

    g.添加1个execute方法

    h.在Struts.xml中添加一个Action(otherFunctionAction)

    10、在com.nf.action添加一个UserAction类

    a.extends ActionSupport

    b.implements ModelDriven<User>,SessionAware

    c.添加一个属性private Map<String,Object> session = null;

    (PS.让框架自动注入,节省了手动写代码:ActionCotext…)

    在setSession方法中添加:this.session = map;

    d.添加一个属性private User user = null;

    e.在getModel()

    编写以下代码

    this.user = new User();

    return this.user;

    实现驱动模式,让框架自动帮我们赋值,节省了手写代码request.getParameters..

    f.添加2个业务方法(loginView ,login)

    g.添加loginView方法,没有任何代码,用于引用loginView.jsp

    return "loginViewSuccess";

    h.在struts中添加userAction,使用_* 等通配符来标识好这个userAction,

    i.新版Struts中,在Action节点要添加<allowed-methods>login,loginView</allowed-methods>

    j.添加login方法

    做出用户名和密码校验的业务逻辑(一般需要访问数据库)

    失败:return this.ERROR;

    成功:(1)添加session  session.put("user",user);

    (2)return this.SUCCESS;

    k.为login添加2个result(error,success)

    其中error:引用/WEB-INF/jsp/error.jsp

    Success:<result name="success" type="redirectAction">indexAction</result>

                    让客户端重新发出一个新的请求,直接访问index,地址栏会改变

    11、建立判断Session是否为null的拦截器LoginInterceptor

    a.在aciton包里,建立一个普通java,命名为LoginInterceptor.java

    b.实现Interceptor接口,在intercept方法中编写拦截规则

    (1)当session不为null,放行

           return actionInvocation.invoke();

    (2)当session为null,引导跳到重新登录的result中(取名为login)

         定义一个全局的result 与之匹配。

    <global-results>

            <result name="login" type="redirectAction">userAction_loginView</result>

          </global-results>

    12、在struts.xml中定义刚刚编写的拦截器。

    a.新增interceptors节点

    b.在interceptors中新增一个

    <interceptor name="" class=""></interceptor>

    13、在需要sesssion登录判断的Action中,添加拦截器的引用。

    a. 在Action节点里面编写引用拦截器的代码

    <interceptor-ref name="loginInterceptor"></interceptor-ref>

    b. 因为自定义拦截器,会导致系统默认的拦截器失效,所以手动添加系统默认拦截器

    <interceptor-ref name="defaultStack"></interceptor-ref>

    ps:登陆页面:http://localhost:8080/userAction_loginView

    地址:https://gitee.com/xiaomosheng888/oa01.git

    <dependency>

    <groupId>org.apache.struts</groupId>

    <artifactId>struts2-core</artifactId>

    <version>2.5.13</version>

    </dependency>

  • 相关阅读:
    一天一个shell实例(1)文本中n个出现频率最高的单词(转)
    JNDI全攻略(一)(转)
    JNDI 学习(转)
    JNDI全攻略(二)(转)
    C#开发系列 文章索引
    AppControls
    JSF系列 文章索引
    [转载]DELPHI组件安装全攻略
    什么是有价值的人生
    [转载]InnoSetup5.2.3啊风增强版 安装代码
  • 原文地址:https://www.cnblogs.com/junhua4254/p/7642839.html
Copyright © 2011-2022 走看看