Link to home
Start Free TrialLog in
Avatar of SuAeE
SuAeE

asked on

refresh page and add a parameter (struts)


Hi all,

Im trying to refresh the current page with a parameter if a particular session variable has a value.
Below is my approach:

String pageOffset = request.getParameter( "offset" );
if(pageOffset!=null)
{
session.setAttribute( "spageOffset", pageOffset );
}

String sessionPageOffset = (String) session.getAttribute( "spageOffset");
if(sessionPageOffset != null)
{
//refresh page("newpage.do?offset="+pageOffset);
}

Im unsure how to refresh the page based on this.
Any suggestions on a better approach to this would also be appreciated.
Btw my app is based on struts.

Thanks
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Why refresh the page?  Surely you could just check the session variable where you check the request object?  ie:

    String sReqOffset = request.getParameter( "offset" ) ;
    String sSesOffset = (String)session.getAttribute( "spageOffset") ;
    String sOffset = null ;

    if( sReqOffset != null )
    {
        sOffset = sReqOffset ;
    }
    else if( sSesOffset != null )
    {
        sOffset = sSesOffset ;
    }
Avatar of SuAeE
SuAeE

ASKER


I want to refresh the page with an added parameter:
//refresh page("newpage.do?offset="+pageOffset);

So then the user will be taken to the correct page.

Sorry but I dont fully understand your previous comment.
So you want to navigate to a different page?  Ahhh, I see

Sorry, by "Refresh" I thought you meant you wanted to the current page again

Assuming you haven't outputted anything yet to the browser, you should be able to do:

    response.sendRedirect( "newpage.do?offset=" + pageOffset ) ;
Avatar of SuAeE

ASKER


>>So you want to navigate to a different page?  Ahhh, I see

Not true, i want to refresh the current page but with an added parameter.
sendRedirect should still work

but as I said, it isn't really necessary...you have all the info you need to generate the page already...
Avatar of SuAeE

ASKER


>>you have all the info you need to generate the page already

I dont understand how this can be true since I done know what the offset will be until I get it from the session?

But once you have got it from the session, why call the same page passing the exact same value through the request?

Then you have the value in the request AND the session

The code I posted in http:Q_21860261.html#16740678 shows how you can let the request value take precidence, but fall back to the session value if a request value does not exist

so

    page.do?offset=233

will work even if the session value is set to 100 for example

Hmmm...  Hope I'm explaining myself well enough :-/

Tim
Avatar of SuAeE

ASKER


>>But once you have got it from the session, why call the same page passing the exact same value through the request?

Once I get it from the session how will the page update? Im just reading the session into the page but im not doing anything with it?

>>Hope I'm explaining myself well enough
I think its me thats not explaining well enough!!

Thanks for your help.
Get it from the session before you generate the page

There must be somewhere you're getting it from the request

So get it from the session there as well (like my code)

Tim
Avatar of SuAeE

ASKER


But the page is defined in the struts-config like this:      
<forward name="display       path="/newpage.do"  />

So how can I add a parameter from there?
Now I'm really confused...

You want all this to happen automatically?
Avatar of SuAeE

ASKER


Well yes, this is why I assumed that refreshing the page would be the only method since struts stops me from directly manipulating redirect url's on the fly.

Am I not understanding something?
Have you tried:

    <%    response.sendRedirect( "newpage.do?offset=" + pageOffset ) ; %>

?
Avatar of SuAeE

ASKER


I have tried that but I get this error: "Response has already been committed"
Are you using Tiles?

If not, make sure the redirect is a the TOP of the JSP

If you are, then it will have to go in the template jsp
Avatar of SuAeE

ASKER


I'm using tiles. But getting the same error.
When you say "template jsp" you mean the layout?
no...with usig tiles I always have 3 jsps

1 layout
1 template (that defines the title, and which page to use)
1 page (which i sthe actual body code for the page)

I meant in the template one...

Or you can just use the requestDispatcher to "forward" this request on...  but you won't see the URL update to include the offset

Or you should just be able to use the value in the session as if it came from the request object as I keep saying...

Tim
Avatar of SuAeE

ASKER


>>Or you should just be able to use the value in the session as if it came from the request object as I keep saying

Apologies for re - hashing this but I just can't see the logic.

Do you mean set the sessions and perform the re-direct directly from one of the action classes?
can you post some code?

I am confused again...

Tim
Avatar of SuAeE

ASKER


I have it working Tim, thanks to you. I figured I should be passing the paramter from an action class. This can't be done on the fly since the struts-config file has my path hard-coded so after some digging I found some code that you posted on this in question 3 years ago!!

Although it works id really appreciate how it works.

Here is the code:
            ActionForward success = mapping.findForward("showDisplay") ;
            success = new ActionForward( success.getPath() + "?offset="+sesOffset, success.getRedirect() ) ;
            return success ;

Many thanks ;)
Avatar of SuAeE

ASKER


>>Although it works id really appreciate how it works

Although it works id really appreciate to know how it works
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SuAeE

ASKER


ok thanks Tim, im just confused as to how struts matches up the new actionForward in struts-config.xml since all thats in there is:

    <forward name="showDisplay" path="newpage.do" redirect="true"/>

not

    <forward name="showDisplay" path="newpage.do?offset=xxxx" redirect="true"/>
Ahhh, but it gets the

    newpage.do

from the forward, and then this line:

    success = new ActionForward( success.getPath() + "?offset="+sesOffset, success.getRedirect() ) ;

Adds "offset=" + sesOffset to it

(in the first parameter of that constructor)

Tim
Avatar of SuAeE

ASKER


hmmm ok, but whats being actually returned is "newpage.do?offset=xxx". So if I was to just do this:
Instead of:
  return success;
wrote:
 return "newpage.do?offset=xxx";
(I know it wont accept strings Im just trying to make a point.)
I would get an error.

Im still a little confused, maybe thats becasue i dont as yet fully understand the http protocol or struts.


But thanks very much for the help.

;-)
That's because you have to return an ActionForward object NOT a String object...

You would normally do:

    return mapping.findForward("showDisplay") ;

BUT that would just redirect to "newpage.do" with no offset parameter

So instead, we do (I'll break it up to see if it makes more sense to you):

    // Get the page we should go to next
    ActionForward forward = mapping.findForward("showDisplay") ;

    // Get the path that this forward goes to ("newpage.do")
    String path = forward.getPath() ;

    // add our parameter onto the path:
    path = path + "?offset=" + sesOffset ;

    // Get whether this forward shoud do a redirect (so we honour the struts-config definition)
    boolean redirect = forward.getRedirect() ;

    // Then build up a NEW ActionForward object that goes to this path (with the offset added to its path)
    ActionForward offsetForward = new ActionForward( path, redirect ) ;

    // Then return this new ActionForward.  The controller ill then move us to the specified URL
    return offsetForward ;
Avatar of SuAeE

ASKER


ok, so it in a way by passes struts-config.xml and redirects directly to the specified url?
yeah...

but it uses the URL in struts-config as the base url...  so it's not *really* skipping it...

But you could just do:

    return new ActionForward( "newpage.do?offset=" + sesOffset, true ) ;

...
Avatar of SuAeE

ASKER


ahhhh, now I see,

Thanks Tim
:-)  Good luck :-)

And thanks!

Tim