Struts 2 Select Tag Default Value - java

I'm creating a database record edit form using Struts.
<s:select id="status" name="newRecord.status" list="statusTypes" listKey="id" listValue="description" label="Status:" value="" /><br />
Example list:
Status' list:
1 Open
2 Closed
3 Pending
I want to set a default value on the status field as the current status which is stored in record.status which contains the string representation e.g. "Open".
If I set value="%{record.status} it doesn't work because that's not any list key. Is there a way I can set this value when I only have the string representation to work with?
Or should I redesign record.status to be an object with ID and String?

You should initialize the value of the newRecord.status that should be a separate property that contains the list key value. Then simply set the default value="%{record.status}". When I said the property I mean the bean property that is accessible via OGNL.

You need to use a hash instead of a list. Notice
listKey="id" listValue="description"
Then you can set the value = the key

Related

thymeleaf object binding can ignore case for form fields?

Newly started using thymeleaf for my application. I am building a dynamic form from controller where form fields are created as a list of map. Each key value pair is iterated as below.
<form th:action="#" th:object="${foo}" method="post">...
<input type="text" id=${element.key}
th:name=${element.key} th:value="${element.value}"/> ...</form>
My Pojo has properties in lower case as following java standard. But when building the key property my code uses the Capital letters from external file and also in order to have a better readability on UI not converting from upper case to lower case conversion in
mydata.put("KEY", 90);.
Upon submitting the page, my form elements not binding with propery key of Foo class due to KEY to key is not mapping. Getting null for the form fields due to input form field name as 'KEY'. I suspect the reason for getting null values for input text fields entered in html pages are due to case-sensitive of object properties in Foo class.
class Foo { private String key; //KEY can be a fix but naming convention is missing. }
Is there any thymeleaf tags available to ignore cases when binding html-elements to objects ? Or shall I use any JavaScript tweak upon submitting the form ? Like sending lower case and converting to upper case on form load to display ?
you can try to use ${#strings.toLowerCase(element.key)}
<form th:action="#" th:object="${foo}" method="post">...
<input type="text" th:id="${#strings.toLowerCase(element.key)}" th:name=${#strings.toLowerCase(element.key)} th:value="${element.value}"/> ...
</form>

Populating select options from Beans in struts

I am Having Struts 1.2 bean as below which contains Locations in Array
Register.java
public class Register extends ActionForm
{
private String[] userLocation = {"Chennai", "Bangalore", "Delhi", "Singapore"};
.
.
//Getters and Setters
.
.
.
}
I want to populate this in Dropdown select in options.I tried the Following code but it ain't working.Any Idea how to do this.Is it possible to do this with out using collection by using array.
RegisterForm.jsp
<html:select property="userSelectedLocation">
<html:options property="id" labelProperty="name" />
</html:select>
Read the documentation of the select tag and of the options tag.
The select tag expects to find the selected value of the select box in a property. You gave it userSelectedLocation as the property, but the action form doesn't seem to have any getUserSelectedLocation() method.
The options tag gets options from a collection of values. The place where it gets the collection depends on three attributes: collection, name and property. The documentation says:
Only property is specified - The value of this attribute is the name of a property of the ActionForm bean associated with our form, which will return the collection.
This means that the options tag looks for a method getId() in the action form that would return the array of values. It doesn't seem you have such a method.
The documentation clearly explains how the tag works. If you told us what you want to do (what do you want the select options to have as value and as labels, what is the property that contains the selected value), we could tell you what to use.

How to use an HTML5 data attribute to get session attribute

I'm new to the front end side of Java EE and HTML5. I have read that you could use the data attribute to read through the DOM. How would you properly use this to get a session attribute already set by java. Compared to the other methods such as using a hidden input.
<input id="sid" type="hidden" name="series" value="${sessionScope.series} />
var sid = document.getElementById("sid"), series;
Use something like this:
<div id="div1" data-special-value="${sessionScope.series}"></div>
And get the attribute value like:
document.getElementById("div1").getAttribute("data-special-value")
Or even ( http://caniuse.com/dataset ):
document.getElementById("div1").dataset("special-value")
Or with jQuery:
$("#div1").attr("data-special-value")
// or
$("#div1").data("special-value")
Although I'm not sure storing a session value on an element is right. It's definitely not wrong, I'm just wondering what you'd need/use it for with sessions. Sessions appear once.
The data-* attributes are more useful with storing related data to something. For example, if you loop through a bunch of database records and print their columns, but want to also store the row's database id once, you'd use:
<c:forEach items="${rows}" var="row">
<tr data-row-id="${row.id}">
<td>${row.name}</td>
<td>${row.description}</td>
</tr>
</c:forEach>
Then if you want to get the original row.id value, it's stored in one place an encompasses everything it pertains to (the columns). This is usually how/where I use data-* attributes. Of course, there are many ideas/uses for this.

Play! Need of Flash for input field

I am using Play 1.2.5. In the below statement (got it in Docs):
Name: <input type="text" name="name" value="${flash.name}" />
what is the use/need of value input text parameter and what does ${flash.name} means for the value field?
Please let me know about this.
Regards,
value will be the "default" value or the value to be filled on the page load (for a good example when the users inputs invalid data and you want to show or leave what he has already placed in that field, or to have text fields already filled for the user). More info here: site
the flash.name flash is a "container" of key\value pairs that are received on the request by cookie and set by you previously, it works like session but only last for one request. You can find more info here: site

How to update a the contents of a list displayed on JSP using Struts2?

I'm using Struts2 to display the contents of a list of objects on a JSP.
The flow of events is as following:
GetDataAction.java -> fetches values from
the database, fills in the ArrayList
named tableList. On success, the
displayData.jsp is shown.
displayData.jsp -> uses the s:iterate tag to display the values of objects
in the tableList.
The user changes some values in the
displayData.jsp and presses on the
Update button. On the click of
Update button, the
UpdateDataAction.java is called.
Now my problem is; How do I use the same tableList in UpdateDataAction.java to get the modified values?
I tried declaring an ArrayList with the same name 'tableList' (along with getters and setters), in UpdateDataAction.java but it throws a NullPointerException.
Please suggest.
IMO the way you are updating is not a good idea.Either you should link every row to a seperate edit page or use ajax.There are many plugins available to update table values using ajax,If you need i can provide you the links
Back to your way of doing it,i guess you are doing it as follows
<s:form action="UpdateDataActionName">
<s:iterator value="tableList">
<s:textfield name="objectName.propertyName1" value="%(propertyName1)">
<s:textfield name="objectName.propertyName2" value="%(propertyName2)">
<s:textfield name="objectName.propertyName3" value="%(propertyName3)">
</s:iterator>
<s:submit value="Update"/>
</s:form>
Now declare a list in your UpdateDataAction,of type <objectNameoftableListType> i.e. the same object type which the tabeList is representing.The name of the list must be objectName.Try to Iteate and check if you are getting the right values as submitted from the jsp.

Categories

Resources