SSI框架为基础开发的,hulian平台
struts2拦截器里如何知道你请求的是那个方法
使用:invocation.getInvocationContext().getName(); //输出Priv_queryPriv,这正是我访问的Action中的方法。
1.struts.xml中这么定义的
loginMain,loginTop,loginSwitch,loginRight,login,leftMenuShow, queryCityList,queryInOrOutAreaList,queryDistricts /errorPrivPage.jsp /jsp/phone/xxxx.jsp /jsp/phone/login/trunToLogin.jsp /errorPage.jsp ${successPath} ${errorPath} ${inputPath} ${redirectActionPath} ${chainPath} ${redirectPath} application/vnd.ms-excel inputStream filename="${printFileName}" 1024
2.Action这么写
/** * 权限信息控制 * @author ken * @date 2011-9-13 下午15:00:46 */ @Scope("prototype") @Controller("PrivAction") public class PrivAction extends BaseAction{ private static final long serialVersionUID = 1L; static final Logger log = Logger.getLogger(PrivAction.class); @Autowired private PrivService privService; /* 权限模型 */ private TEmployeePriv employeePriv; /** * 权限查询 * @return */ public String queryPriv(){ if(employeePriv==null){ employeePriv = new TEmployeePriv(); successPath = "/jsp/phone/priv/priv/privList.jsp"; return SUCCESS; } try { entitys = this.privService.queryAllPriv(employeePriv); } catch (Exception e) { log.error("",e); } successPath = "/jsp/phone/priv/priv/privList.jsp?flag=true"; return SUCCESS; } }
3.struts2拦截器
/** * 权限拦截器Interceptor * @author mengxianjun * @date 2011-4-8 下午03:07:24 * */ @SuppressWarnings("serial") @Component( "PrivInterceptor" ) @Scope("prototype") public class PrivInterceptor extends MethodFilterInterceptor{ @Resource(name = "EmployeeService") private EmployeeService empSafeService;//工号安全Service @Resource(name="EmployeeRoleService") private EmployeeRoleService empRoleService; /* (non-Javadoc) * @see com.opensymphony.xwork2.interceptor.MethodFilterInterceptor#doIntercept(com.opensymphony.xwork2.ActionInvocation) * @author mengxianjun * @date 2011-4-8 下午03:07:24 */ @SuppressWarnings("unchecked") @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("============"+invocation.getInvocationContext().getName()); System.out.println("============"+invocation.getInvocationContext().getLocale()); System.out.println("============"+invocation.getInvocationContext().getParameters()); System.out.println("执行到拦截器里。。。。"); ActionContext act = invocation.getInvocationContext(); //获得session Map session = invocation.getInvocationContext().getSession(); TEmployeeInfo sessionInfo = (TEmployeeInfo) session.get("user"); String employee_id=""; /** * 一、是否登录 */ try { employee_id = sessionInfo.getEmployeeId(); } catch( NullPointerException e ) { act.put("message", "Session过期,请重新登录!"); return "loginPage"; } /*=========================================================单点登录判断============================================*/ HashMapmap = (HashMap ) ServletActionContext.getServletContext().getAttribute("userList"); String sessionID_User = map.get( employee_id ); //登录用户session的ID String sessionID_Now = ServletActionContext.getRequest().getSession().getId(); //当前session的ID if( ! sessionID_User.trim().equals(sessionID_Now) ) { act.put("message", "此账号已登录!"); return "privError"; } /*=========================================================单点登录判断============================================*/ /** * 二、登录成功后,根据URL进行权限判断 */ if( !"".equals(employee_id.trim()) && null!=employee_id ) { /** * 2.1判断工号登录后,业务密码是否为123456,是跳转到商户安全设置,修改业务密码 */ /*TEmployeeSafe empSafe = empSafeService.queryEmployeSafe(employee_id); if( null!=empSafe ) { String MD5password = KeyedDigestMD5.getKeyedDigest("123456","").toUpperCase();//获得123456的MD5值 String employeePass = empSafe.getEmployeePass();//获得登录密码 String employeePass2 = empSafe.getEmployeePass2();//获得工号业务密码 if( MD5password.equals(employeePass) || MD5password.equals(employeePass2) ) { act.put("message", "欢迎使用本系统,您的登录密码、业务密码过于简单,请修改!"); return "updateEmpPassword"; } }*/ /** * 2.2截取请求URL */ HttpServletRequest request = ServletActionContext.getRequest(); String currentURL = request.getRequestURI(); String targetURL = ""; if( -1 != currentURL.indexOf("?") )//普通
输出:
16:05:35,813 INFO [STDOUT] ============Priv_queryPriv 16:05:35,813 INFO [STDOUT] ============zh_CN 16:05:35,813 INFO [STDOUT] ============{} 16:05:35,813 INFO [STDOUT] 执行到拦截器里。。。。 16:05:35,813 INFO [STDOUT] 请求路径URL:/Priv_queryPriv
=========================================================================================================
struts2 拦截拦截器怎么样获得action 后面的参数 例如 login.action? user.user_name='aaaaa'
我想获得到action 后面的参数再做判断,请问怎么样获得呢
问题补充:
我想在拦截器里面得到传入的user.user_name
最佳答案
先在拦截器intercept()方法中获取parameters对象:
Map paramMap = invocation.getInvocationContext().getParameters();
然后用parameters对象获取参数(是一个字符串数组):
String[] names = (String[]) paramMap.get("user.user_name");
String username=names[0];