我想在JSP页面中进行验证jsr 303。当我正确填写时,一切正常。保留空值时,输出错误。
错误是java.lang.NoSuchMethodError: javax.el.ExpressionFactory.newInstance()Ljavax / EL / ExpressionFactory; 在 org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm。(InterpolationTerm.java:59) 在 org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateExpression(ResourceBundleMessageInterpolator.java:341) 在 org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateMessage(ResourceBundleMessageInterpolator.java:279) 在 org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:194) 在 org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:50) 在 org.hibernate.validator.internal.engine.ValidationContext.interpolate(ValidationContext.java:362)...
@Controller
public class ProductCreateController {
private static final Logger log = Logger.getLogger(ProductCreateController.class);
@Autowired
private ProductService productService;
@Autowired
private SaleService saleService;
@Autowired
private UserService userService;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public String createCourse(Model model, HttpSession session,
HttpServletRequest request) {
model.addAttribute("eMail", session.getAttribute("eMail"));
if (session.getAttribute("name") == null) {
return "redirect:/login";
} else {
model.addAttribute("eMail", session.getAttribute("eMail"));
User user = userService.findUserByName(session.getAttribute("name")
.toString());
CreateProductDTO createProductDTO = new CreateProductDTO();
model.addAttribute(createProductDTO);
return "create";
}
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String createCoursePost(Model model, HttpSession session,
HttpServletRequest request, @Valid CreateProductDTO createProductDTO,
BindingResult result) {
model.addAttribute("eMail", session.getAttribute("eMail"));
if (result.hasErrors()) {
System.out.println("hasErrors");
return "create";
} else {
model.addAttribute("eMail", session.getAttribute("eMail"));
Product product = new Product();
product.setName(createProductDTO.getName());
product.setDescription(createProductDTO.getDescription());
product.setLinks(createProductDTO.getLinks());
product.setCategory(createProductDTO.getCategory());
product.setCountry(createProductDTO.getCountry());
product.setPrice(createProductDTO.getPrice());
product = productService.createProduct(product);
return "redirect:/products";
}
}
}
CreateProductDTO.java
public class CreateProductDTO {
@NotEmpty
private String name;
@NotEmpty
private String category;
@NotEmpty
private String description;
@NotEmpty
private String links;
@NotEmpty
private String country;
@NotNull
private int price;
public CreateProductDTO() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
JSP页面
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="./css/style.css"/>
<style>
.error {
color: #ff0000;
}
.errorblock {
color: #000;
background-color: #ffEEEE;
border: 3px solid #ff0000;
padding: 8px;
margin: 16px;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>
Create a new Product
<div class="logout">
<span id="currentUserLogin">
${eMail}
</span>
<a href="logout.html">
<i class="icon-off"></i>
</a>
</div>
</h1>
</header>
<form:form commandName ="createProductDTO" method="POST">
<form:errors path="*" cssClass="errorblock" element="div" />
<fieldset>
<div class="control-group">
<label class="control-label">Name</label>
<div class="controls">
<form:input path="name" />
<form:errors path="name" cssClass="error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Category</label>
<div class="controls">
<form:input path="category" />
<form:errors path="category" cssClass="error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<form:input path="description" />
<form:errors path="description" cssClass="error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Price</label>
<div class="controls">
<form:input path="price" />
<form:errors path="price" cssClass="error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Country</label>
<div class="controls">
<form:input path="country" />
<form:errors path="country" cssClass="error" />
</div>
</div>
<div class="control-group">
<label class="control-label">Links</label>
<div class="controls">
<form:input path="links" />
<form:errors path="links" cssClass="error" />
</div>
</div>
<div class="form-actions">
<button id="createButton" name="createButton" class="btn btn-primary" type="submit">Create</button>
</div>
</fieldset>
</form:form>
<a class="btn" href="products.html">Cancel</a>
</div>
讯息
NotEmpty.createProductDTO.name =名称是必需的!
NotEmpty.createProductDTO.category =类别是必需的!
NotEmpty.createProductDTO.description =描述是必需的!
NotEmpty.createProductDTO.links =需要链接!
NotEmpty.createProductDTO.country =国家/地区是必需的!
NotNull.createProductDTO.price =价格是必需的!
弹簧-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.nhl.web" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- bind your messages.properties -->
<bean class="org.springframework.context.support.ResourceBundleMessageSource"
id="messageSource">
<property name="basename" value="messages" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>