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.
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 an asp.net app that does ModelMapping from a submitted form. Today, I serialize the form and everything works great. I'm planning on nesting the current data structure (that the form maps to) within a class but the problem is that the class's name can vary.
Today I use Jquery's Serialize command on the form but now I need to prepend the new class name (and a '.') to the various inputs in the form.
Is there a simple way to tell Serialize to prepend a variable name? Alternatively, does anyone know an easily prepend a class on a set of variables that are serialized?
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.
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?
This is for a web application using struts.
I have an array of objects in my Form that gets listed in a table. A user can add/edit/delete from the table. How would I send the changed table back to the Action class?
Will I need to create a string or array of strings, and parse that into an object? Is there a way that java/struts handles objects that are to be modified in the jsp? Or does this need to be cared for in javascript?
Struts binds the request parameters onto the ActionForm object based on name of the input.
actionFormObj.setBla(String x) { ... } matches <input name="bla"... in the form.
When you have related inputs, you can use maps or arrays for ActionForm properties and Struts is smart enough to treat them as well. See here.
Additionally, if your table contains read-only data that you switch to input when editing, you might have to deal with a lot of hidden fields in your form. If you still consider JavaScript as an option, you could create a POST request based on a JavaScript object (that you create with whatever data you wish from the table) and then use jQuery to send it. See here.
Indexed properties in struts, apparently takes care of this.