0

을 표시하지 내 컨트롤러에서 사용자 정의 유효성 검사기를 초기화하고 양식 및 저장 방법에 대한 매핑을 설정하십시오.봄 유효성 검사 오류가 나는 주석 검증의 조합 및 사용자 정의 유효성 검사기</p> <p>객체를 사용하고

컨트롤러 : 난 단지 내 필드 (나는 부트 스트랩 사용)의 CSS를 변경 때문에 오류를 표시 할 첫 번째 단계에서

@Controller 
public class MonitoringSystemController { 

@Autowired 
private MonitoringSystemValidator monitoringSystemValidator; 

@InitBinder 
public void dataBinding(WebDataBinder binder) { 
    binder.addValidators(monitoringSystemValidator); 
} 

@RequestMapping("/monitoringsystem/new") 
public String newMonitoringSystem(Model model, HttpServletRequest request) { 
    MonitoringSystem monitoringSystem = new MonitoringSystem(); 
    model.addAttribute("monitoringSystem", monitoringSystem); 
    request.getSession().setAttribute("anonymization", monitoringSystem.getAnonymization()); 
    request.getSession().setAttribute("hosts", monitoringSystem.getHosts()); 

    return "monitoringsystem/form"; 
} 

@RequestMapping(value = "/monitoringsystem/save", method = RequestMethod.POST) 
public String save(@Valid MonitoringSystem monitoringSystem, BindingResult result, HttpServletRequest request, Model model) { 

    if(result.hasErrors()){ 
     model.addAttribute("monitoringSystem", monitoringSystem); 
     request.getSession().setAttribute("anonymization", request.getSession().getAttribute("anonymization")); 
     request.getSession().setAttribute("hosts", request.getSession().getAttribute("hosts"));   
     return "monitoringsystem/form"; 
    } 

//more code 

.

형태 : 내 유효성 검사가 제대로 작동

<form class="form-horizontal" th:modelAttribute="monitoringSystem" th:object="${monitoringSystem}" th:action="@{/monitoringsystem/save}" method="post"> 
      <input type="hidden" th:field="*{id}"/> 
      <fieldset> 
       <legend>New Monitoring-System</legend> 
       <div class="form-group" th:classappend="${#fields.hasErrors('name')} ?: 'has-error has-danger'"> 
        <label class="col-md-4 control-label" for="textinput">Systemname</label> 
        <div class="col-md-5"> 
         <input th:field="*{name}" class="form-control input-md" type="text" /> 
        </div> 
       </div> 
       <div class="form-group" th:classappend="${#fields.hasErrors('url')} ?: 'has-error has-danger'"> 
        <label class="col-md-4 control-label" for="textinput">URL</label> 
        <div class="col-md-5"> 
         <input th:field="*{url}" class="form-control input-md" type="text" /> 
        </div> 
       </div> 
       <div class="form-group" th:classappend="${#fields.hasErrors('username')} ?: 'has-error has-danger'"> 
        <label class="col-md-4 control-label" for="textinput">Username</label> 
        <div class="col-md-5"> 
         <input th:field="*{username}" class="form-control input-md" type="text" /> 
        </div> 
       </div> 
       <div class="form-group" th:classappend="${#fields.hasErrors('password')} ?: 'has-error has-danger'"> 
        <label class="col-md-4 control-label" for="textinput">Password</label> 
        <div class="col-md-5"> 
         <input th:field="*{password}" class="form-control input-md" type="password" /> 
        </div> 
       </div> 
       <div class="form-group" th:classappend="${#fields.hasErrors('passwordConfirm')} ?: 'has-error has-danger'"> 
        <label class="col-md-4 control-label" for="textinput">Confirm Password</label> 
        <div class="col-md-5"> 
         <input th:field="*{passwordConfirm}" class="form-control input-md" type="password" /> 
        </div> 
       </div> 
       <div class="form-group"> 
        <label class="col-md-4 control-label" for="singlebutton"></label> 
        <div class="col-md-4"> 
         <a th:href="@{/monitoringsystem}" class="btn btn-default btn-small">Cancel</a> <button id="singlebutton" name="singlebutton" class="btn btn-primary btn-small">Submit</button> 
        </div> 
       </div> 
      </fieldset> 
     </form> 

. 내 필드가 null이 아니고 크기가 1보다 크고 암호가 일치하는 경우에만 양식이 저장됩니다. 그렇지 않으면 내 컨트롤러가 나를 양식으로 리디렉션합니다.

문제는 내 CSS가 변경되지 않는다는 것입니다. 그래서 내 view-code에 문제가 있거나 errorBinding이 뷰에 올바르게 전달되지 않았습니다. 그러나 나는 나의 실수를 발견 할 수 없다.

답변

0

errorclasses를 추가하는 my if 조건에 오류가있었습니다. 나는 변경해야했다 ${#fields.hasErrors('url')} ?: 'has-error has-danger' to ${#fields.hasErrors('*{name}')} ? 'has-error has-danger'