2015-10-09 78 views
1

我正在創建一個Spring MVC(4.2.1)/ Hibernate(5.0.1)web應用程序。我試圖在我的表單中獲得錯誤驗證。Spring MVC <form:errors />不顯示錯誤

驗證本身正在工作。當我在我的控制器中撥打result.getAllErrors()時。我得到以下輸出:

[Field error in object 'user' on field 'first_name': rejected value [a]; 
    codes [Size.user.first_name,Size.first_name,Size]; 
    arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.first_name,first_name]; 
    arguments []; 
    default message [first_name],4,2]; 
    default message [size must be between 2 and 4] 
] 

我的.jsp文件中的<form:errors />不能正常工作。它根本不輸出任何東西。

有沒有人知道如何解決這個問題?

create.jsp(視圖)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="tg" tagdir="/WEB-INF/tags" %> 

<div class="container-fluid"> 
    <ol class="breadcrumb"> 
     <li><a href="/host/manage/dashboard/index"><spring:message code="dashboard.title" /></a></li> 
     <li><a href="/host/manage/users/index"><spring:message code="user.title" /></a></li> 
     <li class="active"><spring:message code="user.create_entity" /></li> 
    </ol> 
    <form:form method="post" modelAttribute="user" autocomplete="off"> 
      <div class="panel panel-default"> 
       <div class="panel-heading"> 
        <strong><spring:message code="user.title" /></strong> 
        <p><spring:message code="user.create_entity" /></p> 
       </div> 
       <div class="panel-body"> 
        <div class="row"> 
         <div class="col-md-6"> 
          <div class="form-group"> 
           <label for="firstName"><spring:message code="user.first_name" /></label> 
           <spring:message code="user.first_name_placeholder" var="user_first_name_placeholder"/> 
           <form:input type="text" path="firstName" class="form-control" id="firstName" placeholder="${user_first_name_placeholder}" autocomplete="off" /> 
           <form:errors path="firstName" /> 
          </div> 
         </div> 
         <div class="col-md-6"> 
          <div class="form-group"> 
           <label for="lastName"><spring:message code="user.last_name" /></label> 
           <spring:message code="user.last_name_placeholder" var="user_last_name_placeholder"/> 
           <form:input type="text" path="lastName" class="form-control" id="lastName" placeholder="${user_last_name_placeholder}" autocomplete="off" /> 
          </div> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-md-12"> 
          <div class="form-group"> 
           <label for="email"><spring:message code="user.email" /></label> 
           <spring:message code="user.email_placeholder" var="user_email_placeholder"/> 
           <form:input type="email" path="email" class="form-control" id="email" placeholder="${user_email_placeholder}" autocomplete="off" /> 
          </div> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-md-6"> 
          <div class="form-group"> 
           <label for="password"><spring:message code="user.password" /></label> 
           <spring:message code="user.password_placeholder" var="user_password_placeholder"/> 
           <form:input type="password" path="password" class="form-control" id="password" placeholder="${user_password_placeholder}" autocomplete="off" /> 
          </div> 
         </div> 
         <div class="col-md-6"> 
          <div class="form-group"> 
           <label for="passwordConfirmation"><spring:message code="user.password_confirmation" /></label> 
           <spring:message code="user.password_confirmation_placeholder" var="user_password_confirmation_placeholder"/> 
           <form:input type="password" path="passwordConfirmation" class="form-control" id="passwordConfirmation" placeholder="${user_password_confirmation_placeholder}" autocomplete="off" /> 
          </div> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-md-12"> 
          <div class="form-group"> 
           <label for="role"><spring:message code="user.role" /></label> 
           <form:select path="role" id="role" class="form-control" required="required"> 
            <option default>-- <spring:message code="general.click_to_select" /> --</option> 
            <c:forEach items="${roles}" var="role"> 
             <c:set value="user.${role }" var="roleMessage" /> 
             <option value="${role }"><spring:message code="${roleMessage }"/></option> 
            </c:forEach> 
           </form:select> 
          </div> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-md-12"> 
          <div class="form-group"> 
           <label for="enabled"><spring:message code="user.enabled" /></label> 
           <form:select path="enabled" id="enabled" class="form-control" required="required"> 
            <option default>-- <spring:message code="general.click_to_select" /> --</option> 
            <option value="1"><spring:message code="user.enabled_1" /></option> 
            <option value="0"><spring:message code="user.enabled_0" /></option> 
           </form:select> 
          </div> 
         </div> 
        </div> 
       </div> 
       <div class="panel-footer"> 
        <form:button type="submit" class="btn btn-sm btn-success"><spring:message code="general.save" /></form:button> 
       </div> 
      </div> 
    </form:form> 
