2014-03-06 23 views
1

我不知道是否有一种方法到是在web应用程序呈现调试资源包Spring消息 - 有没有办法输出密钥而不是webapp上的值?

实例属性文件

page.header1=Welcome page 

的HTML页面范例

<h1><spring:message code="page.header1" /> </h1> 

所以在默认情况下我们会看到

<h1>Welcome page</h1> 

有没有办法,pre ferably通过查询字符串参数,我们可以关闭该键的值的过程中,也许渲染的关键,而不是

<h1>page.header1</h1> 

的想法是,我们有非技术人员查看一个网站,我们希望给他们在键名和值之间切换的选项。

回答

2

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 Descriptorsrc/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> 
+0

我从来没有得到全面测试这个功能,它不是为我们的项目实践。我接受验证它不是功能的答案。非常感谢 – Chris

相关问题