Spring MVC Form Check if a field has an error

摘要: In this article, we will show you few Spring form tag examples to check if a field has an error message. Review the following Spring MVC bean validation example :

In this article, we will show you few Spring form tag examples to check if a field has an error message. Review the following Spring MVC bean validation example :

Technologies used :

  1. Spring 4
  2. JSTL 1.2
//Bean validation
import org.hibernate.validator.constraints.NotEmpty;
public class User {
	@NotEmpty
	String name;
	//...
//Controller class
@RequestMapping(value = "/users", method = RequestMethod.POST)
public String saveOrUpdateUser(
	@ModelAttribute("userForm") @Valid User user,
	BindingResult result, Model model) {
	if (result.hasErrors()) {
		//...
	} else {
		//...

1. form:errors

If ‘name’ field has an error message, you can display it via form:errors

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form method="post" modelAttribute="userForm" action="${userActionUrl}">
	<label>Name</label>
	<form:input path="name" type="text" id="name" />
	<form:errors path="name" />
</form:form>

2. spring:bind and form:errors

With spring:bind, you can use ${status.error} to check if the ‘name’ field has an error, and display different CSS class conditionally.

<form:form method="post" modelAttribute="userForm" action="${userActionUrl}">
    <spring:bind path="name">
	<div class="form-group ${status.error ? 'has-error' : ''}">
		<label>Name</label>
		<form:input path="name" type="text" id="name" />
		<form:errors path="name" />
	</div>
    </spring:bind>
</form:form>

The error message is still displayed via form:errors, but this way you get more controls.

3. c:set and form:errors

Same like example 2, instead, you use c:set to check if the ‘name’ field has an error message.

<form:form method="post" modelAttribute="userForm" action="${userActionUrl}">
	<c:set var="nameHasBindError">
		<form:errors path="name"/>
	</c:set>
	<div class="form-group ${not empty nameHasBindError?"has-error":""}">
		<label>Name</label>
		<form:input path="name" type="text" id="name" />
		${nameHasBindError}
	</div>
</form:form>

This example is a bit weird, but it works.

4. Display all errors

To display all the error messages in the submitted form, use spring:hasBindErrors and loop the ${errors.allErrors}

    <spring:hasBindErrors name="userForm">
	<c:forEach var="error" items="${errors.allErrors}">
		<b><spring:message message="${error}" /></b>
		<br />
	</c:forEach>
    </spring:hasBindErrors>

Alternatively, use path="*"

<form:form method="post" modelAttribute="userForm" action="${userActionUrl}">
	<form:errors path="*" class="has-error" />
</form:form>

References

  1. Spring Tags – bind tag and hasBindErrors tag
  2. Spring MVC form handling example
  3. Spring MVC and JSR 303 @Valid bean validation example

上一篇: Java Generate random integers in a range
下一篇: Python 3 TypeError: Cant convert bytes object to str implicitly
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号