Announcement Announcement Module
Collapse
No announcement yet.
Read ModelAttribute on jsp page Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read ModelAttribute on jsp page

    I have a javax.el.PropertyNotFoundException accessing from a jsp page to an object mapped with @ModelAttribute.

    I have the following (simplified) code
    Code:
        @ModelAttribute("homePage")
        public HomePage populateHomePage(HttpServletRequest request) {
        		homePage.setName("x");
        		homePage.setSurname_1("y");
        		homePage.setSurname_2("z");
        	return homePage;
        }
    
        
        @ModelAttribute("HelloWorld")
        public String populateHelloWorld(HttpServletRequest request) {
        	return "HelloWorld";
        }
    And in the jspx

    Code:
    <p>${homePage.Name}</p> 
    <p>${HelloWorld}</p>
    The HelloWorld attribute works ok, but I can't access to the property "Name" on "homePage" object
    The object "homepage" contains the following code:
    Code:
    	private String Name;
    	public String getName() {
    		return Name;
    	}
    The error raised is:
    javax.el.PropertyNotFoundException: Property 'Name' not found on type com.foo.bar.domain.HomePage

    What is the correct way to access to homePage.Name?

  • #2
    I suggest a read of the JavaBean Specification (as well as the spring reference guide). Property names are the name of the set method, without set and lowercased first letter (in general for the full algorithm I suggest the java beans specification).

    Another note is that your controller is flawed NEVER expose an internal bean (HomePage) to multiple requests. Create a new instance of that bean each time needed (remember that your controller is a singleton now imagine multiple users accessing this controller).... Although not a problem in this controller (same data is written each time) but if there is dynamic content this can become a problem...

    Comment


    • #3
      Many thanks.
      I'll spend the weekend reading some basic documentation...

      About your coment:
      Originally posted by Marten Deinum View Post
      Another note is that your controller is flawed NEVER expose an internal bean (HomePage) to multiple requests.
      I think that is a problem of a fast copy/paste of a simplified version of the code just for posting.

      The real code looks like

      Code:
          @ModelAttribute("homePage")
          public HomePage populateHomePage(HttpServletRequest request) {
          	HomePage homePage=new HomePage();
      ...
          		homePage.setName("z");
          		homePage.setSurname_1("x");
          		homePage.setSurname_2("y");
      ..
          	return homePage;
          }
      Is this implementation correct?

      Comment

      Working...
      X