Why do we need a value attribute as well as a fieldValue attribute in s:checkbox
tag, unlike s:textfield
, which has only the value attribute ?
Does the s:checkbox
tag's fieldValue
attribute allow strings other than "true" or "false" ?
In Struts2 in Action (Manning) , it says about the fieldValue
attribute
The actual value that’ll be submitted by the checkbox. Maybe true or false
That is, if I use
<s:checkbox name="wantMoreChocolates" fieldValue="true"/>
then wantMoreChocolate can only be Boolean type in the java class. In that case, why do the docs specify the type of fieldValue to be a String and not Boolean ?
the value attribute, as with the other UI components, points to the actual Java-side property to which the component is bound, a Boolean in this case.
If through "value" we specify the actual property to which the component is bound, then what's the role of the name attribute. Since, the name attribute has to point to a Boolean value, why does it have to use the value attribute to bind it to another boolean ?
I think I can have a word about this because I have struggled a lot with this topic.
What we can find in official documentation about fieldValue
of <s:checkbox>
says:
Name fieldValue Required FALSE Default TRUE Evaluated FALSE Description The actual HTML value attribute of the checkbox.
But it's saying nothing. I suggest to change it to:
With
fieldValue
, we can submit aString
type value into action class.
It means:
"true"
or "false"
, and the variable in action class is of boolean
type, the string is parsed automatically into boolean value to be stored in the boolean
type variable.fieldValue
at all, the default value "true"
of string type is always submitted to the variable, if there is any defined in Action class to hold it.Note that if we have fieldValue
set, but the checkbox is not checked, nothing will be sent to backend, meaning the String
type field to receive the checkbox's value will be null
.
But, it can do more than that. Let's see a real-world example combining <s:checkbox />
and Struts 2 Action class fields' naming rules.
Imagine I have a list of checkbox, for example, for a list of Users to select, and each checkbox contains its idUser
to process. Now if I want to parse more than one idUser
to do my task, I can define:
<s:iterator value="listUsers">
<s:checkbox name="userId" id="userId" fieldValue="%{idUser}"
....
</s:iterator>
Now we will receive a list of numbers in my action class, but with same name
. Then we define a field of type List<String>
or an array to receive all these IDs of users:
private List<String> userId;
with its getter/setter. Then we can do what we want with this list. Remember that if we want to do calculations with them, these numbers must be parsed into Integer/Long/Whatever you like.
Moreover, if we want to submit "YES/NO" or "1/0", or "radiant/dire" in a checkbox(you name it, and now you know what game I play), we can put a simple JavaScript function in the onchange
event of this checkbox to monitor its status, or, more simply, parse its value in action class.
If we have:
<s:checkbox id="myCamp" name="myCamp" fieldValue="radiant"></s:checkbox>
And in action class we must have:
String myCampString = null;
if (StringUtils.isBlank(myCamp)){ //myCamp = null, means I am in Dire.
myCampString = "dire";
} else if (myCamp.equalsIgnoreCase("radiant")){ //myCamp = "radiant", means I am in Radiant.
myCampString = "radiant";
}
// then we process myCampString, now it cannot be null.
***************************I am the middle river*******************************
As for value="true"/"false"
, I think it's a good example where we programmers cannot name things properly and think everyone can understand what we are talking about :). It can have some clearer name like preselected
/selectByDefault
, etc.
Again, refrencing Struts 2 DOC, we have:
Name value Required FALSE Default Evaluated FALSE Type String Description Preset the value of input element.
Actually, it would be better if we put:
value
in<s:checkbox>
define whether this checkbox should be checked when it is rendered for the first time. (*)
(*) Note that if value
changes after it finished loading the page, Struts 2 will not populate the value into <s:checkbox />
to change its status. JavaScript functions are responsible for this part, for example AngularJS.
For example, if I want to create a new User
with a form, and by default every user is active
when it's created, we can put this:
<s:checkbox name="isActive" id="isActive" value="true"></s:checkbox>
But, when in a form of editing users, we want to make it checked when the user is active, and not to be checked when is not active, we can do this:
<s:checkbox name="isActive" id="isActive" value="%{user.isActive}"
So that's some tips to make it more understandable, although the DOC has made it clear (in the author's way, I think).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With