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

查看:108
本文介绍了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 标签的UI标签参考指南.它具有许多属性,可让您自定义呈现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 小部件.您也可以使用自动完成程序作为选择框,也可以选择小部件作为自动完成程序.您可以在 Struts2 Jquery展示柜中看到的selectautocompleter窗口小部件的所有示例. .


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天全站免登陆