日期:2014-05-18  浏览次数:20594 次

struts2怎样控制权限?
struts2怎样控制权限?或者strut2+hibernate3.1怎样控制权限?

------解决方案--------------------
探讨
拦截器

------解决方案--------------------
这是拦截器代码
Java code

package com.lil.test_ch10_03.interceptor;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthenticationInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext ctx=ActionContext.getContext();
        Map session=ctx.getSession();
        Object user=session.get("user");
        
        if(user==null) {
            ActionSupport action=(ActionSupport)invocation.getAction();
            action.addActionError("you have to login");
            
            return Action.LOGIN;
        }
        
        return invocation.invoke();
    }
}

------解决方案--------------------
Struts2使用拦截器完成权限控制示例
struts2 权限控制

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

一、页面部分 
1、登陆页面代码(login.jsp) 
Java code

<%@ page language="java" contentType="text/html; charset=GBK"%>   
<%@taglib prefix="s" uri="/struts-tags"%>   
<html>   
    <head>   
        <title><s:text name="loginPage" /></title>   
    </head>   
    <body>   
        <!-- 使用form标签生成表单元素 -->   
        <s:form action="login">   
            <s:textfield name="username" label="%{getText('user')}" />   
            <s:textfield name="password" label="%{getText('pass')}" />   
            <s:submit value="%{getText('login')}" />   
        </s:form>   
    </body>   
</html>