3
我寫了一個關於Hibernate Validator的簡單例子,請參考官方文檔。Hibernate Validator EL表達式不起作用
Car.java的代碼段是下面:
@Size(min = 2, max = 14, message = "The license plate '${validatedValue}' must be between {min} and {max} characters long")
private String licensePlate;
@Min(value = 2, message = "There must be at least {value} seat${value > 1 ? 's' : ''}")
private int seatCount;
@DecimalMax(value = "350", message = "The top speed ${formatter.format('%1$.2f', validatedValue)} is higher "
+ "than {value}")
private double topSpeed;
CarTest.java的代碼段是下面:
@Test
public void licensePlateTest()
{
Car car = new Car(null, "A", 1, 400.123456, BigDecimal.valueOf(200000));
String message = validator.validateProperty(car, "licensePlate")
.iterator()
.next()
.getMessage();
assertEquals(
"The license plate must be between 2 and 14 characters long",
message
);
}
@Test
public void seatCountTest()
{
Car car = new Car(null, "A", 1, 400.123456, BigDecimal.valueOf(200000));
String message = validator.validateProperty(car, "seatCount").iterator().next().getMessage();
assertEquals("There must be at least 2 seats", message);
}
licensePlate的驗證信息是:「許可盤子'$ {validatedValue}'的長度必須在2到14個字符之間。「
seatCount的驗證消息是:「必須至少有兩個座位$ {value> 1?'s':''}。」
我們可以看到,EL表達式不起作用。關於我的項目pom.xml是:
<!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
我不知道爲什麼它不工作,任何人都可以幫助我?謝謝。
我想你從這裏得到它? https://github.com/hibernate/hibernate-validator/blob/master/documentation/src/test/java/org/hibernate/validator/referenceguide/chapter04/complete/CarTest.java Dit它從原始源代碼樹工作? – 2015-04-02 08:25:17
請爲實體的註釋添加Imports。 – 2015-04-02 10:48:46
嗨,彼得,你能提供更多的信息嗎? – 2015-04-02 15:01:14