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="+
}
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
ASKER
I want to refresh the page with an added parameter:
//refresh page("newpage.do?offset="+
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 ) ;
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 ) ;
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...
but as I said, it isn't really necessary...you have all the info you need to generate the page already...
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
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
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
There must be somewhere you're getting it from the request
So get it from the session there as well (like my code)
Tim
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?
You want all this to happen automatically?
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 ) ; %>
?
<% response.sendRedirect( "newpage.do?offset=" + pageOffset ) ; %>
?
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
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
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
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
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
I am confused again...
Tim
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("showD
success = new ActionForward( success.getPath() + "?offset="+sesOffset, success.getRedirect() ) ;
return success ;
Many thanks ;)
ASKER
>>Although it works id really appreciate how it works
Although it works id really appreciate to know how it works
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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=xx
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
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
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("showD isplay") ;
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("showD isplay") ;
// 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 ;
You would normally do:
return mapping.findForward("showD
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("showD
// 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 ;
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 ) ;
...
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 ) ;
...
ASKER
ahhhh, now I see,
Thanks Tim
:-) Good luck :-)
And thanks!
Tim
And thanks!
Tim
String sReqOffset = request.getParameter( "offset" ) ;
String sSesOffset = (String)session.getAttribu
String sOffset = null ;
if( sReqOffset != null )
{
sOffset = sReqOffset ;
}
else if( sSesOffset != null )
{
sOffset = sSesOffset ;
}