</div> 

UserController.java(控制器)

package com.klm.workshop.controller.host.manage; 

import com.klm.workshop.dao.UserDAO; 
import com.klm.workshop.model.User; 
import javax.validation.Valid; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.support.PagedListHolder; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.servlet.ModelAndView; 

/** 
* Host manage users controller 
* 
* @author - 
*/ 
@Controller("hostManageUserController") 
@RequestMapping(value="/host/manage") 
public class UserController { 

    /** 
    * User data access object 
    */ 
    @Autowired 
    private UserDAO userDAO; 

    /** 
    * List of users 
    * 
    * @param model Objects and view 
    * @param page Current pagination page 
    * @param search Search value 
    * @return The users list view 
    */ 
    @RequestMapping(value="https://stackoverflow.com/users/index", method=RequestMethod.GET) 
    public ModelAndView index(
      ModelAndView model, 
      @RequestParam(name="p", required=false, defaultValue = "0") int page, 
      @RequestParam(name="search", required=false, defaultValue = "") String search 
    ) { 
     PagedListHolder pagedListHolder = new PagedListHolder(); 
     pagedListHolder.setSource(userDAO.searchAll(search)); 
     pagedListHolder.setPage(page); 


     model.addObject("pagedListHolder", pagedListHolder); 
     model.setViewName("host/manage/users/index"); 
     return model; 
    } 

    /** 
    * Show create user form 
    * 
    * @param model Objects and view 
    * @return Form to create a user 
    */ 
    @RequestMapping(value="https://stackoverflow.com/users/create", method=RequestMethod.GET) 
    public ModelAndView getCreate(ModelAndView model) { 
     model.addObject("user", new User()); 
     model.addObject("roles", User.Role.values()); 
     model.setViewName("host/manage/users/create"); 
     return model; 
    } 

    /** 
    * Create user, and show create user form 
    * 
    * @param model Objects and view 
    * @param user The posted user 
    * @param result Binded validation 
    * @return Form to create a user, or a redirect (if user was created successfully) 
    */ 
    @RequestMapping(value="https://stackoverflow.com/users/create", method=RequestMethod.POST) 
    public ModelAndView postCreate(
      ModelAndView model, 
      @ModelAttribute("user") @Valid User user, 
      BindingResult result 
    ) { 
     if (result.hasErrors()) { 
      model.addObject("user", user); 
      model.addObject("roles", User.Role.values()); 
      model.setViewName("host/manage/users/create"); 
     } else { 
      model.setViewName("redirect:/host/manage/users/create"); 
     } 

     return model; 
    } 

} 

User.java(模型)

package com.klm.workshop.model; 

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.EnumType; 
import javax.persistence.Enumerated; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.Transient; 
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import org.hibernate.validator.constraints.Email; 

/** 
* User model 
* 
* @author - 
*/ 
@Entity 
@Table(name="users") 
public class User implements Serializable { 

    /** 
    * Host role (enum value for the user role) 
    */ 
    public static final String ROLE_HOST = "ROLE_HOST"; 

    /** 
    * Participant role (enum value for the user role) 
    */ 
    public static final String ROLE_PARTICIPANT = "ROLE_PARTICIPANT"; 

    /** 
    * Administrator role (enum value for the user role) 
    */ 
    public static final String ROLE_ADMINISTRATOR = "ROLE_ADMINISTRATOR"; 

    /** 
    * Host action 
    */ 
    public static final String ACTION_HOST = "host"; 

    /** 
    * Participate action 
    */ 
    public static final String ACTION_PARTICIPATE = "participate"; 

