I am a newbie to writing JSPs.
In my JSP I have dynamically created a form with select boxes
(i.e. these fields are not in html)
In one of the selectboxes, I implemented a onchange method which passes the value as a parameter and re-displays the form. When the form is redisplayed the parameter I've passed, sets the value of another select box according to the parameter's value, using an external XML file.
One Problem is that after the onchange method as the current page is resent and reloaded, selected values of the other fields (around 10 fields) are lost.
Currently I use the following approach using the Session to address this problem
in my JSP; in the onchange method, I use javascript to load selected values and pass them as parameters to a servlet in a GET request. In the servlet, I receive the parameters and set attributes in the session.
In the JSP, I use getAttributes in the session and change the selected values during the dynamic creation of the form.
I wonder if there is a simpler way to do this as I have to create a lots of fields for this to work. Also I didnt make use of a databean in this case.
Since dynamic creation of fields is using java logic, Is it true that ajax can't be used?
Could you please advise me on the quality of my design? Am I staying true to the MVC design pattern?
If I am not wrong, your whole question basically boils (understatement) down to the following:
I have a form with a bunch of dropdowns. I am submitting a form during onchange of a dropdown. How do I retain the selected value of all other dropdows after submit?
Yes?
OK, just set the selected attribute whenever the dropdown option's value matches the submitted request parameter value. The below example assumes that you've a Map<String, String> as ${fooOptions}:
<select name="foo">
<c:forEach items="${fooOptions}" var="fooOption">
<option value="${fooOption.key}" ${fooOption.key == param.foo ? 'selected' : ''}>${fooOption.value}</option>
</c:forEach>
</select>
Do you see? When the form get submitted, the currently selected option of the dropdown with the name "foo" get sent as a request parameter and is in the server side available as request parameter ${param.foo}. While dynamically generating the options, you just check if the option's value equals to the request parameter value and if so, then just set the selected attribute.
Please note that this has nothing to do with sessions and such. They serve a different purpose, generally with regard to the current visitor in person, such as its login, preferences, shopping cart, etc. Do not use it to store form values. It'll only conflict whenever you've multiple browser windows/tabs open in the same session.
JavaScript is only helpful if you would like to do the ajax magic. But I guess that it's only another steep learning curve for you. Perhaps for later. In any way, you can find some kickoff examples in this answer: How to use Servlets and Ajax?
Related
I have a simple web page with three radio buttons that, once checked, thanks to onChange() function, the application does things.
Now, I need to save the last state of the three radio buttons. Basically, I need to know which was the latest checked radio button.
The application will be accessible from different browsers from only one person so, there aren't issue involving multiple people changing the status at the same time.
I've found this question here
Save state of radio buttons javascript
but, as soon as I need the data "in the cloud", I can't save it on memory browser or in local on the pc
Am I forced to use a database to store this only data or, am I missing something?
Maybe not the best solution but, an existing MySQL database works fine.
It can be also used an existing table used to store only static parameter. In this way, you can read the data easily using a SELECT as below
SELECT ParamName FROM Param WHERE id = 54
The data we need is saved under ParamName and it's identified by its unique ID 54 inside Param table
Use a serverside script (php,nodejs,python etc) to write to a json file. And use xhr (ajax) call to call the serverside script.
Or use a simple nosql database.
I have a form that registers teams of people. Based on a dropdown to select the number of members in the team, the form should show that many model forms, one for each member.
I am not particularly sure what the correct design patter for this is. Here are the solutions that I have come up with:
Use JS to generate the HTML for each member form and then use the Django ModelForm backend to parse each form. However, in this case I cannot use the inbuilt rendering functions of Django and validation notification becomes bothersome.
Send a GET request whenever the user changes the dropdown value, and the GET request specifies the number of members you want to add. But here it would result in any previously entered data being cleared.
Send a POST request via JS whenever the dropdown is updated, and then re-render the form with the appropriate values picked up from the POST request. I am not sure if this is the right way to do this and seems to be easy to get wrong.
Can you please advise on what is the best solution for this scenario?
I am pretty sure this has already been answered somewhere, but I can't seem to find it anywhere. If you have the link to the answer, please go ahead and mark this question as a duplicate.
I am working on an application using JSF and Hibernate. The fields on the JSF form are backed by the Hibernate entities.
Ex:
value=#{bean.entity.value}
There is a Clear button which should empty all the fields on the form. There are 3 ways to do this:
Call a javascript function which loops through all the fields on the form and sets default value based on the type of field - text/checkbox/dropdown.
Call a bean method which creates a new entity (zeroes all fields) and assign the existing entities id to it. I hoped Hibernate would then update the row with that id, but instead it is creating a new row since it is a detached entity. Is there a way to fix this?
The most straight forward way is to call a bean method and manually set default values for all the fields in the entity. The problem with this is that there are too many fields and each time any change is made on the front end, I need to update the bean method accordingly.
Which is the most appropriate way to do this?
If no business is required to calculate the default value I would use JS function to reset fields. You will save one request to the server.
You could also generate JS function by JSF and get the default value from server side.
This won't solve the issue on page reload though.
I just ran into an odd issue with my CodeIgniter app. I'm using a jQuery .on('submit') event handler to catch all form submissions and follows this procedure: grab the form object using this, stores the object for later, checks the user is logged in with an AJAX call, if logged in, gets the stored jQuery form object and submits the form with form.submit().
This works absolutely fine for all my forms (form validation works fine, returns validation errors correctly) except when I have a form with a checkbox. The checkbox is just ignored.
I use a required form validation rule to make sure the user ticks the checkbox before continuing and it works absolutely fine when this event handler is disabled however, the validation error only appears (oddly) when I add another input to the form that isn't a checkbox!
I'm wondering if there's someone that can understand the theory behind why this error is happening? I could post the code but I'm about 90% sure this is something to do with the way that JavaScript handles posting of checkbox data that I'm just not aware of.
Any ideas?
Javascript, by itself, doesn't actually post any form data.
In the standard HTML submit, if a checkbox is not checked, then there is no key/value submitted for that item.
I've learned the hard way that many Javascript form serializers (used by Ajax calls) are simply broken and don't follow the standards... I spend several days writing my own and my Qunit tests are almost longer than the code itself.
What you'll need to do is use your debugging tools (Firefox or Chrome) and watch the network tab -- see exactly what is submitted.
With very helpful advice from Jeremy J Starcher, I worked out the problem. CodeIgniter's form validation was not running since the $_POST array was empty. Usually, even with only one input field (a checkbox) the actual submit button's value is POSTed, therefore even if the checkbox isn't checked and no value for it is posted, the $_POST array is still populated by the submit button's value. However, when using Javascript to submit the form, the submit button's value is ignored and not POSTed (thanks to Jeremy for that vital info).
The solution was to add a blank, hidden input field (of a type that is always POSTed, regardless of value). There may be a nicer way to solve this using Javascript to create a blank POST variable or something and I welcome further answers relating to this.
As a side note, I was confused as to how CSRF validation was succeeding and yet the $_POST array was empty - I checked out CodeIgniter's Security Class and basically it checks the CSRF POST variable and if it's all good, it unsets it, so by the time the $_POST array was hitting the Form Validation Class, it was empty.
I am going to do my best to describe this. What I am looking to do is populate a form with values based on a single dropdown. I want to take what the user selects in the dropdown, send a request to rails with the value they selected, do some stuff in a controller based on that selection and return an object back to the form with the fields filled in. Ideally I'd like to do this with jquery.
What you're describing on the frontend probably lends itself to and MVC or MVVM framework. I've used knockout.js for that with great success.
Basically you bind the properties of a JS "viewmodel" object to various elements in the markup. When the viewmodel changes, it automatically updates the markup, and vice versa. When you're ready to send it to your backend, use jQuery.post() to send back a serialized version of the viewmodel. The server can then respond with data that can be used to populate various fields in the viewmodel.