简体   繁体   中英

In SpringMVC, after form is submitted, PropertyNotFoundException Encountered

This is Controller .

@Controller
@RequestMapping("/Register")
public class RegistrationController {

    @RequestMapping(method=RequestMethod.GET)
    public String showForm(ModelMap model)
    {
        System.out.println("..In showform()..");
        UserBean userbean=new UserBean();
        model.addAttribute("USER", userbean);
        return "Test";
    }
    @RequestMapping(value="/Register" ,method=RequestMethod.POST)
    public String processForm(@ModelAttribute("USER") UserBean user)
    {   
            System.out.println("UserDetails are :"+ user.getFirstName());
            System.out.println("UserDetails are :"+ user.getAge());
            return "Success";
    }

}

This is Test.jsp

<form:form method="POST" modelAttribute="USER"  action="Register">
<table>

<tr><td>Name</td> <td><form:input path="FirstName"/></td></tr>
<tr><td>Age</td> <td><form:input path="Age"/></td></tr>
<tr><td><input type="submit" value="Submit"/></td></tr>
</table>
</form:form>

This is Success.jsp

<body>
    <table>
        <tr>
            <td>User Name :</td>
            <td><core:out value="${USER.FirstName}" /></td>
        </tr>
        <tr>
            <td>Age :</td>
            <td><core:out value="${USER.Age}" /></td>
        </tr>
    </table>
</body>

And this is the com.beans.UserBean

public class UserBean {

    private String FirstName;

    private int Age;

    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        this.FirstName = firstName;
    }
    public int getAge() {
        return Age;
    }
    public void setAge(int age) {
        this.Age = age;
    }
}

Now after i Clik on SUBMIT button the following error is Encountered. Dont know why. I have the proper getter/setters in the form backing bean.

javax.el.PropertyNotFoundException: Property 'FirstName' not found on type com.beans.UserBean

The getter method public String getFirstName() is the equivalent for a instance variable named: firstName not FirstName .

Since variables starting with lowercase is a Java convention , Spring MVC will search for instance variables named : firstName, age and NOT FirstName, Age . The exception: javax.el.PropertyNotFoundException is thrown by Spring because he couldn't find any method accesors for FirstName and Age .

All you have to do is to change those instance variables names so they may start with a lowercase : firstName, age .

    private String FirstName;

    private int Age;

This is really a bad practice, define the first character in your variable with lower case. Make it lower case and try again.

rename you property to firstName with lower f. because of namening convention and try again (also in jsp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM