<form:error> 不打印消息
<form:error> not printing message
这是我的代码。有人可以帮忙吗?我已经尝试了互联网上的所有内容。我已经参考许多网站更改了我的代码,但徒劳无功。我一直在寻找答案很长一段时间,但没有找到任何东西。请帮忙
豆子class
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column
@NotEmpty (message="First Name cannot be blank")
private String firstName;
@Column
private String lastName;
@Column
@Email(message="Email Id is not valid")
private String email;
@Column
private int age;
@Column
private String phoneNumber;
@Column
private boolean isAdmin = true;
@Column
@Size(min=3 , max = 10,message="Username should be between 3 to 10 characters long")
private String userName;
@Column
private String password;
@Column
private String dob;
@Column
private boolean isActive;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public String toString() {
return "UserVo [userId=" + userId + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
+ ", age=" + age + ", phoneNumber=" + phoneNumber + ", isAdmin=" + isAdmin + ", userName=" + userName
+ ", password=" + password + ", dob=" + dob + ", isActive=" + isActive + "]";
}
}
Spring-servlet.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.ism"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://EHRRCMDEV:2433" />
<property name="username" value="OMDTrainee" />
<property name="password" value="Pass@OMDTrainee" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ism.vo.UserVo</value>
<value>com.ism.vo.CategoryVo</value>
<value>com.ism.vo.ItemVo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Jsp 文件
<tr>
<td>Email:</td>
<td><form:input path="email"/><font color="red"><form:errors path="email"></form:errors></font></td>
</tr>
Message.properties
NotEmpty.userVo.firstName=First Name is required
Email.userVo.email= Email addesss is not valid
我复制了您的代码段并添加了以下代码来满足您的要求。它正在显示验证消息。
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String testMethodPost(HttpServletRequest request, @Valid @ModelAttribute("Bean") Bean bean, BindingResult result, ModelMap model) {
model.addAttribute("org.springframework.validation.BindingResult.bean", result);
model.addAttribute("bean", bean);
return "MyJsp";
}
你能包括你的控制器和 jsp 吗? bean定义一定有错误。
我需要的片段是:@Requestmethod.Get returns 你的表格,< form:form>[=15= 的声明] 标签,以及处理传入请求的方法 @Requestmethod.Post
这是我的代码。有人可以帮忙吗?我已经尝试了互联网上的所有内容。我已经参考许多网站更改了我的代码,但徒劳无功。我一直在寻找答案很长一段时间,但没有找到任何东西。请帮忙
豆子class
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@Column
@NotEmpty (message="First Name cannot be blank")
private String firstName;
@Column
private String lastName;
@Column
@Email(message="Email Id is not valid")
private String email;
@Column
private int age;
@Column
private String phoneNumber;
@Column
private boolean isAdmin = true;
@Column
@Size(min=3 , max = 10,message="Username should be between 3 to 10 characters long")
private String userName;
@Column
private String password;
@Column
private String dob;
@Column
private boolean isActive;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
@Override
public String toString() {
return "UserVo [userId=" + userId + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
+ ", age=" + age + ", phoneNumber=" + phoneNumber + ", isAdmin=" + isAdmin + ", userName=" + userName
+ ", password=" + password + ", dob=" + dob + ", isActive=" + isActive + "]";
}
}
Spring-servlet.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.ism"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
<!-- DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://EHRRCMDEV:2433" />
<property name="username" value="OMDTrainee" />
<property name="password" value="Pass@OMDTrainee" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.ism.vo.UserVo</value>
<value>com.ism.vo.CategoryVo</value>
<value>com.ism.vo.ItemVo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Jsp 文件
<tr>
<td>Email:</td>
<td><form:input path="email"/><font color="red"><form:errors path="email"></form:errors></font></td>
</tr>
Message.properties
NotEmpty.userVo.firstName=First Name is required
Email.userVo.email= Email addesss is not valid
我复制了您的代码段并添加了以下代码来满足您的要求。它正在显示验证消息。
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String testMethodPost(HttpServletRequest request, @Valid @ModelAttribute("Bean") Bean bean, BindingResult result, ModelMap model) {
model.addAttribute("org.springframework.validation.BindingResult.bean", result);
model.addAttribute("bean", bean);
return "MyJsp";
}
你能包括你的控制器和 jsp 吗? bean定义一定有错误。 我需要的片段是:@Requestmethod.Get returns 你的表格,< form:form>[=15= 的声明] 标签,以及处理传入请求的方法 @Requestmethod.Post