コード例 #1
0
  /**
   * 方法用途和描述: 数据权限拦截器,将获取到的数据权限设置给action
   *
   * @author dongshen
   * @return
   * @since dongshen
   */
  public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext actionContext = invocation.getInvocationContext();
    log.debug(
        "---AuthenticationInterceptor----"
            + invocation.getAction()
            + "!"
            + invocation.getResultCode());

    Object action = invocation.getAction();
    if (action instanceof BaseAction) {
      @SuppressWarnings("rawtypes")
      BaseAction baseAction = (BaseAction) action;
      HttpServletRequest request =
          (HttpServletRequest) actionContext.get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
      HttpSession session = request.getSession();
      UserRightEntity tbUser =
          (UserRightEntity) session.getAttribute(SessionUtils.USER); // SessionUtils.getUser();
      if (tbUser == null) return invocation.invoke();
      // 将登录用户账号回传给页面
      baseAction.setAdminname(tbUser.getUserEntity().getAccouont());
      baseAction.setAdminrole(tbUser.getUserEntity().getTbRole().getNote());
    }
    log.debug("拦截器通过!");
    return invocation.invoke();
  }
コード例 #2
0
  public void testIncludeParameterInResult() throws Exception {

    ResultConfig resultConfig =
        new ResultConfig.Builder("", "")
            .addParam("actionName", "someActionName")
            .addParam("namespace", "someNamespace")
            .addParam("encode", "true")
            .addParam("parse", "true")
            .addParam("location", "someLocation")
            .addParam("prependServletContext", "true")
            .addParam("method", "someMethod")
            .addParam("param1", "value 1")
            .addParam("param2", "value 2")
            .addParam("param3", "value 3")
            .addParam("anchor", "fragment")
            .build();

    ActionContext context = ActionContext.getContext();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);

    Map<String, ResultConfig> results = new HashMap<String, ResultConfig>();
    results.put("myResult", resultConfig);

    ActionConfig actionConfig =
        new ActionConfig.Builder("", "", "").addResultConfigs(results).build();

    ServletActionRedirectResult result = new ServletActionRedirectResult();
    result.setActionName("myAction");
    result.setNamespace("/myNamespace");
    result.setParse(false);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setAnchor("fragment");
    result.setUrlHelper(new DefaultUrlHelper());

    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
    expect(mockInvocation.getResultCode()).andReturn("myResult");
    expect(mockActionProxy.getConfig()).andReturn(actionConfig);
    expect(mockInvocation.getInvocationContext()).andReturn(context);

    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals(
        "/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3#fragment",
        res.getRedirectedUrl());

    control.verify();
  }