Struts中html:button、html:submit、html:cancel等标签中的文字如何国际化?

Struts 中html:button、html:submit、html:cancel等标签,这些tag的各项属性中都没有一项是用来国际化的,说白一点就是没有 一个属性是可以给我们填message resource的key的,这就给国际化带来了问题,因为这些都是按钮,按钮上的文字不可能我们手动在代码中通过value这项属性来写死。

有了问题,就要查struts的资料和他的example,发现了原来要这样解决:

<html:submit property="submit" styleClass="bottonyellow">

<bean:message key="login.submitbtnvalue"/>

</html:submit>

其中,这些tag中,property这个属性是必须的,这个属性相当于输出html中的name=...,所以是必须的,如果要国际化按钮上的文字,那 么需要手动加入bean:message这一行。为什么struts能这样来实现国际化呢?查看了struts的源码,有了一些意外收获。

原 来在jsp的tag库的规范中,有几个方法,以前一直没理解:do_starttag、do_afterbody、do_endtag,现在明白了,当碰 到标签时,触发do_starttag,当碰到标签结束时,触发do_endtag,象上面那种情况,当触发到标签之间包含的内容时(即body部分), 就会触发do_afterbody(相信还有do_startbody等类似方法),struts的ButtonTag这个类就是通过在 do_afterbody方法中添加代码,将value属性重新置成我们body指定的值,来实现国际化button上的文字的目的的。以下给出 struts的ButtonTag.java的部分源码:

/**

* Process the start of this tag.

* @exception JspException if a JSP exception has occurred

*/

public int doStartTag() throws JspException {

// Do nothing until doEndTag() is called

this.text = null;

return (EVAL_BODY_TAG);

}

/**

* Save the associated label from the body content (if any).

* @exception JspException if a JSP exception has occurred

*/

public int doAfterBody() throws JspException {

if (bodyContent != null) {

String value = bodyContent.getString().trim();

if (value.length() > 0)

text = value;

}

return (SKIP_BODY);

}

/**

* Process the end of this tag.

* <p>

* Support for indexed property since Struts 1.1

* @exception JspException if a JSP exception has occurred

*/

public int doEndTag() throws JspException {

// Acquire the label value we will be generating

String label = value;

if ((label == null) && (text != null))

label = text;

if ((label == null) || (label.trim().length() < 1))

label = "Click";

// Generate an HTML element

StringBuffer results = new StringBuffer();

results.append("<input type=\"button\"");

if (property != null) {

results.append(" name=\"");

results.append(property);

// * @since Struts 1.1

if( indexed )

prepareIndex( results, null );

results.append("\"");

}

if (accesskey != null) {

results.append(" accesskey=\"");

results.append(accesskey);

results.append("\"");

}

if (tabindex != null) {

results.append(" tabindex=\"");

results.append(tabindex);

results.append("\"");

}

results.append(" value=\"");

results.append(label);

results.append("\"");

results.append(prepareEventHandlers());

results.append(prepareStyles());

results.append(getElementClose());

// Render this element to our writer

TagUtils.getInstance().write(pageContext, results.toString());

// Evaluate the remainder of this page

return (EVAL_PAGE);

}