lundi 7 janvier 2019

getting 400 error for checkboxs in spring mvc


I'm trying to submit records in checkbox as an Object array.
This is the original model object.

Language.java

public class Language {
    private long langid;
    private String language;

    //getters setters
    --
}    

This is the object that using language as a array field.

Customer.java

public class Customer {

    private long id;
    private String firstname;
    private String secondname;
    private int age;
    private String address;
    private Country country;
    private Language[] language;

    public Customer() {
    }
     public Customer(String firstname, String secondname, int age, String 
       address, Country country, Language[] language) {
        this.firstname = firstname;
        this.secondname = secondname;
        this.age = age;
        this.address = address;
        this.country = country;
        this.language = language;
    }

     // other getter setters
     ...
     public Language[] getLanguage() {
        return language;
    }

    public void setLanguage(Language[] language) {
        this.language = language;
    }
}    

This is the controller class that return the request.

FormController.java

@Controller
@Transactional
@RequestMapping("/form")
public class FormController {

    @Autowired
    CustomerDAO customerDAO;

    @Autowired
    LanguageDAO langaugeDAO;

    @RequestMapping("/customer")
    public void addCustomer() {
    }

    @RequestMapping(value = "/customer", method = RequestMethod.POST)
    public String prosCustomer(Model model, @ModelAttribute() Customer 
    customer) {

        int result = customerDAO.saveCustomer(customer);

        if (result > 0) {
            int result2 = langaugeDAO.saveCustLang(customer.getLanguage(), customer.getId());

            if (result2 > 0) {
                model.addAttribute("msg", customer.getFirstname() + " successfully created");
            } else {
                model.addAttribute("msg", "customer registration failed.");
            }
        } else {
            model.addAttribute("msg", "customer registration failed.");
        }

        return "form/home";
    }
      @ModelAttribute("defaultcustomer")
      public Customer defaultCustomer() {

      Customer c = new Customer();
      return c;
      }

     @ModelAttribute("languages")
     public List<Language> languageList() {
     List<Language> list = new ArrayList<>();
     list = langaugeDAO.languageList();
     return list;
     }

}

And finally this is the view.

customer.jsp

 <form:form method="post" modelAttribute="defaultcustomer">
            name :      <form:input path="firstname"/>
            <br/>
            <form:hidden path="secondname"  />
            Age          <form:input path="age"/>
            <br/>
            Address  <form:textarea path="address"/>
            <br/>
            Country
            <form:select path="country.countryid">
                <c:forEach items="${countries}" var="country">
                    <form:option value="${country.countryid}">${country.country}</form:option>
                </c:forEach>
            </form:select>
            <br/>
            Languages: 
        <c:forEach items="${languages}" var="language">
            ${language.language} : <form:checkbox value="${language.langid}" 
            path="language"  />
        </c:forEach>
            <br/> 
            <input type="submit" value="submit"/>
        </form:form>

When I search customer.jsp through http://localhost:8080/mvcquick/form/customer everything is working.

customer.jsp view

But somehow when I submit the form I'm getting 404 error.

404 error

And I can't figure out what is the issue here. Please help. Thanks.




Aucun commentaire:

Enregistrer un commentaire