When user submits one page, I need to update hidden field values before submitting. How can I achieve that?
<af:button text="Continue" action="#{addAccount_backing.addBankAccountConfirm}" partialSubmit="true" onclick="goSubmit(this);"/>
Inside goSubmit java script function I need to update hidden field values before posting. I'm using ADF framework and JSF for view.
You can use f:ajax and do the stuff you need, See this example
Related
I am newbie in UI i.e. front end. I am using Javascript and JSP. I need to perform the below task.
I have a textarea in jsp page which gets populated from a text file which i read using Java. Now, I want to empty the text area and that text file on clicking a button. Emptying the file is not an issue only i don't get how to transfer call from jsp file to Servlet on clicking that button.
Please help.
There are two ways to do this:
the classical way is form submission:
http://www.tutorialspoint.com/jsp/jsp_form_processing.htm
e.g. you embedded a form in your jsp page that will send the data to your server when submitted.
the second way is via ajax
https://en.wikipedia.org/wiki/Ajax_(programming)
I would suggest you google for the basics of form submission (which is not jsp specific) or doing ajax requests with javascript (with a library, for example jquery)
there are two ways one by jsp and another by javascript
if the button is submit type and if you are using a form then
if the button is simple a button then
function x()
{
window.location.replace("servlet name");
}
please tell me if you have any doubts
There are many ways to do so.
use jquery and ajax, its so easy.
similar question here:
jQuery Ajax POST example with PHP
you can find documentation here
http://api.jquery.com/jQuery.ajax/
or use JSNI.
https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI
for some time I'm trying to pass an object between two pages, that are using different managed beans. I'm using jsf 2.1 and primefaces 3.1.
My case:
I have a p:dataTable and one column is a h:link where the user can click to edit the current object
after clicking a new page will be open, where the content of the selected object will be displayed for further actions
I've tried sending some parameters from page 1 as GET request and process them from page 2. It works, but this is not the solution I want. I need the whole object that was selected. I was thinking to pass it in the request map, or to declare the bean responsible for page 1 as #SessionScoped and to inject this bean from the second one.
What I don't succeed is to find out which object was selected before changing the page. I've tried using ajax, or an actionListener for the link tags, but nothing works. Before performing the actionListener or the ajax event, the page is changed...
How can I first set the selected object/row and only after that change the page?
Thank you for your help.
Instead of using h:link, you would want to use p:commandLink, set the selected object in actionListener, and change the page in oncomplete phase, with a javascript line like location = 'page2.xhtml';.
You can find the related tag documentation below. It also exists in version 3.1.
http://www.primefaces.org/docs/vdl/3.4/primefaces-p/commandLink.html
PS: Either page 1 or page 2 still needs to be session-scoped, otherwise it won't work.
In my web-application I have a the following requirement.
I need to have a dropdown for country.
Based on the country selected I need to display the corresponding states.
I need to fetch both the country and state from database.
I cant use javascript since in production(live), javascript will be disabled.
I guess I have to use ajax to implement this?
Can anyone please tell if there is any better way to implement the above requirement?
I have seen the following link.
Populating cascading dropdown lists in JSP/Servlet
But I dont like to use javascript (since my client will disable javascript in Production environment)
I cant use javascript since in production(live), javascript will be disabled.
Can't use javascript than you would have to refresh the page i.e.:
Select Country from drop-down
Provide a link or button to submit this value (click on this)
Server call, which will retrieve the Country values and also the State values for the country selected
display the JSP with the values retrieved and make the Country which was submitted pre-selected in the drop-down.
I guess I have to use ajax to implement this?
Now my friend you are contradicting your own self, because ajax (I am sorry to say this) is nothing but javascript, it is not some different language than javascript.
I try to find pattern how to return 'prompt()' result in java like http://www.mysticcoders.com/blog/wicket-ajax-confirmation-modal-window/ (but in this case author using 'confirm' instead of 'prompt' and doesn't return anything from javascript). Now I am using hidden field in form and update this field before submit, but maybe you know how to solve this problem more elegant (for example using AJAX components in wicket). Thank you for your time.
If you just want so submit the prompted value within your form, your hidden field approach looks adequate to me. If you want to call some Wicket code on the server with the prompted value independently of the form submission, see How do I call Java code from JavaScript code in Wicket?
In many cases you want to add HTML-Controls or Facelets to your Website, but how easy is it really to just access these when you call upon an action?
I have the following commandLink to execute an Action
<h:commandLink action="#{MyBean.save}" value="">
<f:verbatim><input type="button" value="Save"/></f:verbatim>
<f:param name="id" value="#{MyBean.id}"/>
</h:commandLink>
The Param is there to save the QueryString-state so my application won't crash.
Now, imagine you have a <input type="text" /> or <h:inputText value="hello" /> now these components are fundamental to get your site up and running, especially if you are creating some dynamics.
However, if the Controls are not Bound to anything, or if you have a Listbox which you can add elements to with JavaScript, how do you access these when you execute the commandLink or commandButton? Do you Have to bind the controls to your Bean or is it possible to access this FaceContext in another way to retreive the values of either a listbox, input text or whatever else you would want to have?
It can be difficult to treat a JSF-rendered page as a big glob of HTML and JavaScript (which, I think, is at the heart of the question). You cannot add arbitrary form controls to the application on the client and expect the JSF framework to interpret them. JSF uses pre-defined components, so a component would have to know how to interpret the extra data coming from the request. Because it all ends up as a HTML form interpreted by a servlet, the data sent from the client will still end up in the parameter map - but, these values won't pass through the JSF lifecycle, so won't benefit from validation/data binding/etc.
A JSF control tree looks much like any other widget tree (like Swing or SWT):
UIViewRoot
|_HtmlForm
|_HtmlInputText
|_HtmlCommandButton
A JSF control basically works like this:
a UIComponent instance encapsulates the values (initially populated from the JSP/Facelet); example: HtmlInputTextarea
When the page is rendered, the lifecycle looks up the Renderer implementation for the component; the Renderer writes markup to the output
When the form is posted, the same renderer looks at the incoming parameter map for keyed values it recognises and will eventually (after conversion and validation) push the new value into the UIComponent (which may in turn push it to a value binding for the model)
Taking your specific example of a listbox - this is a tricky one. JSF provides a HtmlSelectManyListbox which ends up as a SELECT element in HTML. It is possible to add OPTION children to the DOM using JavaScript on the client, but this doesn't do any good when it comes to submitting the form. If you read the HTML spec, it says:
Only selected options will be successful. When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted.
So, when the form is submitted, only the list elements that are selected by the user would be transmitted to the server. You would need some hidden fields to get all the new data to the server. There isn't a control in the core set that will help you with this.
You have a few options:
Find an existing control from a 3rd party library that does this. If you can't find one, consider using AJAX to perform the updates - look at the RichFaces select controls for inspiration.
Write your own control (warning: read the spec and be aware of the many fiddly, manual steps)
Put up with having to perform a POST operation every time you want to add an element to the list
You can retrieve values from "non-JSF" controls on your page using the standard request parameter map that can be accessed with
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
in whatever action you call eg. MyBean.save()