我花了一整天時間弄清楚,但我終於成功地做到你想要什麼(我也需要相同的行爲)。
我現在能夠確定的東西一樣,選擇:
<my:myselect key="label.dataSet" name="dataSet"
list="dataSetList" listKey="key" listValue="value"
cssClass="formSelect" onchange="checkDataSet(this)"
listDisabled="disabled" />
假設「禁用」是你KeyValueElement對象的新屬性。將此屬性設置爲非null或「」的屬性,並且您的選項將被禁用。
基本上,你所要做的就是創建原始Struts的選擇的一個子類/組件。您將需要至少以下文件
MySelect.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;
}
}
MySelectModel
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);
}
}
然後,你需要將原來的select.ftl文件從你的資源文件夾中的Struts2的核心罐子(模板/簡單/ select.ftl)複製添加以下代碼接近尾聲:
myselect.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」時,我希望這將是足夠的(只是模仿已經存在的東西,我不能強調的東西在代碼中)明確。
然後,還可以添加新的標籤在你的項目tags.tld:
項目tags.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還必須瞭解這種新的標籤:
ProjectTagLib.java
public class ProjectTagLib implements TagLibraryModelProvider {
@Override
public Object getModels(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
return new ProjectModels(stack, req, res);
}
}
ProjectModels.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" %>
我希望那些需要爲它工作的所有文件。但基本的想法是創建自己的選擇標籤,這將能夠將「禁用」添加到「」標記。
「殘疾」選項是什麼意思? –
– Yoichi
@Yoichi:不,它不能在服務器端完成,你需要在UI中處理。請注意,根據HTML規範禁用的字段將被提交到服務器 –