Results 1 to 3 of 3

Thread: Read ModelAttribute on jsp page

  1. #1
    Join Date
    Jan 2012
    Posts
    21

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2012
    Posts
    21

    Default

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

    About your coment:
    Quote 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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •