How to handle options in JSPX?

We have several domain objects with fields with a zero value. We read that hibernation requires an "unprocessed" object to display it correctly, so our recipients return options. Our domain objects are as follows:

public class User { private String firstName; private User boss; public Optional<String> getFirstName(){ .... } public Optional<User> getBoss() { ... } } 

But now we have problems resolving / linking these fields in .jspx files. (Both displays, as well as form input fields). For primitive types and strings, we could get around this by specifying a custom OptionToStringConverter.

However, the problem is null references for other domain objects.

We examined several options, but are not really satisfied with any of them:

  • Defining custom converters for all objects and domain types (will lead to many converters and does not work for input fields)
  • Definition of optional and optional getters for each domain object (1. duplicate code, 2. we want fields with zero values ​​to be clean, 3. do not feel clean to access optional and optional fields differently)
  • Definition of a show command that returns "raw" or null (duplicate code)
  • Defining a custom tag that handles additional parameters (when a domain object becomes optional, we will need to change the tagx)

We wondered if there is a good and clean way to enable options in jspx.

+5
source share
1 answer

In terms of form binding, Spring handles this pretty well: for example. It maps the first value below Optional.empty() , and the second and third to the corresponding Optional<Boolean> .

 <form:select path="anOptionalBoolean"> <form:option value="" label="Not Specified"/> <form:option value="true" label="Yes"/> <form:option value="false" label="No"/> </form:select> 

Regarding the display of values, I did not find anything better than just using .get and .present , etc. in the expressions of EL. This makes for rather cumbersome code.

0
source

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


All Articles