Spring框架Taglibs没有这样的功能。但是使用扩展Spring的MessageTag的自定义标签,您可以添加此功能。 一个工作示例here。
在此示例中,只有在查询参数messagekeys
(例如,请求中提供了http://localhost:8080/homepage?messagekeys=enabled
。
以下是基本步骤。
创建扩展Spring的MessageTag
package taglib;
import org.springframework.context.NoSuchMessageException;
import org.springframework.web.servlet.tags.MessageTag;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
public class SwitchableMessageTag extends MessageTag {
private String code;
@Override
protected String resolveMessage() throws JspException, NoSuchMessageException {
if(showMessageKeys() && hasPermission()) {
return this.code;
}
return super.resolveMessage();
}
protected boolean showMessageKeys() {
//decision whether message keys should be shown or not can be everything
//in this example it is computed on a per request basis
HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
String value = req.getParameter("messagekeys");
if(value instanceof String && "enabled".equals(value)) {
return true;
}
return false;
}
protected boolean hasPermission() {
//check if current principal has permission to inspect message keys
return true;
}
@Override
public void setCode(String code) {
super.setCode(code);
this.code = code;
}
}
创建Tag Library Descriptor下src/main/resources/META-INF/common.tld
自定义标签(我假设你正在使用Maven作为构建工具)。 它包含一个名为message
的标记,它是spring.tld(与spring-webmvc.x.x.x.jar一起提供)的消息标记的副本。只有标记类根据自定义实现类而改变。
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<tlib-version>1.0</tlib-version>
<short-name>common</short-name>
<uri>http://sandbox.local/common.tld</uri>
<tag>
<name>message</name>
<tag-class>taglib.SwitchableMessageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>message</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>code</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>arguments</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>argumentSeparator</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>text</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>htmlEscape</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>javaScriptEscape</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在JSP中,你可以使用你的自定义标签与所有与Spring自己的标签
<%@ taglib uri="http://sandbox.local/common.tld" prefix="common" %>
<a href="product/edit.do"><common:message code="add.product" /></a>
我从来没有得到全面测试这个功能,它不是为我们的项目实践。我接受验证它不是功能的答案。非常感谢 – Chris