File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Setting form action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Setting form action" Watch "Setting form action" New topic
Author

Setting form action

Stefan Anderssson
Greenhorn

Joined: Oct 27, 2008
Posts: 11
Is it possible to set the form action (<form action="...") in JSF? The reason I want to do this is that I'm posting the form through a javascript and I don't want to to a full postback to the server here since this form is located inside a (n expandable) div in the page.

Everything in the form works just fine, except that if I press enter the form get submitted in a postback, and reloads the whole page. If I could just set an empty form-action I believe this would work just as I want it to.

...or perhaps I should do this in a totally different way?


Toto, I have a feeling we�re not in Kansas anymore
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

You can't do it that way. Normally, a JSF post will carry a lot of invisible freight without which it can't maintain proper context from page view to page view.

What you could do is make an AJAX request. There's some very civilized stuff available for JSF nowadays that allow you to make partial page updates and do things like that without having to code a lot of javascript yourself.
[ October 27, 2008: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Stefan Anderssson
Greenhorn

Joined: Oct 27, 2008
Posts: 11
Thanks for helping out. I've tried using RichFaces for the ajax-requests, but that didn't work out in my case (the (RichFaces) javascript-code wouldn't run and/or I got numerous javascript-errors), probably because my form is located inside a div. I guess I have to try something else for the ajax-requests.

Regarding the invisible parts: is there anything outside of the <form></form> tags that needs to be posted? Everything between the form-tags are posted (using prototype's serialize-function) and the form(s) seems to work just fine, except that when I press enter I get a postback of the whole page.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

If your Richfaces JSF has lots of JavaScript code in it, you're doing it wrong. Richfaces will handle the most common stuff for you auto-magically, including adding in all the nasty browser-specific junk that is the bane of JavaScript programmers everywhere.

ANY submit request will normally cause the entire page to be reloaded. RichFaces can tap AJAX to limit the reload to specific parts of the page using the "reRender" attribute. And yes, RichFaces does inject JSF context into form submits, so it will post both everything within the form and as much of the surrounding JSF context as it finds necessary.

HTML should be limited or non-existent on a well-designed JSF page. HTML is all about presentation and JSF is about function. Instead of using DIV tags, consider using one of the JSF or RichFaces equivalents, such as Panel.

I highly recommend spending some time looking over the RichFaces Live Demo at http://livedemo.exadel.com, especially the AJAX Support components and the Rich Output components.
Stefan Anderssson
Greenhorn

Joined: Oct 27, 2008
Posts: 11
I know that I'm not doing things here the way JSF, and RichFaces, was intended to be used, but I'm converting a (~75% finished) PHP-project that I started a while ago. Since I haven't been too impressed by the standard JSP/Servlet way of doing things I decided to try JSF for some parts (mostly forms) of the site. I might convert the whole, somewhat complicated, layout to JSF/*Faces later, but right now I'm just using it for selected parts.

I've looked at the RichFaces demos, but I haven't found anything that does exactly what I need right now: something like prototypes Ajax Updater (http://www.prototypejs.org/api/ajax/updater) where I actually load a (jsf)page, containing a form, inside a div as opposed to creating (a large number of) hidden forms that expands.

I could get everything to run; the form was saved etc. But I did get a large number of (RichFaces) javascript-errors when the form was located inside the div. When I tried using the form in the main container(s) everything worked just fine.
[ October 29, 2008: Message edited by: Stefan Anderssson ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I can't guess about your JavaScript errors except perhaps the page's concept of the element ID's might be thrown off by the DIVs.

All DIV really is is a way of framing a chunk of page with the understanding that the div resets horizontal page position. It's otherwise equivalent to a SPAN, which frames a chunk of page without resetting horizontal page position.

But neither one are generally a good way to lay out JSF. First, because part of the design of JSF is that you might want to output the page in other formats than HTML, so the page should not assume HTML by explicitly including HTML content (unfortunately, sometimes we've got no choice).

Secondly, the effects of things like DIV are normally produced in JSF by using JSF constructs. If I read your complaint correctly, you were objecting to using dynamic display components to frame static parts of your page. And you shouldn't - the basic JSF grid controls are sufficient for that. JSF generates its own divs when it needs to, by the way.

Just as a side comment, I think if there's one thing that people laying out JSF pages dislike more than anything else, it's the fact that the core JSF tags don't include any sort of table or grid component capable of creating a row- or column-spanning table. You end up having to nest simpler components to get that effect.

I can't say without actually seeing the code in detail what the ideal solution for your situation is, but I was impressed with how well RichFaces could do partial page updates with little or no user-supplied JavaScript.
[ October 29, 2008: Message edited by: Tim Holloway ]
Stefan Anderssson
Greenhorn

Joined: Oct 27, 2008
Posts: 11
Thanks for you input, again. I'm back to palying around with RichFaces instead of going for the ugly JSF hack-attempts. I haven't been able to do exactly what I want, yet, but I'm getting there.

Eventually, I will try to go for the full-blown JSF layout, but at the moment I just want to get this site up and running as fast as possible.

On a side note I have to say that it is a bit worrying that the (main)menu at livedemo.exadel.com doesn't, fully, work in my Browser (Firefox 3.0.3, Windows XP), but Firefox 3 isn't officially supported at the moment, as far as I can see.
abhijita singha
Greenhorn

Joined: Sep 07, 2010
Posts: 7
hey Stefan ...is your task completed...because i m upon the same task and facing the same problem so if you got any solution
please mail me a little explanatory code at enggabhijit.singh@gmail.com.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting form action