0

I am getting this exception :

    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'RegistrationBean' available as request attribute

This is my controller class

@Controller
public class TenantRegistrationController {

    @Autowired
    private TenantService tenantService;

    @Autowired
    private UserService userService;

RegistrationBean is the bean class which is passed from the jsp as model attribute.

    @RequestMapping(value="/registration", method= RequestMethod.POST)
    public String addNewTenant(@ModelAttribute("RegistrationBean")RegistrationBean registrationBean, 
            BindingResult result, Map<String,Object> map)
    {
        Tenant tenant = new Tenant();

        tenant.setTenant_name(registrationBean.getTenantName());
        tenant.setTenant_email(registrationBean.getTenantEmail());
        tenant.setAddress(registrationBean.getTenantAddress());
//      tenant.setReminder_email(Integer.parseInt(registrationBean.getReminderMail()));
        tenant.setReminder_email(registrationBean.getReminderMail());
        tenant.setThankyou_email(registrationBean.getThankyouMail());

        CurrencyFormat cf = new CurrencyFormat();
        cf.setCurrencyFormatId(registrationBean.getCurrencyFormatId());
        tenant.setTenantCurrencyFormat(cf);

        DateFormat df = new DateFormat();
        df.setDateFormatId(registrationBean.getDateFormatId());
        tenant.setTenantDateFormat(df);

        tenant.setRegistration_date(registrationBean.createRegistrationDate());

        tenantService.addTenant(tenant);


        User user = new User();
        user.setName(registrationBean.getUserName());
        user.setAddress(registrationBean.getUserAddress());
        user.setUser_name(registrationBean.getUserLoginName());
        user.setDesignation(registrationBean.getUserDesignation());
        user.setPassword(registrationBean.getUserPassword());
        user.setEmail(registrationBean.getUserEmail());
        user.setContact_number(registrationBean.getUserContactNo());

        Role urole=new Role();
        urole.setRole_id(1);
        user.setUser_role(urole);


        userService.addUser(user);

        map.put("tenant_name", tenant.getTenant_name());
        return "tenant_registration_success";

    }


    @RequestMapping("/addANewTenant")
    public String showAddTenantPage(Map<String, Object> map)
    {
        /*map.put("item", new Item());
        map.put("itemList", itemService.listitems());*/
        return "add_tenant_details";
    }

}

This is my JSP file

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<%@ 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">
<title>Tanent Registration and Tanent Administrator Registration</title>
<style>
.error {
color: #ff0000;
font-style: italic;
}
</style>
</head>
<body>

I am specifying the bean named "RegistrationBean" as modelAttribute

<h3>Tenant Registration</h3>
<form:form method="post" action="/MTInvoiceManagement/tenant/registration.html" modelAttribute="RegistrationBean">
<table align="left">
       <tr>
              <td>Tenant Name</td>
              <td><form:input path="tenantName" type="text" /></td>
              <td><form:errors path="tenantName" cssClass="error"></form:errors></td>
       </tr>

       <tr>
              <td>Tenant Email</td>
              <td><form:input path="tenantEmail" type="text" /></td>
              <td><form:errors path="tenantEmail" cssClass="error"/></td>

       </tr>
       <tr>
              <td>Tenant Address</td>
              <td><input name="tenantAddress" type="text" /></td>
              <td><form:errors path="tenantAddress" cssClass="error" ></form:errors></td>
       </tr>

       <tr>
              <td>Reminder Mail</td>
              <td>
                     <form:select path="reminderMail">                     
                     <form:option value="0" label="No"  />
                     <form:option value="1" label="Yes" />
                     </form:select>
              </td>
              <td><form:errors path="reminderMail" cssClass="errors"></form:errors></td>
       </tr>


       <tr>
              <td>Thank You Mail</td>
              <td>
                     <form:select path="thankyouMail">
                     <form:option value="0" label="No"></form:option>
                     <form:option value="1" label="Yes"></form:option>
                     </form:select>
              </td>
              <td> <form:errors path="thankyouMail" cssClass="errors"></form:errors>  </td>
       </tr>

       <tr>
              <td>Date Format</td>
              <td>
                     <form:select path="dateFormatId">
                     <form:option value="1" label="DD-MM-YY"></form:option>
                     <form:option value="2" label="MM-DD-YY"></form:option>
                     <form:option value="3" label="YYYY-MM-DD"></form:option>
                     </form:select>
              </td>
              <td><form:errors path="dateFormatId" cssClass="errors"></form:errors></td>
       </tr>

       <tr>
              <td>Currency Format</td>
              <td>
                     <form:select path="currencyFormatId">
                     <form:option value="1" label="$"></form:option>
                     <form:option value="2" label="£"></form:option>
                     <form:option value="3" label="¥"></form:option>
                     <form:option value="4" label="€"></form:option>
                     <form:option value="5" label="₨"></form:option>
                     </form:select>
              </td>
              <td><form:errors path="currencyFormatId" cssClass="errors" ></form:errors></td>
       </tr>

       <tr>
            <td> <h3> Tanent Administrator Details  </h3>  </td>
       </tr>

       <tr>
              <td>Tanent Admin Name</td>
              <td><form:input path="userName" type="text"/> </td>
              <td> <form:errors path ="userName" cssClass="errors"></form:errors> </td>
       </tr>

       <tr>
              <td>Tanent Admin User Login Name</td>              
              <td><form:input path="userLoginName" type="text"/> </td>
              <td> <form:errors path ="userLoginName" cssClass="errors"></form:errors> </td>
       </tr>
       <tr>
              <td>Tanent Admin User Password</td>              
              <td><form:input path="userPassword" type="password"/> </td>
              <td> <form:errors path ="userPassword" cssClass="errors"></form:errors> </td>
       </tr>
       <tr>
              <td>Tanent Admin User Email</td>              
              <td><form:input path="userEmail" type="text"/> </td>
              <td> <form:errors path ="userEmail" cssClass="errors"></form:errors> </td>
       </tr>
       <tr>
              <td>Tanent Admin User Contact No</td>              
              <td><form:input path="userContactNo" type="text"/> </td>
              <td> <form:errors path ="userContactNo" cssClass="errors"></form:errors> </td>
       </tr>
              <tr>
              <td>&nbsp;</td>
              <td>
                     <input name="Submit" type="submit" value="submit" />

              </td>
       </tr>
</table>
</form:form>

</body>
</html>

Please help me!!

user2251798
  • 323
  • 1
  • 3
  • 5

1 Answers1

0

you have to put your RegistrationBean into model when displaing the form. You can do it in the same way as you are doing it in your public String addNewTenant method

 @RequestMapping("/addANewTenant")
    public String showAddTenantPage(Map<String, Object> map,@ModelAttribute("RegistrationBean")RegistrationBean registrationBean)

if you need same special initialisation of your form object you can define method with @ModelAttribute annotation whichwil handle the form object initialisation.

@ModelAttribute
public RegistrationBean getRegistrationBean() {

}
Josef Prochazka
  • 1,273
  • 2
  • 9
  • 28