NNikN
NNikN

Reputation: 3850

formbackingobject not working Spring for the jsp

When I call formBackingObject method to set the default value it does not show anything in the input field of First Name.

The JSP and formBackingObject Code is written below.

SimpleFormController subclass java code

protected Object formBackingObject(HttpServletRequest request)
        throws Exception {
    //This does not work
    User u=new User();
    u.setFname("Roger");
    return u;
}


protected ModelAndView onSubmit(Object obj) throws Exception {
    //This works when submit button is clicked,I get the user entered values
    User u=(User)obj;
    userService.addUser(u);
    return new ModelAndView(getSuccessView(), "user", u);
}

JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/registrationcss.css">
<title>Insert title here</title>
</head>
<body>
    <form method="post">

        <p>
            First Name <input type="text" name="fname"></input>
        </p>
        <p>
            Last Name <input type="text" name="lname"></input>
        </p>
        <p>
            Your E-mail <input type="text" name="email"></input>
        </p>
        <p>
            Password <input type="password" name="password"></input>
        </p>
        <p>
            Re-enter Your Password <input type="password" name="repassword"></input>
        </p>
        <p>
            Gender</br> <input type="radio" name="gender" checked>Male</input> <input
                type="radio" name="gender">Fe-Male</input> <input type="radio"
                name="gender">Other</input>
        </p>
        <p>
            <input type="Submit" value="Sign Up" /input>
        </p>
    </form>
</body>
</html>

Upvotes: 0

Views: 234

Answers (1)

NNikN
NNikN

Reputation: 3850

Even though onSubmit method gives me the object from the FORM, the other way is not possible unless form-taglib is used.

Form-taglib is required for binding the data to the JSP.

The reference of the form tag lib can be found here.

Modifying the JSP to the one shown below, solved my issue.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/registrationcss.css">
<title>Insert title here</title>
</head>
<body>
    <form:form method="post" commandName="user">

        <p>
            First Name <form:input path="fname"></form:input>
        </p>
        <p>
            Last Name <form:input path="lname"></form:input>
        </p>
        <p>
            Your E-mail <form:input path="email"></form:input>
        </p>
        <p>
            Password <form:password path="password"></form:password>
        </p>
        <p>
            Re-enter Your Password <form:password path="repassword"></form:password>
        </p>
        <p>
            Gender<br> <form:radiobutton path="gender" label="Male"/> <form:radiobutton
                path="gender" label="Fe-Male"/> <form:radiobutton
                path="gender" label="Other"/>
        </p>
        <p>
            <input type="Submit" value="Sign Up" />
        </p>
    </form:form>
</body>
</html>

Upvotes: 1

Related Questions