wood burning stoves 2.0*
The moose likes Struts and the fly likes How to do URL rewriting in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to do URL rewriting in Struts" Watch "How to do URL rewriting in Struts" New topic
Author

How to do URL rewriting in Struts

Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
Hi

I have one link in a page.
How to do URL rewriting in struts like i want to send some value when user clicks that link.

Thanks
Regards
Gaurav
Bharat Athreya
Greenhorn

Joined: Mar 09, 2006
Posts: 10
Here you go!!!

When you clikc on a link provided in a page, it would take some value attached in the URL.

First JSP :

<script>
function getPreviousPage()
{
var schemaName = document.all['selectedSchema'].value;

document.forms[0].action="getList.do?schema="+schemaName;
document.forms[0].submit();
}
</script>
......
......
<td> <a href="javascript:getPreviousPage()"> <bean:message key="com.previouspage" /> </a> </td>

Struts Config.xml :

<action path="/getList"
name="glListForm"
type="com.gltables.action.GLListAction"
parameter="getList"
scope="request"
input="login.jsp"
validate="true" >

<forward name="SUCCESS" path="/gltablesList.jsp" />
</action>

And in the Action class you can retrieve the values that is assigned to Schema using: request.getParameter("schema");

Does it make sense to you... This is one such easy way to do so...

Bharat Athreya


Bharat Athreya
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
If you are using the html:link tag, it is easy to add a parameter that will appear on the URL. Look at the paramId, paramName and paramProperty attributes. If you have to send more than one parameter, then you can use paramName and paramProperty along with a Map.

http://struts.apache.org/struts-taglib/tagreference-struts-html.html#html:link

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Regarding your original question about URL rewriting, Struts automatically does URL rewriting for:

* The action parameter in an <html:form> tag
* an <html:link> tag
* a <logic:forward> tag

So, as long as you use struts tags for any links or actions, you don't need to worry about URL rewriting.


Merrill
Consultant, Sima Solutions
Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
HI

i have follow all the steps said by you.
in my jsp page i have added:

<html:link paramId="gaurav" paramName="MiscForm" paramProperty="gaurav">TRY</html:link>

in my struts-config.xml i have written:

<form-beans>
<form-bean name="MiscForm" type="com.yourcompany.struts.miscForm" />
</form-beans>

and in my formBean class i have set the getters and setters well.

Now when i run the jsp page it says that ::::


javax.servlet.ServletException: Cannot find bean MiscForm in any scope

It is not being able to find that bean in any scope.What should i do so that it can find that bean.

Thanks
Gaurav
Gaurav Chhabras
Ranch Hand

Joined: Sep 21, 2005
Posts: 126
Hi

i have done with above problem , now the thing is that it is doing URL rewriting but not showing the value in URl like :

http://localhost:8080/struts_test/ram.jsp?gaurav=

it is not showing anything in corresponding to "gaurav" in URL, but i have set the value of this bean explicitly before creating this link.

Where i am lacking.

Regards
Gaurav
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The syntax of your tag looks correct. Are you absolutely 100% certain that the getGuarav() called on your instance of MiscForm returns a value other than blank? I'd suggest putting a System.out.println in the method.

Below is a small test page that I created. In this page, the value of the link is http://myserver/myapp/error.jsp?guarav=xyz

Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
It might help if you posted the related action definitions from struts-config along with the code where you are setting this value.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to do URL rewriting in Struts
 
Similar Threads
Do I need to "add" encodeURL for "action" tag in this case ?
Servlet basics
cookies in JSP
How can I turn off URL-Rewriting of tomcat ? Thank you!
cookie interference with user session