Question

I have form:

     <form:form commandName="entity" role="form">
       <myCustomTag path="field_id" />
     </form:form>

How I can find out what is commandName inside my custom tag? (MyCustomTag.tag file below)

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

     <%@ attribute name="path" required="true" rtexprvalue="true"
         description="Name of corresponding property in bean object"%>

     <div class="form-group">
         <form:label path="${path}"><spring:message code="mylabelformat_${what_to_put_here?}_${path}"/></form:label>
         <form:input path="${path}" class="form-control" placeholder="${placeholder}" />
         <form:errors class="invalid" path="${path}" />
     </div>
Was it helpful?

Solution

For your use-case you can use nestedPath request attribute, which is being set by the <spring:form> tag:

<spring:message code="mylabelformat_${nestedPath}${path}"/>

The translation code would then be (for your example):

mylabelformat_entity.field_id

OTHER TIPS

The form attribute commandName is referring to a model attribute (you could use the equivalent form attribute modelAttribute). Since model attributes are just request attributes when the view is rendered, entity is actually just a request attribute.

In your custom tag, just get it

MyEntity entity = (MyEntity) request.getAttribute("entity");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top