• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3155
  • Last Modified:

passing a parameter in url with struts


Hi all,

Im trying to pass a parameter like so to a struts page:
mypage.do?param=dfd

But it does'nt redirect to that page. If I just go to mypage.do it will then redirect to that page.

Im new to struts so im confused as to whats going on.

Thanks very much
0
prograMNewbie
Asked:
prograMNewbie
  • 5
  • 4
  • 3
  • +3
1 Solution
 
boonlengCommented:
Where you put the url to pass the parameter.
Can you post the codes.
0
 
prograMNewbieAuthor Commented:

Im using respnse.Redirect("mypage.do?myparam=dfd");

I think its something to do with struts config but im not sure what?
0
 
aflat362Commented:
In general:

Don't use Response.redirect.

Instead use the built in Struts Action Forwarding.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aflat362Commented:
Some more info on my above comment.

Use struts-config.xml to set up all of your actions.

If you are on a JSP and want a page to go somewhere else, link to an action.

The action could be a Struts Action (a servlet)
or a Tiles Page

If you are in a servlet and want the page to go somewhere else, forward the action:

Like this:

return mapping.findForward("YourForward");
0
 
boonlengCommented:
If you are redirecting in action class, try using action forward. E.g:

struts-config.xml
<action path="/someAction"  type="SomeActionClass">
    <forward name="success" path="mypage.do?myparam=dfd" redirect="true"/>
</action>

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)throws Exception {
   return mapping.findForward("success");
}


If you are redirecting in jsp, try using <logic:redirect> tag
Example
<logic:redirect href="mypage.do?myparam=dfd" />
0
 
actonwangCommented:
>>Im using respnse.Redirect("mypage.do?myparam=dfd");

     It has nothing to do with struts. at least you need to use:

     response.sendRedirect("..");

This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.

   so make sure you pass in correct URI.

Acton
0
 
prograMNewbieAuthor Commented:

Thank you for all the comments.

Im still confident is something to do with struts config. If I could get the following working id be fine. Im still learnign struts so im sure im missing something simple. Let me re - phrase my question.
I load my web app and im given 2 options  - A and B, ill call this page the options page.
When i click A im redirected to page A.
Now if I add "?myparam=myparam" within page A I'm re-directed to the options page.
Should'nt the current page just refresh?

Hope this makes more sense.

Thanks
0
 
prograMNewbieAuthor Commented:

>>Now if I add "?myparam=myparam" within page A I'm re-directed to the options page.

To explain this more clearly:

if I add "?myparam=myparam" to the url bar in the browser within page A I'm re-directed to the options page

Thanks again
0
 
fargoCommented:
you said: if I add "?myparam=myparam" to the url bar in the browser within page A I'm re-directed to the options page

how have u done this? I mean manually for testing or in the jsp page programatically.
Moreover, show us the struts-config.xml for this particular action.
0
 
actonwangCommented:
>>Im using respnse.Redirect("mypage.do?myparam=dfd");

   1. As I posted, it should be response.sendRedirect("...").
   2. You need to write is as:
         respnse.sendRedirect("/<yourappname>/mypage.do?myparam=dfd");

      assuming you register your action in struts-config.xml as:

     <action path="/mypage" ...
       
    3. Make sure you put response.sendRedirect(...) before anything has been sent (otherwise exception will occur).

Acton


   
0
 
actonwangCommented:
If you want to do redirect, you need to be aware:

1. Before redirect, you don't output anything in servlet code or action code to avoid illegalStateException
2. You might also use the method as boonleng suggested. It is flexible and don't need to be hardcoded as I show above if you use sendRedirect(..).
    BTW: if you are sure that your mypage.do always is in the same path as in the servlet or jsp calling this, response.sendRedirect("mypage.do") should do it but it is hardly guranteed and not safe.


Hope it clarifies everything for you.

Acton
0
 
prograMNewbieAuthor Commented:

Actionwang: That does'nt work for me. It will not re - direct to the correct page.


Fargo:
>>you said: if I add "?myparam=myparam" to the url bar in the browser within page A I'm re-directed to the options page
how have u done this? I mean manually for testing or in the jsp page programatically.

I done it manually for testing.

Thanks for the comments.
0
 
prograMNewbieAuthor Commented:

I fixed it!!! I had to add "redirect=true" to my struts confg.

It was a bit of a geuss really. What does setting "reirect=true" accomplish?

Thanks
0
 
aflat362Commented:
redirect=true sends the redirect through the client's browser.  The browser receives a command to redirect the page and the URL in the address window changes and the client browser requests the new page.

without it - a server-side redirect is performed.  The user's browser is unaware of the redirection.
0
 
CrshUndrPrsreCommented:

ok, thanks
0
 
actonwangCommented:
as i said, response.sendRedirect would work anyway if you set up correctly. It is hard to some extent so I would not be suprised if you say "it doesn't work" .


see boonleng's previous post which I recommended also:

<action path="/someAction"  type="SomeActionClass">
    <forward name="success" path="mypage.do?myparam=dfd" redirect="true"/>
</action>


what "redirect =.." does is essentially as same as response.sendRedirect(..) and it handles those URI stuff for you automatically. If you have access to Struts source code you will all details.

Acton
0
 
boonlengCommented:
I agree with Action. response.sendRedirect() will work as long as the path is correct.
You can try append the contextpath to the url:

respnse.sendRedirect(request.getContextPath() + "/mypage.do?myparam=dfd");

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now