You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Filipe David Manana <fd...@ieee.org> on 2008/02/12 11:44:12 UTC

Struts2 action returning dynamically created Results

Hi,

I am trying to use an action that returns directly instances of the
Result class, due to the nature of my application where the result is
dynamically decided by some logic.

My action class method is:

  public Result doLogin() throws Exception
   {
      // etc...
      if ( !validUser )
      {
         addActionError("Authentication failed. Invalid
username/password provided.");
         return new ServletDispatcherResult("/jsp/login.jsp");
      }

      // valid user
      session.put("user", userName);

      ServletActionRedirectResult result =
(ServletActionRedirectResult) session.get("_prev_uri_");

      if ( result == null )
      {
         result = new ServletActionRedirectResult("/home", "home", "execute");
      }

      return result;
  }

  My struts.xml:

  <action name="doLogin" class="actions.LoginAction" method="doLogin">
  </action>

  After executing the action's method I always get a
NullPointerException from ServletActionRedirectResult :S

exception

javax.servlet.ServletException: java.lang.NullPointerException
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

root cause

java.lang.NullPointerException
	org.apache.struts2.dispatcher.ServletActionRedirectResult.execute(ServletActionRedirectResult.java:184)
	com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
	actions.CaptureLastURIRequestedInterceptor.intercept(CaptureLastURIRequestedInterceptor.java:48)
	com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
	com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
	com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
	com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
	org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
	org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
	org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
	org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)

Am I missing something?

cheers

-- 
Filipe David Manana,
fdmanana@ieee.org

Obvious facts are like secrets to those not trained to see them.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 action returning dynamically created Results

Posted by Filipe David Manana <fd...@ieee.org>.
Nevermind. I managed to find a working solution without creating
dymamic Result objects.

So in the interceptor code:

   public String intercept(ActionInvocation invocation) throws Exception
   {
      ActionContext context = invocation.getInvocationContext();
      Map session = context.getSession();
      String action = invocation.getProxy().getActionName();
      String namespace = invocation.getProxy().getNamespace();

      if ( ( action.equals("login") || action.equals("doLogin") )
            && namespace.equals("/") )
         return invocation.invoke();

      Object user = session.get("user");

      if ( user == null )
      {
         if ( session.get("_prev_uri_") == null )
         {
            HttpServletRequest request = (HttpServletRequest) context
                  .get(HTTP_REQUEST);

            StringBuffer uri = new StringBuffer(namespace);
            uri.append('/');
            uri.append(action);
            uri.append(ACTION_EXT); // .action, read dynamically from
struts.properties in the init() method

            if ( request.getQueryString() != null )
            {
               uri.append('?');
               uri.append(request.getQueryString());
            }

            session.put("_prev_uri_", uri.toString());
         }

         return "login";
      }

      return invocation.invoke();
}

In the doLogin action:

  public String doLogin() throws Exception
   {
      boolean validUser = false;

     // validate the user
     // etc...

      if ( !validUser )
      {
         addActionError("Authentication failed. Invalid
username/password provided.");
         return INPUT;
      }

      // valid user
      session.put("user", userName);

      redirectUri = (String) session.get("_prev_uri_");

      session.remove("_prev_uri_");

      if ( redirectUri == null )
         return "gotohome";

      return SUCCESS;
   }

And in struts.xml:

		<action name="doLogin" class="actions.LoginAction" method="doLogin">
			<result name="input">/jsp/login.jsp</result>
			<result name="success" type="redirect">
				<param name="parse">true</param>
				<param name="location">${redirectUri}</param>
			</result>
			<result name="gotohome" type="redirect-action">
				<param name="actionName">home</param>
				<param name="namespace">/home</param>
			</result>
		</action>

It was not working before, but now I changed the order of interceptors
in my interceptor stack and is working :)
If anyone interested in more details, let me know.

Thanks anyway.

