You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ken McWilliams <ke...@gmail.com> on 2010/12/17 22:58:56 UTC

Struts2 URL building in action for JSON.

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


Re: Struts2 URL building in action for JSON.

Posted by Mead Lai <la...@gmail.com>.
you can use JsonLib, following is the usage.
http://json-lib.sourceforge.net/usage.html
I do not really clear your requirement, if you provide a example would be
better.

Regards,
Mead


On Sat, Dec 18, 2010 at 5:58 AM, Ken McWilliams <ke...@gmail.com>wrote:

> build a url with parameters so I can return it as a JSON result.

Re: Struts2 URL building in action for JSON.

Posted by Chris Pratt <th...@gmail.com>.
Struts only injects those when the container calls .inject() on the object,
when is that going to happen on a singleton?  This is what I had to do
inside a JSP Tag Library to get it to work:

  /**
   * Process the start tag
   *
   * @return EVAL_BODY_BUFFERED
   */
  @Override
  public int doStartTag () throws JspException {
    Dispatcher.getInstance().getContainer().inject(this);
    return super.doStartTag();
  } //doStartTag

So I know Struts just doesn't do it willy nilly anytime you define the
annotation.
  (*Chris*)

On Sat, Dec 18, 2010 at 12:23 PM, Dave Newton <da...@gmail.com> wrote:

> With an @Inject, the same way you can insert things like Strings or
> configuration values.
>
> On Sat, Dec 18, 2010 at 3:19 PM, Chris Pratt <thechrispratt@gmail.com
> >wrote:
>
> > How would Struts know to inject the helper class?  It wouldn't be
> creating
> > it, it would conceivably be created either as a singleton (static or
> > otherwise) and those configuration and action mapping classes are
> critical
> > to the functioning of the method.  I'd love to find a way to move this
> out
> > of my inheritance hierarchy, so do tell?
> >   (*Chris*)
> >
> > On Sat, Dec 18, 2010 at 12:14 PM, Dave Newton <da...@gmail.com>
> > wrote:
> >
> > > There's no reason it couldn't work in a "helper class", though; you can
> > > define arbitrary beans etc. inside the config.
> > >
> > > Dave
> > >
> > > On Sat, Dec 18, 2010 at 3:12 PM, Chris Pratt <thechrispratt@gmail.com
> > > >wrote:
> > >
> > > > 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
> > > > >
> > > > >
> > > >
> > >
> >
>

Re: Struts2 URL building in action for JSON.

Posted by Dave Newton <da...@gmail.com>.
With an @Inject, the same way you can insert things like Strings or
configuration values.

On Sat, Dec 18, 2010 at 3:19 PM, Chris Pratt <th...@gmail.com>wrote:

> How would Struts know to inject the helper class?  It wouldn't be creating
> it, it would conceivably be created either as a singleton (static or
> otherwise) and those configuration and action mapping classes are critical
> to the functioning of the method.  I'd love to find a way to move this out
> of my inheritance hierarchy, so do tell?
>   (*Chris*)
>
> On Sat, Dec 18, 2010 at 12:14 PM, Dave Newton <da...@gmail.com>
> wrote:
>
> > There's no reason it couldn't work in a "helper class", though; you can
> > define arbitrary beans etc. inside the config.
> >
> > Dave
> >
> > On Sat, Dec 18, 2010 at 3:12 PM, Chris Pratt <thechrispratt@gmail.com
> > >wrote:
> >
> > > 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
> > > >
> > > >
> > >
> >
>

Re: Struts2 URL building in action for JSON.

Posted by Chris Pratt <th...@gmail.com>.
How would Struts know to inject the helper class?  It wouldn't be creating
it, it would conceivably be created either as a singleton (static or
otherwise) and those configuration and action mapping classes are critical
to the functioning of the method.  I'd love to find a way to move this out
of my inheritance hierarchy, so do tell?
  (*Chris*)

On Sat, Dec 18, 2010 at 12:14 PM, Dave Newton <da...@gmail.com> wrote:

> There's no reason it couldn't work in a "helper class", though; you can
> define arbitrary beans etc. inside the config.
>
> Dave
>
> On Sat, Dec 18, 2010 at 3:12 PM, Chris Pratt <thechrispratt@gmail.com
> >wrote:
>
> > 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
> > >
> > >
> >
>

Re: Struts2 URL building in action for JSON.

Posted by Dave Newton <da...@gmail.com>.
There's no reason it couldn't work in a "helper class", though; you can
define arbitrary beans etc. inside the config.

Dave

On Sat, Dec 18, 2010 at 3:12 PM, Chris Pratt <th...@gmail.com>wrote:

> 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
> >
> >
>

Re: Struts2 URL building in action for JSON.

Posted by Chris Pratt <th...@gmail.com>.
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 <ke...@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
>
>