Reputation: 5157
I am trying to create a program where I am getting the error
Edit.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:if test="${session_username != null }">Hello ${session_username}!</c:if>
<font face="verdana" size="2">
${welcomeMessage} <BR><BR>
<form:form action="${pageContext.request.contextPath}/editemployee" method="POST" modelAttribute="editForm">
<form:errors path="studenterrors" />
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Edit</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td>Mobile No.</td>
<td><form:input path="mobileno" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthdate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</font>
</body>
</html>
LoginSuccess.java
package java4s;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java4s.EmployeeService;
@Controller
public class LoginSuccessController {
@Autowired
EmployeeService emp_service;
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @ModelAttribute("editForm") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
}
Whenever I click on submit button, I get HTTP Status 400 -
error. But when I remove @ModelAttribute("editForm") Employee employee
from the function EditSaveMyInfo
, the error doesnt appears?
How can I solve the error?
EDIT
Employee.java
package java4s;
import java.util.Date;
public class Employee {
private String userid;
private String firstname;
private String lastname;
private String mobileno;
private String username;
private String password;
private String email;
private Date birthDate;
private String profession;
private String studenterrors;
public String getUserid()
{
return userid;
}
public void setUserid(String userid)
{
this.userid = userid;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getMobileno()
{
return mobileno;
}
public void setMobileno(String mobileno)
{
this.mobileno = mobileno;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Date getBirthdate()
{
return birthDate;
}
public void setBirthdate(Date birthDate)
{
this.birthDate = birthDate;
}
public String getProfession()
{
return profession;
}
public void setProfession(String profession)
{
this.profession = profession;
}
public String getstudenterrors()
{
return studenterrors;
}
public void setstudenterrors(String studenterrors)
{
this.studenterrors = studenterrors;
}
}
Upvotes: 0
Views: 2032
Reputation: 3109
Your problem is that the binding between the form and the model(Employee) was failed.
Change your controller (Just read it very carefully, you will get a picture about how binding actually works) to :
package java4s;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java4s.EmployeeService;
@Controller
public class LoginSuccessController {
@Autowired
EmployeeService emp_service;
//Give you a form (edit.jsp) as response after binding happens
@RequestMapping(value = "/employeeform", method=RequestMethod.POST)
public ModelAndView doBinding() {
ModelAndView mv = new ModelAndView("edit", "employee", new Employee());
return mv;
}
// Now the form comes here after it gets submitted.
// And you can start populating the fields
@RequestMapping(value = "/saveemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @ModelAttribute("employee") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
}
And change your edit.jsp
to :
modelAttribute="employee"
, because I mapped it with new ModelAndView("edit", "employee", new Employee());
in doBinding(...)
controller) action=..
path to /saveemployeeAdd <context:annotation-config />
there in your spring-servlet.xml also xmlns:context="http://www.springframework.org/schema/context"
in <beans..
to enable it.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:if test="${session_username != null }">Hello ${session_username}!</c:if>
<font face="verdana" size="2">
${welcomeMessage} <BR><BR>
<form:form action="${pageContext.request.contextPath}/saveemployee" method="POST" modelAttribute="employee">
<form:errors path="studenterrors" />
<table>
<tr>
<td colspan="2" align="center">Spring MVC Form Demo - Edit</td>
</tr>
<tr>
<td>User Name</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>First Name</td>
<td><form:input path="firstname" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><form:input path="lastname" /></td>
</tr>
<tr>
<td>Mobile No.</td>
<td><form:input path="mobileno" /></td>
</tr>
<tr>
<td>Password</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>Email</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td>BirthDate (mm/dd/yyyy)</td>
<td><form:input path="birthdate" /></td>
</tr>
<tr>
<td>Profession</td>
<td><form:select path="profession" items="${professionList}" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</font>
</body>
</html>
Now request the form with --> ...../employeeform
EditSaveMyInfo()
(action = "/saveemployee")Note : I don't expect my answer to directly solve your problem (because the errors could come from somewhere else in your codes), but I just want you to get the binding process in spring mvc.
Upvotes: 1
Reputation: 3
HTTP STATUS 400 means some thing wrong with the parameters passing from front end to back end.
@ModelAttribute
tells get value from session, I suppose to use @RequestParam
should be used at your situation which is getting parameters from http request.
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestParam("employee") Employee employee) {
model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
<form:form action="${pageContext.request.contextPath}/editemployee" method="POST">
Upvotes: 0
Reputation: 695
use @RequestBody @Valid Employee employee
instead of @ModelAttribute("editForm") Employee employee
and you controller become
@RequestMapping(value = "/editemployee", method=RequestMethod.POST)
public ModelAndView EditSaveMyInfo(ModelMap model, @RequestBody @Valid Employee employee,BindingResult result) {
if (result.hasErrors()) {
throw new BadRequestException();
} model.addAttribute("message", "Information Updated");
return new ModelAndView("EditSuccess",model);
}
binding result object is used for catch bad request exception. this is the spring standard
Upvotes: 1