如何在 Struts2中设置默认选择值标签?

发布于 2024-09-24 09:59:58 字数 92 浏览 2 评论 0原文

我是 Struts2 的新手。我有用户表单,角色列有一个下拉列表。当用户窗体处于编辑模式时,存储的值被放入相应的控件中。但我无法将下拉列表设置为默认选定值。我该怎么做呢?

I am new in Struts2. I have user form, role column have a drop-down list. When user form is in edit mode, the stored values are placed into corresponding controls. But I can't set drop-down list by default selected value. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情绪失控 2024-10-01 09:59:58

除了 Nate 的回答之外,我发现如果键类型是字符串,则需要在值属性中的数据周围加上撇号,以便它识别我的输入值是字符串。

In addition to Nate's answer, I've found that I need to put apostrophes around the data in the value attribute if the key type is a String in order for it to recognize that my input value is a String.

不爱素颜 2024-10-01 09:59:58

如果 select 标记中的值与 select 标记中列表中的键匹配,Struts 将执行正确的操作并将该值设置为默认值。请注意,类型必须匹配。

https://struts.apache.org/tag-developers/select-tag。 html

If the value in your select tag matches a key from the list in the select tag, Struts will do the correct thing and make that value the default. Note that the types must match.

https://struts.apache.org/tag-developers/select-tag.html

前事休说 2024-10-01 09:59:58

用一个例子来说明这一点:

<s:select name="employee.course.courseId" value="3"  label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="courseName" />
  • 这里 Employee 对象包含一个名为“course”的对象,它有
    属性“courseId”
  • listKey 被选为 courseId,listValue 被选为
    courseName
  • 因此输出将如下所示:

     <选项值=“1”>计算机科学
        <选项值=“2”>电子
        <选项值=“3”>机械
    
  • 值属性设置为“3”,并且它与中的第三个属性匹配
    列表,即 Mechanical

  • 因此这将是下拉列表中的默认选择值,
    因此输出 html 将类似于:

     <选项值=“1”>计算机科学
        <选项值=“2”>电子
        <选项值=“3”选择=“选定”>机械
    

希望这有帮助。

To illustrate this in an example :

<s:select name="employee.course.courseId" value="3"  label="%{getText('label.courseName')}" list="courses" listKey="courseId" listValue="courseName" />
  • Here the Employee object contains an object called "course" and it has
    a property "courseId"
  • listKey is selected as courseId and the listValue is selected as
    courseName
  • Hence the output will be like :

        <option value="1">Computer Science</option>
        <option value="2">Electronics</option>
        <option value="3">Mechanical</option>
    
  • The value attribute is set to "3" and it matches the 3rd attribute in
    the list, which is Mechanical

  • Therefore this will be the default selected value in the dropdown,
    hence the output html will be like :

        <option value="1">Computer Science</option>
        <option value="2">Electronics</option>
        <option value="3" selected="selected">Mechanical</option>
    

Hope this helps.

自我难过 2024-10-01 09:59:58

即使您正确遵循了所有内容并且它没有预先选择,那么您需要确保键的返回类型匹配。
例如,以下列表

<select onchange="showHideDefDiv('typeListId')" style="selectCss50" class="selectCss50" id="typeListId" name="definitionDiv_I">
  <option value="blank"> </option>
  <option selected="selected" value="definitionDiv_I">I</option>
</select>

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='definitionDiv_I' />

不起作用,但

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='%{editRulePojo.groupType}' />

有效。

来自 Struts2 文档:

注意:对于任何使用列表的标签(select 可能是最普遍的),它使用 OGNL 列表
表示法(参见上面的“月份”示例),应该注意的是创建的映射键(在月份示例中,
键入“01”、“02”等)。 '1' 是一个字符,'01' 是一个字符串,“1”是一个字符串。这很重要,因为如果
“value”属性返回的值与“list”属性中的键的类型不同,它们
不会匹配,即使它们的字符串值可能相等。如果不匹配,则列表中没有任何内容
将被自动选择。

Even though you follow everything correctly and it doesn't preselect then you need to make sure that return type of the key matches.
e.g. for following list

<select onchange="showHideDefDiv('typeListId')" style="selectCss50" class="selectCss50" id="typeListId" name="definitionDiv_I">
  <option value="blank"> </option>
  <option selected="selected" value="definitionDiv_I">I</option>
</select>

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='definitionDiv_I' />

doesn't work, while

<s:select list="%{editRulePojo.groupPojoList}" listKey="%{groupType}"
          listValue="%{groupTypeValue}" value='%{editRulePojo.groupType}' />

works.

From Struts2 documentation:

Note: For any of the tags that use lists (select probably being the most ubiquitous), which uses the OGNL list
notation (see the "months" example above), it should be noted that the map key created (in the months example,
the '01', '02', etc.) is typed. '1' is a char, '01' is a String, "1" is a String. This is important since if
the value returned by your "value" attribute is NOT the same type as the key in the "list" attribute, they
WILL NOT MATCH, even though their String values may be equivalent. If they don't match, nothing in your list
will be auto-selected.

当爱已成负担 2024-10-01 09:59:58

在下拉列表中显示所选值的正确方法是在操作类中创建 get 方法:

操作类:

public String getDefaultValue() {
    return "defaultVal";
} 

JSP:

<s:select theme="simple" list="yourList" name="name" id="id" listKey="id" listValue="description" value="defaultValue"/>

Right way to do show the selected value in drop down is to create get method in your action class :

Action Class:

public String getDefaultValue() {
    return "defaultVal";
} 

JSP:

<s:select theme="simple" list="yourList" name="name" id="id" listKey="id" listValue="description" value="defaultValue"/>
浪漫之都 2024-10-01 09:59:58
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
    <!-- name attribute could be anything you want but value attribute must be a model class variable-->
    <s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
        <!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result  -->
        <!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
        <s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
                <!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
                    leadSource_String_Id is element specified in var of <iterator> tag  
                -->
                <s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>

                    <option value="<s:property value='leadSource_String_Id'/>" selected="selected">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:if>
                <s:else>
                    <option
                        value="<s:property value='leadSource_String_Id'/>">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:else>
        </s:iterator>
</select>
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
    <!-- name attribute could be anything you want but value attribute must be a model class variable-->
    <s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
        <!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result  -->
        <!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
        <s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
                <!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
                    leadSource_String_Id is element specified in var of <iterator> tag  
                -->
                <s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>

                    <option value="<s:property value='leadSource_String_Id'/>" selected="selected">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:if>
                <s:else>
                    <option
                        value="<s:property value='leadSource_String_Id'/>">
                        <s:property value="leadSource_String_Name" />
                    </option>
                </s:else>
        </s:iterator>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文