views:

418

answers:

3

I've got several Portlets with some links in them, all I want is to hide the URL params. So I thought it would be easy to include some jQuery code, which builds a form for each and binds a click event on it to submit the form.

This does not work. The action request isn't hit for some reason.

Does anyone have a different suggestion for hiding URL parameters?

+1  A: 

Links fire GET requests by default. You cannot fire HTTP GET requests without passing parameters through the URL. The only what can do this is HTTP POST. All parameters are then included in the request body. But you would need to replace all links by forms with buttons and you need to modify the server side code so that it listens on POST requests instead of GET requests to take actions accordingly.

Javascript can also fire GET requests just in "the background" with help of XMLHttpRequest, but when a client has JS disabled, your application will either break or still display the parameters. Also the client has full control over JS code, so it does not necessarily "hide" the parameters from the client, but only from the browser address bar.

BalusC
+2  A: 
<a href="#" onclick="JavaScript: handleThisLink();"> description of link </a>
<form name="mydataform" id="mydataform" action="/actionurlonyoursite" method="post">
  <input type="hidden" name="myparam" value="" />
</form>
<script type="text/javascript">
function handleThisLink()
{
    // access the hidden element in which you wish to pass the value of the parameter
    dojo.byId("myparam").value = "myvalue";
    // the value might be precomputed (while generating this page) or 
    // might need to be computed based on other data in the page
    // submit the form by HTTP POST method to the URL which will handle it.
    document.forms['mydataform'].submit();
    // also possible to actually send a background AJAX request and publish
    // the response to some part of the current page, thus avoiding full
    // page refresh
    // I used dojo.byId() as a shortcut to access the input element
    // this is based on dojo toolkit.
}

</script>
Shailesh Kumar
thanks for your answer shailesh - Like I told in my question: I already transformed each link to a form but my portlet action isn't triggerd by this forms - why? I don't know really. All params are set and seems to be right, but nothing happens at all
asrijaal
me again, seems like my jboss portal only reacts on GET not on POST
asrijaal
This is weird. A somewhat unrelated discussion may help here. DOJO toolkit (a JavaScript library) has a widget called dijit.layout.ContentPane. You can specify a URL with it (as an HREF) parameter. Naturally this widget accepts only GET URLs. To put the result of POST operations inside it, the way is to use its setContent() method. My button handler will submit a form in an AJAX request using POST, receive the response, and then call the setContent method of the content pane to put the response inside it. I don't know if your JBosss portal can do something similar.
Shailesh Kumar
I've solved the problem with a "workaround". If I add "?action=1" on my action - which indicates an ActionURL then portlets are processing the necessary actions. So I've changed "<form action="/portal/portletWindow" method="post">" to "<form action="/portal/portletWindow?action=1" method="post">" and it works.
asrijaal
A: 

You can using XMLHttpRequest to hide URL parameter or using session variable of servlet container.

Maybe you can using encode url to show complex data to end user.

good luck

misamap