struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Pratt <thechrispr...@gmail.com>
Subject Re: Struts2 URL building in action for JSON.
Date Sat, 18 Dec 2010 20:12:01 GMT
I assume what you want to do is turn an action name into a URL.  Here's what
I've used before, I'm not sure if there's a better way, but this was adapted
from code inside struts.  You could put it in your action's super class to
make it more reusable, but because of the injection required it wouldn't
really work in a helper class.
  (*Chris*)

   /**
   * Struts: Inject a flag as to whether Dynamic Method Invocation is enabled
   *
   * @param enable "true" to enable
   */
  @Inject(StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION)
  public void setEnableDynamicMethodInvocation (String enable) {
    enableDynamicMethodInvocation = Boolean.parseBoolean(enable);
  } //setEnableDynamicMethodInvocation

  /**
   * Struts: Inject the Struts Configuration for looking up the proper Action
   *
   * @param configuration The Struts Configuration
   */
  @Inject
  public void setConfiguration (Configuration configuration) {
    this.configuration = configuration;
  } //setConfiguration

  /**
   * Struts: Inject the Action Mapper
   *
   * @param actionMapper The Action Mapper
   */
  @Inject
  public void setActionMapper (ActionMapper actionMapper) {
    this.actionMapper = actionMapper;
  } //setActionMapper

  /**
   * Process the Action Attribute
   *
   * @param ctx The Action Context
   * @param stack The Value Stack
   * @param action The action attribute value
   * @param namespace The namespace attribute value
   * @return The action attribute of the generated form
   */
  protected String processAction (ActionContext ctx,ValueStack
stack,String action,String namespace) {
    HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
    HttpServletResponse res = (HttpServletResponse)pageContext.getResponse();
    if(action == null) {
        // no action supplied, default to the current request
      ActionInvocation invoke =
(ActionInvocation)stack.getContext().get(ActionContext.ACTION_INVOCATION);
      if(invoke != null) {
        ActionProxy proxy = invoke.getProxy();
        action = proxy.getActionName();
        namespace = proxy.getNamespace();
      } else {
        String uri = req.getRequestURI();
        action = uri.substring(uri.lastIndexOf('/'));
      }
    }
    String method = "";
    if(enableDynamicMethodInvocation) {
      int bang;
      if((bang = action.lastIndexOf('!')) != -1) {
        method = action.substring(bang + 1);
        action = action.substring(0,bang);
      }
    }
    if(namespace == null) {
      namespace = "";
    }
    ActionConfig config =
configuration.getRuntimeConfiguration().getActionConfig(namespace,action);
    if(config != null) {
      return UrlHelper.buildUrl(actionMapper.getUriFromActionMapping(new
ActionMapping(action,namespace,method,null)),req,res,null);
    } else if(action != null) {
      return UrlHelper.buildUrl(action,req,res,null);
    }
    return null;
  } //processAction



On Fri, Dec 17, 2010 at 1:58 PM, Ken McWilliams <ken.mcwilliams@gmail.com>wrote:

> I want <s:url> tag like functionality from within my action, so I can
> build a url with parameters so I can return it as a JSON result.
>
> So is there a method I can call from perhaps an AwareInterface that can
> accomplish this?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message