Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

refresh page and add a parameter (struts)

Posted on 2006-05-23
30
Medium Priority
?
1,264 Views
Last Modified: 2008-02-01

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
0
Comment
Question by:SuAeE
  • 15
  • 15
30 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 16740678
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 ;
    }
0
 

Author Comment

by:SuAeE
ID: 16740698

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.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16740702
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 ) ;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SuAeE
ID: 16740711

>>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.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16740721
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...
0
 

Author Comment

by:SuAeE
ID: 16740836

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

0
 
LVL 35

Expert Comment

by:TimYates
ID: 16740856
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
0
 

Author Comment

by:SuAeE
ID: 16740878

>>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.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16740884
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
0
 

Author Comment

by:SuAeE
ID: 16741010

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?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16741016
Now I'm really confused...

You want all this to happen automatically?
0
 

Author Comment

by:SuAeE
ID: 16741032

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?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16741057
Have you tried:

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

?
0
 

Author Comment

by:SuAeE
ID: 16741073

I have tried that but I get this error: "Response has already been committed"
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16741084
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
0
 

Author Comment

by:SuAeE
ID: 16741206

I'm using tiles. But getting the same error.
When you say "template jsp" you mean the layout?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16741235
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
0
 

Author Comment

by:SuAeE
ID: 16741470

>>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?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16742525
can you post some code?

I am confused again...

Tim
0
 

Author Comment

by:SuAeE
ID: 16743081

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 ;)
0
 

Author Comment

by:SuAeE
ID: 16743092

>>Although it works id really appreciate how it works

Although it works id really appreciate to know how it works
0
 
LVL 35

Accepted Solution

by:
TimYates earned 640 total points
ID: 16743161
Yay!  Solutions from the past coming back to help! :-D

Right, in your struts-config.xml, I assume you have the forward defined to go to the next page ie:

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

so what that code does is:

    // Get the forward as defined in struts-config.xml
    ActionForward success = mapping.findForward("showDisplay") ;

    // Create a new ActionForward which has the parameter you require added to the URL (and keep the redirect="true" bit)
    // success.getPath() == "newpage.do" (from the struts-config.xml line)
    // success.getRedirect() == "true" (from the same line)
    // we store this back into the original variable (success), as we are lazy ;-)
    success = new ActionForward( success.getPath() + "?offset="+sesOffset, success.getRedirect() ) ;

    // Then, return this actionforward that we have constructed
    return success ;

Hope this explains it :-)

Tim
0
 

Author Comment

by:SuAeE
ID: 16743250

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"/>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16749245
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
0
 

Author Comment

by:SuAeE
ID: 16749451

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.

;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16749484
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 ;
0
 

Author Comment

by:SuAeE
ID: 16749541

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

Expert Comment

by:TimYates
ID: 16749549
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 ) ;

...
0
 

Author Comment

by:SuAeE
ID: 16749571

ahhhh, now I see,

Thanks Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 16749582
:-)  Good luck :-)

And thanks!

Tim
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, the users face difficulty in accessing Outlook 2016 PST files on Windows 10 computer. One of the reasons behind it is the improper functioning of MS Outlook when the user tries to open it. MS Outlook suddenly stops working, or it will not op…
If you try to migrate from Elastix to Issabel, you will face a lot of issues. These problems are inevitable but fortunately, you can fix them. In the guide below, I will explain how I performed the migration while keeping all data and successfully t…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question