gpt4 book ai didi

java - 我无法使用 JSR303 @Valid 显示错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:17 24 4
gpt4 key购买 nike

我尝试在 jsp 文件中显示错误消息,但没有成功。

这里是我的模型类,名为 EmployeeEntity.java

@Entity
@Table(name="EMPLOYEE")
public class EmployeeEntity
{
@Id
@Column(name="ID")
@GeneratedValue
private Integer id;

@Column(name="FIRSTNAME")
private String firstname;

@Column(name="LASTNAME")
private String lastname;

@NotEmpty
@Column(name="ADDRESS")
private String address;

@Column(name="EMAIL")
private String email;

@Column(name="TELEPHONE")
private String telephone;

@Column(name="USERNAME")
private String username;

@Column(name="PASSWORD")
private String password;


public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

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 getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}
}

这是我的jsp页面

<form:form method="post" commandName="employeeEntity" action="add">
<form:errors path="*" cssClass="errorblock" element="div" />
<div class="panel panel-primary">
<div class="panel-heading">
Employee Register Form
</div>
<div class="panel-body">
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.firstname" />
</label>
<div class="col-lg-10">
<form:input path="firstname" class="form-control" placeholder="first name" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.lastname"/>
</label>
<div class="col-lg-10">
<form:input path="lastname" class="form-control" placeholder="last name" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.address"/>
</label>
<div class="col-lg-10">
<form:textarea rows="5" path="address" class="form-control" placeholder="address" />
<form:errors path="address" cssClass="error" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.gender"/>
</label>
<div class="col-lg-10">
<label class="label-radio">
<form:radiobutton path="gender" value="male"/>
Male
</label>
<label class="label-checkbox">
<form:radiobutton path="gender" value="female" />
Female
</label>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.email"/>
</label>
<div class="col-lg-10">
<form:input path="email" class="form-control" placeholder="email" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.phonenumber"/>
</label>
<div class="col-lg-10">
<form:input path="telephone" class="form-control" placeholder="phone number" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.username"/>
</label>
<div class="col-lg-10">
<form:input path="username" class="form-control" placeholder="username" />
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">
<fmt:message key="label.password"/>
</label>
<div class="col-lg-10">
<form:password path="password" class="form-control" placeholder="password" />
<span class="help-block"></span>
</div>
</div>
</div>
<div class="panel-footer">
<input type="submit" class="btn btn-primary" value="<fmt:message key="button.submit" />" />
<input type="reset" class="btn btn-warning" value="<fmt:message key="button.reset" />" />
</div>
</div>

这是我的 xml 文件

<context:annotation-config />
<context:component-scan base-package="com.howtodoinjava.controller" />

<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />


enter code here
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl"></bean>
<bean id="carDAO" class="com.howtodoinjava.dao.CarDaoImpl"></bean>

<bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl"></bean>
<bean id="carManager" class="com.howtodoinjava.service.CarManagerImpl"></bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

这是我的 Controller 类

    @RequestMapping(value = "registerForm/add", method = RequestMethod.POST)
public String addEmployee(@Valid EmployeeEntity employee, BindingResult result){
if(result.hasErrors()) {
return "redirect:/registerForm/form";
} else {
employeeManager.addEmployee(employee);
return "redirect:/list";
}
}

我希望你能帮助我,我是 Spring Hibernate 的新手

谢谢,最好的问候

最佳答案

您需要有一个 validator 类(例如 EmployeeValidator),它实现包含验证逻辑的 org.springframework.validation.Validator 接口(interface)。除此之外,您还需要在 Controller 类中绑定(bind)该 validator ,其中 addEmployee 方法的定义如下:

假设您通过 Controller 类中的变量employeeValidator 声明了 EmployeeValidator(您可以通过在 beans 配置 xml 中声明它来 Autowiring ),那么您需要在 Controller 类中绑定(bind) validator ,如下所示。

@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(employeeValidator);
}

然后 validator 将自动调用并在 BindingResult 中设置错误(即方法签名中的结果)。

要编写 validator 实现,请检查以下链接:

Spring MVC Form Validation Example using Annotation and Custom Validator implementation

Validation Framework in Spring with Example

希望有帮助。

关于java - 我无法使用 JSR303 @Valid 显示错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25784540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com