我想知道是否有一种方法可以调试在网络应用程序上呈现的资源包
示例属性文件
page.header1=Welcome page
示例 HTML 页面
<h1><spring:message code="page.header1" /> </h1>
所以默认情况下我们会看到
<h1>Welcome page</h1>
有没有一种方法,最好是通过查询字符串参数,我们可以关闭键值的处理,并可能渲染键
示例
<h1>page.header1</h1>
我们的想法是让非技术人员审查网站,我们希望为他们提供在键名称和值之间切换的选项。
最佳答案
Spring框架标签库没有这样的功能。但是使用扩展了 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.x.jar 一起提供)。根据自定义实现类,只有tag-class发生了变化。
<?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>
关于java - Spring 消息 - 有没有办法在 web 应用程序上输出键而不是值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229513/