Java Spring MVC-POJO投掷和#x415;来自无效spring的异常:表单信息如何在控制器中捕获
我创建了POJO,该POJO验证产品名称不少于3个字符。在JSP中,使用spring:form在提交时创建对象。当我发送一个空白表单“name”时,POJO会抛出eXception。控制器在POST方法中具有@Modelattribute product,但未创建product对象,应用程序崩溃,POJO除外。我希望捕获此异常并将其作为错误消息添加回用户。我做错了什么 POJO代码:Java Spring MVC-POJO投掷和#x415;来自无效spring的异常:表单信息如何在控制器中捕获,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我创建了POJO,该POJO验证产品名称不少于3个字符。在JSP中,使用spring:form在提交时创建对象。当我发送一个空白表单“name”时,POJO会抛出eXception。控制器在POST方法中具有@Modelattribute product,但未创建product对象,应用程序崩溃,POJO除外。我希望捕获此异常并将其作为错误消息添加回用户。我做错了什么 POJO代码: public class Product { private int id; private String na
public class Product {
private int id;
private String name;
private Department deparment;
public Product() {
}
// Create product from DB
public Product(int id, String name, Department deparment) {
this.id = id;
try {
setName(name);
setDeparment(deparment);
} catch (ProductException | DeparmentException e) {
e.getMessage();
e.printStackTrace();
}
}
// Create new product in DB
public Product(String name, Department deparment) {
try {
setName(name);
setDeparment(deparment);
} catch (ProductException | DeparmentException e) {
e.getMessage();
e.printStackTrace();
}
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) throws ProductException {
if (name.length() > 3) {
this.name = name;
} else {
throw new ProductException("Invalid name for product");
}
}
public Department getDeparment() {
return deparment;
}
public void setDeparment(Department deparment) throws DeparmentException {
if (deparment != null) {
this.deparment = deparment;
} else {
throw new DeparmentException("Invalid deparment for product");
}
}
JSP代码:
<c:if test="${not empty error}">
<p style="color: red">Error: ${error}</p>
</c:if>
<c:if test="${not empty success}">
<p style="color: green">${success}</p>
</c:if>
<springForm:form action="product" method="post" commandName="product">
<springForm:input type="text" placeholder="Име на продукта"
path="name" pattern=".{3,}" />
<br>
<br>
<select id="department" name="department">
<option value="0">Department</option>
<!-- Var mi e promenlivata items e masiva-->
<c:forEach var="department" items="${departments}">
<option value="${department.id}">${department.name}</option>
</c:forEach>
</select>
<button>Create</button>
</springForm:form>
错误:${Error}
${success}
部门
${department.name}
创造
控制器代码:
public class ProductController {
@RequestMapping(method = RequestMethod.GET, value = "/product")
public String showProductForm(Model viewModel) {
DepartmentDAO deparmentDAO = new DepartmentDAO();
List<Department> departments = new ArrayList<Department>();
try {
departments = deparmentDAO.selectAllDeparments();
} catch (DeparmentException e) {
e.printStackTrace();
}
viewModel.addAttribute("departments", departments);
viewModel.addAttribute("product", new Product());
return "product";
}
@RequestMapping(method = RequestMethod.POST, value = "/product")
public String addNewProduct(@ModelAttribute Product product, Model viewModel,@ModelAttribute("department") int departmentId, BindingResult result) {
DepartmentDAO departmentDAO = new DepartmentDAO();
ProductDAO productDAO = new ProductDAO();
if (departmentId == 0) {
viewModel.addAttribute("error", "Department is not selected.");
return showProductForm(viewModel);
}
try {
Department department = departmentDAO.getDepartmentById(departmentId);
product.setDeparment(department);
productDAO.addProduct(product);
} catch (ProductException | DeparmentException e) {
viewModel.addAttribute("error", e.getMessage());
e.printStackTrace();
return showProductForm(viewModel);
}
viewModel.addAttribute("success", "Въведохте успешно Продукта: <br>" + product.getName());
return showProductForm(viewModel);
}
公共类ProductController{
@RequestMapping(method=RequestMethod.GET,value=“/product”)
公共字符串showProductForm(模型视图模型){
DepartmentDAO deparmentDAO=新的DepartmentDAO();
List departments=new ArrayList();
试一试{
departments=departmentdao.selectAllDepartments();
}捕获(DeparmentException e){
e、 printStackTrace();
}
viewModel.addAttribute(“部门”,部门);
viewModel.addAttribute(“产品”,新产品());
退回“产品”;
}
@RequestMapping(method=RequestMethod.POST,value=“/product”)
公共字符串addNewProduct(@modeldattribute-Product-Product,Model-viewModel,@modeldattribute(“department”)int-departmentId,BindingResult){
DepartmentDAO DepartmentDAO=新的DepartmentDAO();
ProductDAO ProductDAO=新的ProductDAO();
如果(部门ID==0){
viewModel.addAttribute(“错误”,“未选择部门”);
返回showProductForm(视图模型);
}
试一试{
Department Department=departmentDAO.getDepartmentById(departmentId);
产品设置部门(部门);
productDAO.addProduct(产品);
}捕获(ProductException | DeparmentException e){
addAttribute(“错误”,例如getMessage());
e、 printStackTrace();
返回showProductForm(视图模型);
}
viewModel.addAttribute(“success”、“Съъъъъъъъъъъъъъъъъъ;
返回showProductForm(视图模型);
}
您不应该从setter抛出异常;如果抛出异常,Spring将无法填充模型以将其传递给控制器。
您应该对此使用验证;或者推出一个自定义验证,它可能会抛出您可以捕获的异常:
public void validate (Product p) throws ProductException() {...}
或者使用Spring提供的标准验证支持:
@RequestMapping(method = RequestMethod.POST, value = "/product")
public String addNewProduct(@ModelAttribute @Valid Product product, BindingResult result, Model viewModel,@ModelAttribute("department") int departmentId, BindingResult result) {...
有关更多信息,请参阅spring文档;我建议使用此方法,这是标准方法您不应该从setter抛出异常;如果这样做,spring将无法填充模型以将其传递给控制器。 您应该对此使用验证;或者推出一个自定义验证,它可能会抛出您可以捕获的异常:
public void validate (Product p) throws ProductException() {...}
或者使用Spring提供的标准验证支持:
@RequestMapping(method = RequestMethod.POST, value = "/product")
public String addNewProduct(@ModelAttribute @Valid Product product, BindingResult result, Model viewModel,@ModelAttribute("department") int departmentId, BindingResult result) {...
有关更多信息,请参阅spring文档;我建议使用此方法,这是标准方法