我迟到了讨论,但我的方法有点不同。我使用自定义标记处理程序为 JSP 页面提供所需的常量值(数字或字符串)。这是我的做法:
假设我有一个保留所有常量的类:
public class AppJspConstants implements Serializable {
public static final int MAXLENGTH_SIGNON_ID = 100;
public static final int MAXLENGTH_PASSWORD = 100;
public static final int MAXLENGTH_FULLNAME = 30;
public static final int MAXLENGTH_PHONENUMBER = 30;
public static final int MAXLENGTH_EXTENSION = 10;
public static final int MAXLENGTH_EMAIL = 235;
}
我也有这个非常简单的自定义标签:
public class JspFieldAttributes extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
getJspContext().setAttribute("maxlength_signon_id", AppJspConstants.MAXLENGTH_SIGNON_ID);
getJspContext().setAttribute("maxlength_password", AppJspConstants.MAXLENGTH_PASSWORD);
getJspContext().setAttribute("maxlength_fullname", AppJspConstants.MAXLENGTH_FULLNAME);
getJspContext().setAttribute("maxlength_phonenumber", AppJspConstants.MAXLENGTH_PHONENUMBER);
getJspContext().setAttribute("maxlength_extension", AppJspConstants.MAXLENGTH_EXTENSION);
getJspContext().setAttribute("maxlength_email", AppJspConstants.MAXLENGTH_EMAIL);
getJspBody().invoke(null);
}
}
然后我有一个 StringHelper.tld。在里面,我有这个:
<tag>
<name>fieldAttributes</name>
<tag-class>package.path.JspFieldAttributes</tag-class>
<body-content>scriptless</body-content>
<info>This tag provide HTML field attributes that CCS is unable to do.</info>
</tag>
在 JSP 上,我以正常方式包含 StringHelper.tld:
<%@ taglib uri="/WEB-INF/tags/StringHelper.tld" prefix="stringHelper" %>
最后,我使用标签并使用 EL 应用所需的值。
<stringHelper:fieldAttributes>
[snip]
<form:input path="emailAddress" cssClass="formeffect" cssErrorClass="formEffect error" maxlength="**${maxlength_email}**"/>
<form:errors path="emailAddress" cssClass="error" element="span"/>
[snip]
</stringHelper:fieldAttributes>