On Feb 12, 2008 2:04 PM, Filipe David Manana <fd...@ieee.org> wrote:
> Yes I need. Because in the S2 configuration file I have to specify all
> the parameters of the query string one by one.
> In my app, I don't know the name and number of these parameters. So I
> am building a Servlet Redirect Action Result in an interceptor like
> this:
>
> public String intercept(ActionInvocation invocation) throws Exception
>    {
>       ActionContext context = invocation.getInvocationContext();
>       Map session = context.getSession();
>       String action = invocation.getProxy().getActionName();
>       String namespace = invocation.getProxy().getNamespace();
>
>       if ( ( action.equals("login") || action.equals("doLogin") )
>             && namespace.equals("/") )
>          return invocation.invoke();
>
>       Object user = session.get("user");
>
>       if ( user == null )
>       {
>          if ( session.get("_prev_uri_") == null )
>          {
>             String method = invocation.getProxy().getMethod();
>             ServletActionRedirectResult result = new
> ServletActionRedirectResult(namespace, action, method);
>
>             Map params = context.getParameters();
>             Iterator it = params.entrySet().iterator();
>             Map.Entry entry = null;
>
>             while ( it.hasNext() )
>             {
>                entry = (Map.Entry) it.next();
>                result.addParameter((String) entry.getKey(), entry.getValue());
>             }
>
>             session.put("_prev_uri_", result);
>          }
> // etc...
>
> I tried using a "redirect" type result in the xml config, and
> appending the query string to the url, but struts2 ignores it. The
> query string was obtained with the HttpServletRequest class. (I was
> logging it and it was correct).
>
> Any suggestion?
>
>
> On Feb 12, 2008 1:54 PM, Dave Newton <ne...@yahoo.com> wrote:
> > Are you sure you need to do it like this? You can use OGNL expressions in
> > your S2 configuration file to do things like set a URL to redirect to etc.
> >
> > Dave
> >
> >
> > --- Filipe David Manana <fd...@ieee.org> wrote:
> >
> > > Hi,
> > >
> > > I am trying to use an action that returns directly instances of the
> > > Result class, due to the nature of my application where the result is
> > > dynamically decided by some logic.
> > >
> > > My action class method is:
> > >
> > >   public Result doLogin() throws Exception
> > >    {
> > >       // etc...
> > >       if ( !validUser )
> > >       {
> > >          addActionError("Authentication failed. Invalid
> > > username/password provided.");
> > >          return new ServletDispatcherResult("/jsp/login.jsp");
> > >       }
> > >
> > >       // valid user
> > >       session.put("user", userName);
> > >
> > >       ServletActionRedirectResult result =
> > > (ServletActionRedirectResult) session.get("_prev_uri_");
> > >
> > >       if ( result == null )
> > >       {
> > >          result = new ServletActionRedirectResult("/home", "home",
> > > "execute");
> > >       }
> > >
> > >       return result;
> > >   }
> > >
> > >   My struts.xml:
> > >
> > >   <action name="doLogin" class="actions.LoginAction" method="doLogin">
> > >   </action>
> > >
> > >   After executing the action's method I always get a
> > > NullPointerException from ServletActionRedirectResult :S
> > >
> > > exception
> > >
> > > javax.servlet.ServletException: java.lang.NullPointerException
> > >
> > > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
> > >
> > >
> > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> > >
> > >
> > org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> > >
> > > root cause
> > >
> > > java.lang.NullPointerException
> > >
> > >
> > org.apache.struts2.dispatcher.ServletActionRedirectResult.execute(ServletActionRedirectResult.java:184)
> > >
> > >
> > com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
> > >
> > >
> > com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
> > >
> > >
> > actions.CaptureLastURIRequestedInterceptor.intercept(CaptureLastURIRequestedInterceptor.java:48)
> > >
> > >
> > com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
> > >
> > >
> > com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
> > >
> > >
> > com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
> > >
> > >
> > com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
> > >
> > >
> > org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
> > >
> > > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
> > >
> > >
> > org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> > >
> > >
> > org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> > >
> > > Am I missing something?
> > >
> > > cheers
> > >
> > > --
> > > Filipe David Manana,
> > > fdmanana@ieee.org
> > >
> > > Obvious facts are like secrets to those not trained to see them.
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > > For additional commands, e-mail: user-help@struts.apache.org
> > >
> > >
> >
> >
>
>
>
> --
> Filipe David Manana,
> fdmanana@ieee.org
>
> Obvious facts are like secrets to those not trained to see them.
>



-- 
Filipe David Manana,
fdmanana@ieee.org

Obvious facts are like secrets to those not trained to see them.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 action returning dynamically created Results

Posted by Filipe David Manana <fd...@ieee.org>.
Yes I need. Because in the S2 configuration file I have to specify all
the parameters of the query string one by one.
In my app, I don't know the name and number of these parameters. So I
am building a Servlet Redirect Action Result in an interceptor like
this:

public String intercept(ActionInvocation invocation) throws Exception
   {
      ActionContext context = invocation.getInvocationContext();
      Map session = context.getSession();
      String action = invocation.getProxy().getActionName();
      String namespace = invocation.getProxy().getNamespace();

      if ( ( action.equals("login") || action.equals("doLogin") )
            && namespace.equals("/") )
         return invocation.invoke();

      Object user = session.get("user");

      if ( user == null )
      {
         if ( session.get("_prev_uri_") == null )
         {
            String method = invocation.getProxy().getMethod();
            ServletActionRedirectResult result = new
ServletActionRedirectResult(namespace, action, method);

            Map params = context.getParameters();
            Iterator it = params.entrySet().iterator();
            Map.Entry entry = null;

            while ( it.hasNext() )
            {
               entry = (Map.Entry) it.next();
               result.addParameter((String) entry.getKey(), entry.getValue());
            }

            session.put("_prev_uri_", result);
         }
// etc...

I tried using a "redirect" type result in the xml config, and
appending the query string to the url, but struts2 ignores it. The
query string was obtained with the HttpServletRequest class. (I was
logging it and it was correct).

Any suggestion?

On Feb 12, 2008 1:54 PM, Dave Newton <ne...@yahoo.com> wrote:
> Are you sure you need to do it like this? You can use OGNL expressions in
> your S2 configuration file to do things like set a URL to redirect to etc.
>
> Dave
>
>
> --- Filipe David Manana <fd...@ieee.org> wrote:
>
> > Hi,
> >
> > I am trying to use an action that returns directly instances of the
> > Result class, due to the nature of my application where the result is
> > dynamically decided by some logic.
> >
> > My action class method is:
> >
> >   public Result doLogin() throws Exception
> >    {
> >       // etc...
> >       if ( !validUser )
> >       {
> >          addActionError("Authentication failed. Invalid
> > username/password provided.");
> >          return new ServletDispatcherResult("/jsp/login.jsp");
> >       }
> >
> >       // valid user
> >       session.put("user", userName);
> >
> >       ServletActionRedirectResult result =
> > (ServletActionRedirectResult) session.get("_prev_uri_");
> >
> >       if ( result == null )
> >       {
> >          result = new ServletActionRedirectResult("/home", "home",
> > "execute");
> >       }
> >
> >       return result;
> >   }
> >
> >   My struts.xml:
> >
> >   <action name="doLogin" class="actions.LoginAction" method="doLogin">
> >   </action>
> >
> >   After executing the action's method I always get a
> > NullPointerException from ServletActionRedirectResult :S
> >
> > exception
> >
> > javax.servlet.ServletException: java.lang.NullPointerException
> >
> > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
> >
> >
> org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> >
> >
> org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> >
> > root cause
> >
> > java.lang.NullPointerException
> >
> >
> org.apache.struts2.dispatcher.ServletActionRedirectResult.execute(ServletActionRedirectResult.java:184)
> >
> >
> com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
> >
> >
> com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
> >
> >
> actions.CaptureLastURIRequestedInterceptor.intercept(CaptureLastURIRequestedInterceptor.java:48)
> >
> >
> com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
> >
> >
> com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
> >
> >
> com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
> >
> >
> com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
> >
> >
> org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
> >
> > org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
> >
> >
> org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> >
> >
> org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> >
> > Am I missing something?
> >
> > cheers
> >
> > --
> > Filipe David Manana,
> > fdmanana@ieee.org
> >
> > Obvious facts are like secrets to those not trained to see them.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>
>



-- 
Filipe David Manana,
fdmanana@ieee.org

Obvious facts are like secrets to those not trained to see them.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts2 action returning dynamically created Results

Posted by Dave Newton <ne...@yahoo.com>.
Are you sure you need to do it like this? You can use OGNL expressions in
your S2 configuration file to do things like set a URL to redirect to etc.

Dave

--- Filipe David Manana <fd...@ieee.org> wrote:

> Hi,
> 
> I am trying to use an action that returns directly instances of the
> Result class, due to the nature of my application where the result is
> dynamically decided by some logic.
> 
> My action class method is:
> 
>   public Result doLogin() throws Exception
>    {
>       // etc...
>       if ( !validUser )
>       {
>          addActionError("Authentication failed. Invalid
> username/password provided.");
>          return new ServletDispatcherResult("/jsp/login.jsp");
>       }
> 
>       // valid user
>       session.put("user", userName);
> 
>       ServletActionRedirectResult result =
> (ServletActionRedirectResult) session.get("_prev_uri_");
> 
>       if ( result == null )
>       {
>          result = new ServletActionRedirectResult("/home", "home",
> "execute");
>       }
> 
>       return result;
>   }
> 
>   My struts.xml:
> 
>   <action name="doLogin" class="actions.LoginAction" method="doLogin">
>   </action>
> 
>   After executing the action's method I always get a
> NullPointerException from ServletActionRedirectResult :S
> 
> exception
> 
> javax.servlet.ServletException: java.lang.NullPointerException
> 
> org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:515)
> 
>
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> 
>
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> 
> root cause
> 
> java.lang.NullPointerException
> 
>
org.apache.struts2.dispatcher.ServletActionRedirectResult.execute(ServletActionRedirectResult.java:184)
> 
>
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
> 
>
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
> 
>
actions.CaptureLastURIRequestedInterceptor.intercept(CaptureLastURIRequestedInterceptor.java:48)
> 
>
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
> 
>
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
> 
>
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
> 
>
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
> 
>
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
> 
> org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
> 
>
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419)
> 
>
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99)
> 
> Am I missing something?
> 
> cheers
> 
> -- 
> Filipe David Manana,
> fdmanana@ieee.org
> 
> Obvious facts are like secrets to those not trained to see them.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org