牙龈炎吃头孢几代:struts2登录拦截器

来源:百度文库 编辑:中财网 时间:2024/04/29 17:36:10


     

    示例需求:

      要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源;否则,系统直接转入登陆页面。

      一、页面部分

      1、登陆页面代码(login.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”loginPage” />

      

      

      

      

      

      

      

      

      

      

      2、登陆成功页面(welcome.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”succPage” />

      

      

      

      

      

      

      

      show

      

      add

      

      qurey

      

      

      3、登陆失败页面(error.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”errorPage” />

      

      

      

      

      return

      

      

      4、和权限有关的几个显示页面

      (add.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”addPage”/>

      

      

      

      

      return login

      

      

      (show.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”showPage”/>

      

      

      

      

      return login

      

      

      (qurey.jsp)

      Java代码

      <%@ page language=”java” contentType=”text/html; charset=GBK”%>

      <%@taglib prefix=”s” uri=”/struts-tags”%>

      

      

      <s:text name=”qureyPage”/>

      

      

      

      

      return login

      

      

      <

      二、Action部分(LoginAction.java)

      Java代码

      public class LoginAction extends ActionSupport {

      private static final long serialVersionUID = 1030294046920869257L;

      private String username;

      private String password;

      // 处理用户请求的execute方法

      public String execute() throws Exception {

      if (isInvalid(getUsername()))

      return INPUT;

      if (isInvalid(getPassword()))

      return INPUT;

      if ((getUsername().equals(”mm”) || getUsername().equals(”aumy”))

      && getPassword().equals(”111”)) {

      // 通过ActionContext对象访问Web应用的Session

      ActionContext.getContext().getSession().put(”user”, getUsername());

      ActionContext.getContext().getSession().put(”pass”, getPassword());

      System.out.println(getUsername() + ”----” + getPassword());

      return SUCCESS;

      } else {

      System.out.println(getUsername() + ”----” + getPassword());

      return ERROR;

      }

      }

      private boolean isInvalid(String value) {

      return (value == null || value.length() == 0);

      }

      public String add() {

      return SUCCESS;

      }

      public String show() {

      return SUCCESS;

      }

      public String qurey() {

      return SUCCESS;

      }

      public String getUsername() {

      return username;

      }

      public void setUsername(String username) {

      this.username = username;

      }

      public String getPassword() {

      return password;

      }

      public void setPassword(String password) {

      this.password = password;

      }

      }

    三、拦截器部分(AuthorityInterceptor.java)

      Java代码

      public class AuthorityInterceptor extends AbstractInterceptor {

      private static final long serialVersionUID = 1358600090729208361L;

      //拦截Action处理的拦截方法

      public String intercept(ActionInvocation invocation) throws Exception {

      // 取得请求相关的ActionContext实例

      ActionContext ctx=invocation.getInvocationContext();

      Map session=ctx.getSession();

      //取出名为user的session属性

      String user=(String)session.get(”user”);

      //如果没有登陆,或者登陆所有的用户名不是aumy,都返回重新登陆

      if(user!=null && user.equals(”aumy”)){

      return invocation.invoke();

      }

      //没有登陆,将服务器提示设置成一个HttpServletRequest属性

      ctx.put(”tip”,”您还没有登录,请登陆系统”);

      return Action.LOGIN;

      }

      }

      四、配置文件部分

      (struts.xml)

      Java代码

      

      ”-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”

      ”http://struts.apache.org/dtds/struts-2.0.dtd”>

      

      

      

      

      

      /login.jsp

      /error.jsp

      /welcome.jsp

      

      

      /qurey.jsp

      

      

      

      

      

      

      

      class=”com.aumy.struts.example.intercepter.AuthorityInterceptor”

      name=”authority”/>

      

      

      

      

      

      

      

      

      

      

      

      /login.jsp

      

      

      method=”show”>

      /show.jsp

      

      

      method=”add”>

      /add.jsp

      

      

      

      (struts.properties)

      Java代码

      struts.custom.i18n.resources=message.messageResouce

      (web.xml)

      Java代码

      

      

      xmlns=”http://java.sun.com/xml/ns/j2ee”

      xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

      xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee

      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>

      Struts test

      

      struts2

      org.apache.struts2.dispatcher.FilterDispatcher

      

      

      struts2

      /*

      

      

      login.jsp

      

      

      五、国际化资源文件(messageResouce.properties)

      Java代码

      loginPage=Login Page

      errorPage=Error Page

      succPage=Welcome Page

      failTip=Sorry,You can't log in!

      succTip=welcome,you has logged in!

      user=User Name

      pass=User Pass

      login=Login

      showPage=Show Page

      showTip=show a example!

      addPage=Add Page

      addTip=add a example!

      qureyPage=Qurey Page

      qureyTip=qurey a example!