Question

I'm wondering if there's a way to debug resource bundles that are rendered on the webapp

Example properties file

page.header1=Welcome page

Example HTML page

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

So by default we would see

<h1>Welcome page</h1>

Is there a way, preferably via a query string parameter where we can turn off the process of the value for the key, and maybe render the key instead

Example

<h1>page.header1</h1>

The idea is we have non technical people reviewing a website and we would like to give them the option to switch between key name and value.

Was it helpful?

Solution

The Spring Framework Taglibs do not have such a feature. But with a custom tag which extends Spring's MessageTag you can add this functionality. A working example is shown here.

In this example you can toggle between keys and values only if the query paramater messagekeys e.g. http://localhost:8080/homepage?messagekeys=enabled was provided in the request.

Here are the basic steps.

Create a custom tag that extends Spring's 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;
    }
}

Create a Tag Library Descriptor under src/main/resources/META-INF/common.tld (I assume you are using maven as your build tool). It contains one tag named message which is a copy of message tag from spring.tld (shipped with spring-webmvc.x.x.x.x.jar). Only tag-class has changed according to the custom implementation 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>

In your JSP you can use your custom tag with all the functionality that comes with Spring's own tag

<%@ taglib uri="http://sandbox.local/common.tld" prefix="common" %>

<a href="product/edit.do"><common:message code="add.product" /></a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top