Struts2 选择标签 - 动态添加选项 [英] Struts2 select tag - Dynamically add options

查看:33
本文介绍了Struts2 选择标签 - 动态添加选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Struts1 项目迁移到 Struts2.我遇到了以下代码.

I'm working on migrating a Struts1 project to Struts2. And I have encountered the following piece of code.

<html:select ...>
  <logic:iterate id="something" name="" type="">
    <logic:equal name="something" property="" value="">
      <html:option value=""><bean:write ... /></html:option>
    </logic:equal>
  </logic:iterate>
</html:select>

它会检查迭代器中的值,并在满足条件时将选项动态添加到选择列表中.

It checks the value in the iterator and add options to the select list dynamically if the condition is met.

如何使用 Struts2 select 标签实现类似的功能.

How can I achieve something similar with Struts2 select tag.

AFAIK,Struts2 选择标签甚至无法与纯 html 选项标签一起正常工作.

AFAIK, Struts2 select tag won't even work properly with plain html option tag.

推荐答案

确实 Struts2 select 标签不允许在标签体中使用 html option 标签.这是因为它使用 list 属性以具有键/值对的对象形式提供数据.您可以使用 listKeylistValue 来定义对象的哪些属性将用于呈现选项.您可以查看 select 标签.它有许多属性允许您自定义呈现 html.

Indeed Struts2 select tag doesn't allow to use html option tags in the body of the tag. This is because it's using list attribute to provide data in the form of an object with key/value pairs. You can use listKey and listValue to define which properties of the object will be used for rendering options. You can see UI Tags Reference Guide for select tag. It has many attributes that allows you to customize rendering html.

select 标签的实际用法:很少使用.如果您想在 JSP 中显示固定大小的列表或通过 OGNL 创建的地图,它会很有用.例如

Practical usage of the select tag: it's rarely used. It's useful if you want to show fixed size lists or a map created via OGNL in the JSP. For example

<s:select label="Months"
       name="months"
       headerKey="-1" headerValue="Select Month"
       list="#{'01':'Jan', '02':'Feb', [...]}"
       value="selectedMonth"
       required="true"
/>    

<小时>

对于大列表,请考虑使用 autocompleter 小部件.您还可以使用自动完成程序作为选择框或选择小部件作为自动完成程序.您可以在 selectautocompleter 小部件示例rel="nofollow noreferrer">Struts2 Jquery 展示.


For big lists consider to use an autocompleter widget. You can also use an autocompleter as select box or select widget as autocompleter. All examples of select and autocompleter widgets you can see in Struts2 Jquery Showcase.

当从 Struts1 迁移到 Struts2 时,您遇到了呈现呈现的 html select 标签的问题,因为 Struts2 不允许在标签主体内使用选项.因此,可以在操作的 prepare() 方法中构建选项列表.您可以通过阅读此答案找到此用例的示例.

When migrating from Struts1 to Struts2 you have faced with the problem of presenting rendered html select tag because Struts2 doesn't allow options inside the body of the tag. So constructing the list of options could be made in the prepare() method of the action. An example of this usecase you can find via reading this answer.

为什么这种方法更可取,因为您从控制器处理模型,而不是从视图处理,并且当模型准备好显示时,您正在使用一些标签或小部件来呈现 html 或填充 DOM.或者,您可以使用 Ajax 或 Angular 服务从 Struts2 控制器加载模型.数据以 JSON 格式传输.JSON 是在 Java 和 JavaScript 框架之间传输数据的非常强大的工具.

Why this approach is preferable because you are working with the model from the controller, not from the view, and when the model is ready to display you are using some tags or widgets to render html or populate DOM. Or you can use Ajax or Angular services to load a model from the Struts2 controller. The data is transferred in the JSON format. JSON is very powerful tool to transfer data between Java and JavaScript frameworks.

现在如果你仍然想使用地图通过迭代器渲染html select 标签,你可以使用下面的代码.

Now if you still want to use the map to render the html select tag via the iterator, you can use the following code.

<select id="monthId" name="form.monthId">
  <s:iterator var="month" value="%{months}">
    <s:if test="month.value != 'May'">
      <option value="${month.key}" ${month.key == form.monthId?'selected="selected"':''}>
        <s:property value="%{month.value}"/>
      </option>
    </s:if>
   </s:iterator>
</select>

<小时>

请注意,由于 Struts 请求包装器,用于预选选项的 EL 表达式应该可以访问 valueStack 变量.参见 我们如何在框架中使用 JSTL.

这篇关于Struts2 选择标签 - 动态添加选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