    /** 
    * Identifier for user 
    */ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name="id") 
    private int id; 

    /** 
    * Email from the user 
    */ 
    @Email 
    @NotNull 
    @Column(name="email") 
    private String email; 

    /** 
    * Hashed password from the user 
    */ 
    @NotNull 
    @Column(name="password") 
    private String password; 

    /** 
    * Hashed password confirmation from the user 
    */ 
    @Transient 
    private String password_confirmation; 

    /** 
    * Role from the user 
    */ 
    @Enumerated(EnumType.STRING) 
    @NotNull 
    @Column(name="role") 
    private Role role; 

    /** 
    * If user is blocked 
    */ 
    @NotNull 
    @Column(name="enabled") 
    private boolean enabled; 

    /** 
    * User's first name 
    */ 
    @NotNull 
    @Size(min = 2, max = 4) 
    @Column(name="first_name") 
    private String first_name; 

    /** 
    * User's last name (also containing optional insertion) 
    */ 
    @NotNull 
    @Size(min = 2, max = 255) 
    @Column(name="last_name") 
    private String last_name; 

    /** 
    * User's action (want does the user want to do, participate or host) 
    */ 
    @Transient 
    private String action; 

    /** 
    * User's workshop (only set if the user is a participant) 
    */ 
    @Transient 
    private Workshop workshop; 

    /** 
    * Enum values for the role column 
    */ 
    public enum Role { 
     ROLE_PARTICIPANT, 
     ROLE_HOST, 
     ROLE_ADMINISTRATOR; 
    } 

    /** 
    * @return the id 
    */ 
    public int getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 

    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 

    /** 
    * @return the hashed password 
    */ 
    public String getPassword() { 
     return password; 
    } 

    /** 
    * @param password the password to set 
    */ 
    public void setPassword(String password) { 
     BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder(); 
     String hashedPassword = bcrypt.encode(password); 
     this.password = hashedPassword; 
    } 

    /** 
    * @return the hashed password 
    */ 
    public String getPasswordConfirmation() { 
     return password_confirmation; 
    } 

    /** 
    * @param password_confirmation the password confirmation to set 
    */ 
    public void setPasswordConfirmation(String password_confirmation) { 
     BCryptPasswordEncoder bcrypt = new BCryptPasswordEncoder(); 
     String hashedPassword = bcrypt.encode(password_confirmation); 
     this.password_confirmation = hashedPassword; 
    } 

    /** 
    * @return the role 
    */ 
    public Role getRole() { 
     return role; 
    } 

    /** 
    * @param role the role to set 
    */ 
    public void setRole(Role role) { 
     this.role = role; 
    } 

    /** 
    * @return the enabled status 
    */ 
    public boolean getEnabled() { 
     return enabled; 
    } 

    /** 
    * @param enabled the enabled status to set 
    */ 
    public void setEnabled(boolean enabled) { 
     this.enabled = enabled; 
    } 

    /** 
    * @return the first_name 
    */ 
    public String getFirstName() { 
     return first_name; 
    } 

    /** 
    * @param first_name the first_name to set 
    */ 
    public void setFirstName(String first_name) { 
     this.first_name = first_name; 
    } 

    /** 
    * @return the last_name 
    */ 
    public String getLastName() { 
     return last_name; 
    } 

    /** 
    * @param last_name the last_name to set 
    */ 
    public void setLastName(String last_name) { 
     this.last_name = last_name; 
    } 

    /** 
    * @return the first_name and last_name 
    */ 
    public String getFullName() { 
     return getFirstName() + " " + getLastName(); 
    } 

    /** 
    * @return the action 
    */ 
    public String getAction() { 
     return action; 
    } 

    /** 
    * @param action the action to set 
    */ 
    public void setAction(String action) { 
     this.action = action; 
    } 

    /** 
    * @return the workshop 
    */ 
    public Workshop getWorkshop() { 
     if(this.action.equals(ACTION_HOST)) { 
      throw new UnsupportedOperationException("Workshop cannot be returned when user is hosting a workshop"); 
     } 

     return workshop; 
    } 

    /** 
    * @param workshop the workshop to set 
    */ 
    public void setWorkshop(Workshop workshop) { 
     if(this.action.equals(ACTION_HOST)) { 
      throw new UnsupportedOperationException("Workshop cannot be set when user is hosting a workshop"); 
     } 

     this.workshop = workshop; 
    } 

} 

回答

1

您正在執行重定向 model.setViewName(「redirect:/ host/manage/users/create」);

您的錯誤存儲在模型中,隨後存儲在HttpServletRequest屬性中。這些只在一次請求期間持續。重定向會導致客戶端發送新的請求。因此,在重定向視圖呈現時,它們不存在。

考慮使用Flash屬性。

請看下面的例子中

Spring - Redirect after POST (even with validation errors)

+0

我只在沒有錯誤時執行重定向。但正如我提到的那樣,存在錯誤。代碼不會重定向,但會返回視圖,而不是用戶和角色。 – Tijme

+0

我剛剛嘗試了電子郵件屬性的驗證。而且驗證確實有效。首字母 – Tijme

相關問題