I am working on a Java project using Spring MVC framework and JSP for my views. I'm wondering about how best to represent fields that are derived from properties in the bean I'm using as a model attribute. In my example a DisplayName field that is built up from all the various name fields a person might have (first, last, middle, prefix, etc).
For example, if I have a Person bean that looks something like this:
public class Person {
private String lastName;
private String firstName;
.
.
.
**A bunch more name fields here
.
.
.
**All the getters and setters here
.
.
.
public String getDisplayName() {
//Simple example
return this.firstName + " " + this.lastName;
}
}
So if I wanted my display name to combine all these fields with some logic, and then display that on the JSP how should I build that up?
Should I build a "displayName" String in my controller and then pass it in as an additional model attribute?
Should I create a method that builds this in the bean (see example above) and then just access that from the JSP? Using the following seems to work for me:
${person.getDisplayName()}
What's the proper way to handle this pattern?
Edit additional information In my controller I'm passing in a Person object as a model attribute
@RequestMapping(value = "/path", method = RequestMethod.GET)
public String getMethod(final Locale locale, final Model model, final HttpServletRequest request) {
Person myPerson = getThePersonFromSomewhere();
model.addAttribute("person", myPerson);
return "the view";
}
And then in the view JSP I access it like this
<div>
First Name: ${person.firstName}
Display Name: ${wondering how to do this}
</div>