2015-01-31 2 views
1

아래 코드 스 니펫을 참조하십시오. 나는 3 개의 필드가있는 간단한 JSP로 시작한다. 양식을 제출할 때 폼 빈의 주석을 통해 구성된 유효성 검사 (JSR-303)를 시작하여 오류 메시지를 표시하고 싶습니다. 그러나 그것은 일어나지 않습니다. 페이지가 searchActions.findExistingPlayer 메소드에 제출됩니다. 모든 포인터가 도움이 될 것입니다.스프링 웹 플로우 - 모델 bean에서 주석 유효성 검증을 호출 할 수 없습니다.

구성 파일 :

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Source project: sip05, branch: 03 (Maven Project) --> 

<flow xmlns="http://www.springframework.org/schema/webflow" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/webflow 
    http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd" 
    start-state="findExistingPlayerForm"> 

    <view-state id="findExistingPlayerForm"> 
     <on-render> 
      <evaluate expression="findExistingPlayerFormAction.setupForm"></evaluate> 
     </on-render> 
     <transition on="find" to="findExistingPlayerFormActionState"> 
      <evaluate expression="findExistingPlayerFormAction.bindAndValidate"></evaluate> 
     </transition> 
    </view-state> 

    <action-state id="findExistingPlayerFormActionState"> 
     <evaluate expression="searchActions.findExistingPlayer"></evaluate> 
     <transition on="success" to="displayFindExistingPlayerResult"></transition> 
    </action-state> 
    <view-state id="displayFindExistingPlayerResult"> 
     <<---Some transitions-->>> 
    </view-state> 
    <end-state id="newSearchEndState" /> 

</flow> 

액션 매핑

<bean id="findExistingPlayerFormAction" class="org.springframework.webflow.action.FormAction"> 
     <property name="formObjectClass" 
      value="com.saurabhd.springwebflow.form.PlayerSearchForm" /> 
    </bean> 

형태 :

public class PlayerSearchForm implements Serializable{ 
private static final long serialVersionUID = 1L; 


    private String firstName; 

    private String lastName; 
    private String homePhone; 

    @NotEmpty 
    @Size(min=10) 
    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    @NotNull 
    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    @NotBlank 
    public String getHomePhone() { 
     return homePhone; 
    } 
    public void setHomePhone(String homePhone) { 
     this.homePhone = homePhone; 
    } 
} 

웹 흐름 컨텍스트 :

<flow:flow-builder-services id="flowBuilderServices" 
     development="true" 
     validator="validator"/> 
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 
,

JSP

<%-- Source project: sip05, branch: 03 (Maven Project) --%> 
<%@ include file="/WEB-INF/jsp/taglibs.jspf" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:jsp="http://java.sun.com/JSP/Page" 
    xmlns:spring="http://www.springframework.org/tags" 
    xmlns:form="http://www.springframework.org/tags/form"> 

    <head><title>Find Existing Player(s)</title></head> 

    <body> 
    <h2>Search</h2>   
    <p> 
     Please fill the info below: 
    </p> 

    <form:form commandName="playerSearchForm" action="${flowExecutionUrl}">   
     <label for="firstname">Player First Name</label> 
     <form:input path="firstName" /><br/> 
     <form:errors path="firstName"/> <br/><br/> 

     <label for="lastName">Player Last Name</label> 
     <form:input path="lastName" /><br/> 
     <form:errors path="lastName"/> <br/><br/> 



     <label for="homePhone">Home Phone:</label> 
     <form:input path="homePhone" /><br/> 
     <form:errors path="homePhone"/> <br/><br/> 

     <input type="submit" name="_eventId_skip" 
     value="Skip"/> 
     <input type="submit" name="_eventId_find" 
     value="Find"/>    
    </form:form> 

    </body> 
</html> 

업데이트가 - 나는 또한 뷰 상태에서 바인딩 모델을 시도했습니다. 또한 내 콩에 대한 JSR-303 주석 유효성 검사를 호출하지 못한다. :(지금까지 근무하고있다 무엇

사용자 정의 유효성 검사 $ {뷰 상태-ID} 방법은 아래를 참조하십시오 :.

public void validateFindExistingPlayerForm(ValidationContext context){ 
     MessageContext messages = context.getMessageContext(); 
     if(StringUtils.isEmpty(firstName)){ 
      messages.addMessage(new MessageBuilder().error().source("firstName"). 
        defaultText("Please enter the value for First Name.").build()); 
     } 
    } 

Flow.xml을

<var name="playerSearchForm" class="com.saurabhd.springwebflow.form.PlayerSearchForm"/> 

    <view-state id="findExistingPlayerForm" model="playerSearchForm"> 
     <transition on="find" to="findExistingPlayerFormActionState"> 
     </transition> 
    </view-state> 
+0

얘들 아, 누군가가 도움이 여기에 알려 주시기 바랍니다 수 있습니다. – sunnyd

+0

@WillieWheeler 여기에서 도와주세요. – sunnyd

답변

0

내가했습니다 view-state에서 모델을 올바르게 정의하지 않은 것으로 나타났습니다. 모델을 정의 할 때 작업중인 모델의 인스턴스를 흐름 범위 또는 뷰 범위로 설정해야합니다.

흐름에 PlayerSearchForm의 새 인스턴스를 만들고 view-state에 바인딩하는 중입니다. 보기에서 작업중인 인스턴스를 바인드해야하기 때문에 이것은 작동하지 않습니다.

개체의 참조를 전달하는 두 가지 방법이 있습니다.

첫 번째 방법 :

//You can set objects into the flow scope, like this. 
RequestContextHolder.getRequestContext().getFlowScope().put("playerSearchForm", instanceOfPlayerSearchForm); 

두 번째 방법 :

<evaluate expression="findExistingPlayerFormAction.getPlayerSearchForm()" result="flowScope.playerSearchForm"/> 


public PlayerSearchForm getPlayerSearchForm(){ 
    return instanceOfPlayerSearchForm; 
} 
관련 문제