2013-07-15 2 views
19

누구든지 HTML 속성의 객체를 Spring의 Java 클래스에 바인딩하는 방법을 설명 할 수 있습니까? 나는 웹 프레임 워크를 봄으로 초보자 다.Spring의 경로 속성

+0

"스프링 폼 태그"를 검색해보십시오. html 폼을 객체에 바인딩하는 스프링 속성이있는 html 태그처럼 보입니다. – Oneb

+0

나는 유용한 정보가 없으므로 이해할 수 없으므로 Stack : –

+0

참조 http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html –

답변

42

짧은 이야기 경로 속성은 java beans 규칙을 사용하여 Java 속성에 바인딩됩니다. 다음과 같은 형식에 대한 예를 들면 다음과 같습니다의

public class Student { 
    private String name; 
    public String getName() { return this.name; } 
    public void setName(String name) { this.name = name; } 

    private boolean cool; 
    public boolean isCool() { return this.cool; } 
    public void setCool(boolean cool) { this.cool = cool; } 
} 

더 많은 정보 : 학생 클래스는 다음과 같은 속성으로 정의 된 경우

@RequestMapping(...) 
public String updateStudent(@ModelAttribute("theStudent") Student student) { 
    // ... 
} 

자동 바인딩 : 컨트롤러 핸들러 방법에 따라

<form:form method="post" modelAttribute="theStudent"> 
    Name: <form:input type="text" path="name"/> 
    Cool?: <form:input type"checkbox" path="cool"/> 
    <button>Save</button> 
</form:form> 

그리고 JavaBeans convenion은 section 8.3 of the specification document에 있습니다.

+0

명령 객체는 어떻습니까? –

+2

이것은 modelAttribute의 또 다른 구문입니다. 그래서'commandObject = "theStudent"는 같은 결과를 줄 것입니다. – gerrytan

+0

안녕하세요, 당신이 commandName = "command"를 폼에 추가하지 않으면 작동하지 않습니다. – FrankelStein

관련 문제