Search code examples
javaspringjspspring-formspring-messaging

How to reference a spring form input placeholder attribute value to a message source property?


Is it possible to retrieve a message from a .properties message-file using spring:message tag (spring tag library) within a spring-form:input tag (spring form tag library, used for data-binding).


src/main/resources > texts.properties (file)

testEntry=Test entry

src/main/webapp/WIN-INF/JSP > test.jsp

If I try to do it this way:

<%@taglib prefix='spring' uri='http://www.springframework.org/tags'%>
<%@taglib prefix='form' uri='http://www.springframework.org/tags/form'%>

<form:input path="test" placeholder='<spring:message code="testEntry">'/>

I get as a result <spring:message code="testEntry"> as a placeholder instead of Test entry


And if I try to do it this way:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix='spring' uri='http://www.springframework.org/tags'%>
<%@taglib prefix='form' uri='http://www.springframework.org/tags/form'%>

<c:set var="placeholder" value='<spring:message code="testEntry">'/>
<form:input path="test" placeholder='${placeholder}'/>

I get the same result. I understand the problem. But is there another way to do it within a JSP so that the message with the appropriate code appears as an attribute value of a spring-form tag?


Solution

  • You can use the var attribute of the message tag to assign the resolved message to a named variable. You can then use that variable as an EL variable.

    <spring:message code="testEntry" var="placeholder" />
    <form:input path="test" placeholder='${placeholder}'/>
    

    This assigns the resolved message for testEntry to a variable named placeholder which you can then use in the input tag.