2014-03-27 2 views
1

에서 검출 :봄 바인드하지 나는 다음과 같은 형태의 선언이 컨트롤러

<form id="reply-form-frm" novalidate="novalidate" action="/reply/responsive" method="POST"> 
       [@spring.bind "model.form"/] 

       [@spring.bind "model.form.message" /] 

       <label for="${spring.status.expression}" class="clearfix" data-error="Please enter a message"> 
        <span class="label-text">Your message</span> 
        <span class="set-right required-flag">Required</span> 
       </label> 

       <p class="input-error[#if spring.status.errorMessages?size > 0] reveal-error[#else] hide-error[/#if]"> 
        [#if spring.status.errorMessages?size > 0] 
         <span>[#list spring.status.errorMessages as error]${error}[/#list]</span> 
        [/#if] 
       </p> 
       [@spring.formTextarea "model.form.message", 'aria-required="true" required="required" data-clear="true"' /] 
    </form> 

일부 추가 필드가 같은 방식으로 렌더링됩니다.

model: { 
    form: { 
    senderName: "hahahahaha", 
    senderEmail: "[email protected]", 
    advertId: 1000011793, 
    optInMarketing: false, 
    advertClickSource: "natural" 
} 

그리고 내 컨트롤러는이 매핑 방법이 있습니다 : 양식을받는 모델이다

public ModelAndView sendReplyResponsive(@ModelAttribute("model.form") ReplyForm replyForm, 
              BindingResult bindingResult) { 
    //TODO: content irrelevant ... 
} 

문제는 양식을 렌더링 할 때, 요소가 "form.message"대신지도이다 " model.form.message "및 내 컨트롤러 포스트에서 ReplyForm 개체를 찾을 수 없습니다.

"model.form.message"를 이름으로 사용하도록 Spring을 강제로 설정할 수있는 방법에 대한 아이디어가 있습니까?

답변

1

짧은 대답 - 스프링이 "model.form.message"를 이름으로 사용하도록 할 수는 없습니다.

긴 대답 : 스프링은 항상 프레젠테이션 엔진에 종속되지 않는 이름 ("model")의 첫 번째 부분을 제거합니다.이 논리는 org.springframework.web.servlet.support.BindStatus#BindStatus 안에 코딩되어 있습니다. 바인딩
1. "모델"클래스 :

<form id="reply-form-frm" novalidate="novalidate" action="/reply/responsive" method="POST"> 
    [@spring.bind "form.message" /] 

    <label for="${spring.status.expression}" class="clearfix" data-error="Please enter a message"> 
     <span class="label-text">Your message</span> 
     <span class="set-right required-flag">Required</span> 
    </label> 

    <p class="input-error[#if spring.status.errorMessages?size > 0] reveal-error[#else] hide-error[/#if]"> 
     [#if spring.status.errorMessages?size > 0] 
      <span>[#list spring.status.errorMessages as error]${error}[/#list]</span> 
     [/#if] 
    </p> 
    [@spring.formTextarea "form.message", 'aria-required="true" required="required" data-clear="true"' /] 
</form> 
:

public ModelAndView sendReplyResponsive(@ModelAttribute("model") ReplyModel model, BindingResult bindingResult) { 
    ReplyForm replyForm = model.getForm(); 
    //TODO: content irrelevant ... 
} 

또는 2. 독립적 인 모델 속성으로 ReplyForm 노출 일반적인 접근 방식은 렌더링 및 바인딩을 위해 당신이해야하는 경우, 동일한 모델을 사용하는 것입니다 당신의 "모델"유일한 형태가 포함되어있는 경우

model: { 
    form: { 
    senderName: "hahahahaha", 
    senderEmail: "[email protected]", 
    advertId: 1000011793, 
    optInMarketing: false, 
    advertClickSource: "natural" 
    } 
}, 
form: { 
    senderName: "hahahahaha", 
    senderEmail: "[email protected]", 
    advertId: 1000011793, 
    optInMarketing: false, 
    advertClickSource: "natural" 
} 

는 다음을 계속 할 이유가 없다 : 같은

모델이 될 것입니다. 컨트롤러 방법은 다음과 같습니다.

public ModelAndView sendReplyResponsive(@ModelAttribute("form") ReplyForm replyForm, 
              BindingResult bindingResult) { 
    //TODO: content irrelevant ... 
} 
관련 문제