1

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>
Luke
  • 1,218
  • 1
  • 24
  • 40
  • 1
    Your question is ambiguous. To start, does `${person}` alone print something or nothing? (it's expected that `Person#toString()` is being printed). If it already prints nothing, then you need to take a step back and focus on how you're preparing the model for the view, and not focus on model's properties. – BalusC Apr 12 '16 at 18:26
  • Accessing the properties from the Person object in the JSP (${person.firstName} for example). Edited question to describe. Thanks. – Luke Apr 12 '16 at 18:41
  • Oh, now I got it. You basically want `${person.firstName} ${person.middleName} ${person.lastName}` in a single call such as `${person.displayName}`? And you've a `public String getDisplayName() { return firstName + " " + middleName + " " + lastName; }` method which doesn't seem to print anything? Why didn't you show the problematic method anywhere? – BalusC Apr 12 '16 at 18:45
  • @Ali, yes typo, thanks, fixed. – Luke Apr 12 '16 at 18:45
  • @BalusC, actually I was doing it wrong, ${person.getDisplayName()} is working for me. Editing question to reflect that (still wondering if this is the 'best' way to handle this type of logic). – Luke Apr 12 '16 at 20:22
  • You don't need to write out full method names for getters. As to the new question, Stack Overflow is not the right place for overly broad and opinion based questions like "What is the best way?". Use a discussion forum or chatbox for that. Stack Overflow is only for technical problems whereby you post problematic code right away and describe the expected and unexpected behavior in detail. You will get an answer with the right way instead of "best" way. – BalusC Apr 13 '16 at 07:25

1 Answers1

1
Display Name: ${person.displayName}

Should work as long as you have a public String getDisplayName() method in your Person class and it's the simple and reasonable way of doing that.

Ali Seyedi
  • 1,758
  • 1
  • 19
  • 24