我花了一整天的时间才弄清楚,但我终于成功地做到了你想要的(我也需要同样的行为)。
我现在可以用类似的东西定义选择:
<my:myselect key="label.dataSet" name="dataSet"
list="dataSetList" listKey="key" listValue="value"
cssClass="formSelect" onchange="checkDataSet(this)"
listDisabled="disabled" />
假设“禁用”是 KeyValueElement 对象的新属性。将此属性设置为 null 或 "" 以外的值,您的选项将被禁用。
基本上,您要做的是创建原始 Struts 的 Select 的子类/组件。您至少需要以下文件
我的选择.java
@StrutsTag(name = "myselect", tldTagClass = "ch.yotus.project.tags.MySelectTag", description = "Create a Select component with the possibility to disable options")
public class MySelect extends Select {
final public static String TEMPLATE = "myselect";
protected String listDisabled;
public MySelect(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
super(stack, request, response);
}
@Override
protected String getDefaultTemplate() {
return TEMPLATE;
}
@Override
public void evaluateExtraParams() {
super.evaluateExtraParams();
if (listDisabled != null) {
addParameter("listDisabled", findString(listDisabled));
}
}
@StrutsTagAttribute(description = "Can be used to disable an option")
public void setListDisabled(String listDisabled) {
this.listDisabled = listDisabled;
}
}
MySelectTag.java
public class MySelectTag extends SelectTag {
private static final long serialVersionUID = 1L;
protected String listDisabled;
public Component getBean(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new MySelect(stack, req, res);
}
@Override
protected void populateParams() {
super.populateParams();
MySelect select = ((MySelect) component);
select.setListDisabled(listDisabled);
}
public void setListDisabled(String listDisabled) {
this.listDisabled = listDisabled;
}
}
我的选择模型
public class MySelectModel extends TagModel {
public MySelectModel(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
super(stack, req, res);
}
@Override
protected Component getBean() {
return new MySelect(stack, req, res);
}
}
然后,您需要从资源文件夹中的 Struts2 核心 jar (template/simple/select.ftl) 中复制原始 select.ftl 文件,在末尾添加以下代码:
我的选择.ftl
<@s.iterator value="parameters.list">
[Omitted code for reading purposes]
<#if parameters.listTitle??>
<#if stack.findString(parameters.listTitle)??>
<#assign itemTitle= stack.findString(parameters.listTitle)/>
<#else>
<#assign itemTitle = ''/>
</#if>
</#if>
<#if parameters.listDisabled??>
<#if stack.findString(parameters.listDisabled)??>
<#assign itemDisabled= stack.findString(parameters.listDisabled)/>
<#else>
<#assign itemDisabled = ''/>
</#if>
</#if>
<option value="${itemKeyStr?html}"<#rt/>
<#if tag.contains(parameters.nameValue, itemKey) == true>
selected="selected"<#rt/>
</#if>
<#if itemDisabled?if_exists != "">
disabled="disabled"<#rt/>
</#if>
<#if itemCssClass?if_exists != "">
class="${itemCssClass?html}"<#rt/>
</#if>
<#if itemCssStyle?if_exists != "">
style="${itemCssStyle?html}"<#rt/>
</#if>
<#if itemTitle?if_exists != "">
title="${itemTitle?html}"<#rt/>
</#if>
>${itemValue?html}</option><#lt/>
</@s.iterator>
您必须添加的是使用“listDisabled”和“itemDisabled”的行,我希望它足够清楚(只是模仿已经存在的内容,我无法在代码中突出显示内容)。
然后,在 project-tags.tld 中添加新标签:
项目标签.tld
[Beginning of the file skipped]
<tag>
<name>myselect</name>
<tag-class>ch.yotus.project.tags.MySelectTag</tag-class>
<body-content>JSP</body-content>
<description><![CDATA[Custom select that can disable options]]></description>
<attribute>
<name>listDisabled</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Value to output]]></description>
</attribute>
<attribute>
<name>accesskey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<description><![CDATA[Set the html accesskey attribute on rendered html element]]></description>
</attribute>
[Other attributes from original tld skipped]
</tag>
警告:您必须具有原始选择标签的所有属性(可以在 struts-core jar 中找到)。除非您不需要其中的某些。
您的项目 TagLibProvider 还必须知道这个新标签:
项目标签库.java
public class ProjectTagLib implements TagLibraryModelProvider {
@Override
public Object getModels(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new ProjectModels(stack, req, res);
}
}
项目模型.java
public class ProjectModels {
protected ValueStack stack;
protected HttpServletRequest req;
protected HttpServletResponse res;
protected MySelectModel mySelectModel;
public ProjectModels(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
this.stack = stack;
this.req = req;
this.res = res;
}
public MySelectModel getMySelectModel() {
if (mySelectModel == null) {
mySelectModel = new MySelectModel(stack, req, res);
}
return mySelectModel;
}
}
最后在 struts.xml
struts.xml
<struts>
[...]
<bean type="org.apache.struts2.views.TagLibraryModelProvider" name="my" class="ch.yotus.project.tags.ProjectTagLib" />
[...]
</struts>
标签库包括
<%@ taglib prefix="my" uri="/WEB-INF/base/project-tags/project-tags.tld" %>
我希望这些是它工作所需的所有文件。但基本思想是创建您自己的选择标签,该标签将能够将“禁用”添加到“”标记中。