java Struts 2 复选框:当fieldValue 只能为true 或false 时,value 属性有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10027033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:19:36  来源:igfitidea点击:

Struts 2 checkbox : What's the use of the value attribute when the fieldValue can only be true or false?

javacheckboxstruts2struts

提问by Daud

  • Why do we need a valueattribute as well as a fieldValueattribute in s:checkboxtag, unlike s:textfield, which has only the valueattribute ?

  • Does the s:checkboxtag's fieldValueattribute allow strings other than "true" or "false" ? In Struts2 in Action (Manning) , it says about the fieldValueattribute

  • 为什么我们在标签中需要一个value属性和一个fieldValue属性s:checkbox,不像s:textfield只有value属性的 ?

  • s:checkbox标签的fieldValue属性是否允许“ true”或“ false”以外的字符串?在 Struts2 in Action (Manning) 中,它说明了fieldValue属性

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"/>

<s:checkbox name="wantMoreChocolates" fieldValue="true"/>

then wantMoreChocolatecan 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?

那么wantMoreChocolate只能是java类中的Boolean类型。在这种情况下,为什么文档将 fieldValue 的类型指定为 String 而不是 Boolean

  • Moreover, about the value property, it says
  • 此外,关于 value 属性,它说

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.

与其他 UI 组件一样,value 属性指向组件绑定到的实际 Java 端属性,在本例中为布尔值。

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 ?

如果通过“ value”我们指定了组件绑定的实际属性,那么name属性的作用是什么。既然 name 属性必须指向一个 Boolean 值,为什么它必须使用 value 属性将它绑定到另一个 boolean ?

  • Why can't prepopulation occur solely on the basis of the property corresponding to the name attribute
  • 为什么不能仅仅根据name属性对应的property进行预填充

回答by WesternGun

I think I can have a word about this because I have struggled a lot with this topic.

我想我可以谈谈这个,因为我在这个话题上挣扎了很多。

What we can find in official documentationabout fieldValueof <s:checkbox>says:

我们可以在官方文档中找到关于fieldValueof 的内容<s:checkbox>

Name           fieldValue
Required       FALSE
Default        TRUE
Evaluated      FALSE
Description    The actual HTML value attribute of the checkbox.
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 a Stringtype value into action class.

使用fieldValue,我们可以将String类型值提交到操作类中。

It means:

它的意思是:

  • When the string literally is "true"or "false", and the variable in action class is of booleantype, the string is parsed automatically into boolean value to be stored in the booleantype variable.
  • If we do not define fieldValueat 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.
  • 当字符串字面意思是"true"or "false",并且 action 类中的变量是booleantype 时,该字符串会自动解析为布尔值存储在booleantype 变量中。
  • 如果我们根本不定义,字符串类型fieldValue的默认值"true"总是提交给变量,如果在 Action 类中有任何定义来保存它。

Note that if we have fieldValueset, but the checkbox is not checked, nothing will be sent to backend, meaning the Stringtype field to receive the checkbox's value will be null.

请注意,如果我们已fieldValue设置,但未选中复选框,则不会向后端发送任何内容,这意味着String接收复选框值的类型字段将为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.

但是,它可以做的远不止这些。让我们看一个结合<s:checkbox />Struts 2 Action 类字段命名规则的真实示例。

Imagine I have a list of checkbox, for example, for a list of Users to select, and each checkbox contains its idUserto process. Now if I want to parse more than one idUserto do my task, I can define:

想象一下,我有一个复选框列表,例如,要选择的用户列表,每个复选框都包含其idUser要处理的内容。现在,如果我想解析多个idUser来完成我的任务,我可以定义:

<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:

现在我们将在我的动作类中收到一个数字列表,但具有相同的name. 然后我们定义一个类型的字段List<String>或一个数组来接收所有这些用户的 ID:

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 onchangeevent of this checkbox to monitor its status, or, more simply, parse its value in action class.

此外,如果我们想在复选框中提交“YES/NO”或“1/0”或“radiant/dire”(你说出它的名字,现在你知道我玩什么游戏了),我们可以放一个简单的 JavaScript 函数在onchange此复选框的事件中监视其状态,或者更简单地,解析其在操作类中的值。

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.

至于value="true"/"false",我认为这是一个很好的例子,我们程序员无法正确命名事物并认为每个人都能理解我们在说什么:)。它可以有一些更清晰的名称,如preselected/selectByDefault等。

Again, refrencing Struts 2 DOC, we have:

再次参考 Struts 2 DOC,我们有:

Name         value
Required     FALSE
Default  
Evaluated    FALSE
Type         String
Description  Preset the value of input element.
Name         value
Required     FALSE
Default  
Evaluated    FALSE
Type         String
Description  Preset the value of input element.

Actually, it would be better if we put:

实际上,如果我们输入:

valuein <s:checkbox>define whether this checkbox should be checked when it is rendered for the first time. (*)

value<s:checkbox>定义第一次渲染时是否应选中此复选框。(*)

(*) Note that if valuechanges 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.

(*) 请注意,如果value在完成页面加载后发生更改,Struts 2 将不会填充该值<s:checkbox />以更改其状态。JavaScript 函数负责这部分,例如 AngularJS。

For example, if I want to create a new Userwith a form, and by default every user is activewhen it's created, we can put this:

例如,如果我想User用表单创建一个新的,并且默认情况下每个用户都是active在创建时,我们可以这样写:

    <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).

所以这是一些使其更易于理解的提示,尽管 DOC 已经明确说明(我认为以作者的方式)。

回答by Umesh Awasthi

in short fieldValueis The actual HTML value attribute of the checkbox and that will be submitted by the check box.Generally we need not to set this value since this is true by default.

简而言之fieldValue就是复选框的实际HTML值属性,由复选框提交。一般我们不需要设置这个值,因为默认情况下这是真的。

On the other hand valueparameter will be used to Preset the value of input element.Please go through the Struts2 form tag document to understand how exactly its working

另一方面,value参数将用于预设输入元素的值。请查看 Struts2 表单标签文档以了解其工作原理

回答by Do Will

Look up the "key" attribute of checkbox. That may be all that you need. If you don't want to use the "key" attribute, read on.

查找复选框的“key”属性。这可能就是您所需要的。如果您不想使用“key”属性,请继续阅读。

I have found better luck using "value" attribute. I am a bit confused about "fieldValue" myself. I have seen that "fieldValue" can be used for showing the current value, but if you don't have the "value" attribute, the value (after changing on the form) is not sent to the action method.

我发现使用“价值”属性更好。我自己对“fieldValue”有点困惑。我已经看到“fieldValue”可用于显示当前值,但如果您没有“value”属性,则该值(在表单上更改后)不会发送到操作方法。

回答by Hiro_Hamada

fieldValueis the value attribute of checkbox that will be submitted by the check box.

fieldValuecan be String, boolean(true/false) etc depending upon your requirement.

For more info have a glance at this link http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/

fieldValue是复选框将提交的复选框的值属性。

fieldValue可以是String, boolean(true/false) 等,具体取决于您的要求。

有关更多信息,请浏览此链接http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/