Spring property editors only work with forms?

I am working on a Spring application that is just looking for a dataset for things that meet certain criteria. There are two main types: one simple form, which allows the user to customize the search criteria, and the other displays the results in a table.

One of the search fields is a closed set of parameters (about 10). In the code below, I want to treat this as an enum class. The web form includes a drop-down menu that allows the user to select a parameter from this set. I used the form: select for this, filled with a set of lines that describe the values.

To separate presentation and business logic, the enum class is not aware of these lines, so I created a Property Editor to convert between them. When I load the form, the select control is set to the line associated with the enumeration value that I gave it; when submitting the form, the string is converted back to my enum type. It all works fine.

For the results page (which is not a form), I just add data to display in ModelMap. At this point, I must explicitly convert my enumeration type to a string before adding it to the map. What I would like to do is simply add an enumeration to the map and make the property editor convert it for me in the background, as well as for the form. Although I can’t understand. Is it possible to do this? Maybe I missed something really obvious?

+3
source share
1 answer

You can use spring tablib

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

And use conversion markup

<!--If you have a command which command name is account-->
<!--Default command name used in Spring is called command-->
<spring:bind path="account.balance">${status.value}</spring:bind>

or

<spring:bind path="account.balance">
    <spring:transform value="${account.balance}"/>
</spring:bind>

or

<!--Suppose account.balance is a BigDecimal which has a PropertyEditor registered-->
<spring:bind path="account.balance">
    <spring:transform value="${otherCommand.anotherBigDecimalProperty}"/>
</spring:bind>

About the value attribute

The value can be a simple value to convert (a hard-coded String value to a JSP or a JSP expression), or an EL JSP expression to evaluate (converting the result of the expression). Like all Spring JSP tags, this tag is able to independently parse EL expressions in any version of JSP.

Its API

Provides conversion of variables to String using the corresponding custom PropertyEditor from BindTag (can only be used inside BindTag)

MultiActionController, Dummy Command :

public class Command {

    public BigDecimal bigDecimal;
    public Date date;
    /**
      * Other kind of property which needs a PropertyEditor
      */

    // getter and setter's

}

MultiActionController

public class AccountController extends MultiActionController {

    @Autowired
    private Repository<Account, Integer> accountRepository;

    public AccountController() {
        /**
          * You can externalize this WebBindingInitializer if you want
          *
          * Here it goes as a anonymous inner class
          */
        setWebBindingInitializer(new WebBindingInitializer() {
            public void initBinder(WebDataBinder dataBinder, WebRequest request) {
                binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, numberFormat, true));
                binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
            }
        });
    }

    public ModelAndView(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView()
                   .addAllObjects(
                       createBinder(request, new Command())
                      .getBindingResult().getModel())
                   .addObject(accountRepository.findById(Integer.valueOf(request.getParameter("accountId"))));
    }

}

JSP

<c:if test="{not empty account}">
   <!--If you need a BigDecimal PropertyEditor-->
   <spring:bind path="command.bigDecimal">
       <spring:transform value="${account.balance}"/>
   </spring:bind>
   <!--If you need a Date PropertyEditor-->
   <spring:bind path="command.date">
       <spring:transform value="${account.joinedDate}"/>
   </spring:bind>
</c:if>

, PropertyEditor, .

+3

Source: https://habr.com/ru/post/1750553/


All Articles