2011-04-18 5 views
4

JSF 백업 빈 설계 관련 질문이 있습니다. 지금 내 백킹 빈은 UI 디스플레이 정보와 비즈니스 모달 데이터를 보유하고 있습니다. 사람들은 모델과 뷰를 구분해야한다고 제안합니다. 그래서 다른 Bean을 생성하여 UI 표시 데이터를 생성하고 Backing Bean을 참조하도록하는 것이 좋습니다.JSF MVC 설계 질문

답변

8

그래서 보류 UI 표시 데이터와 다른 빈을 작성하여 보충을 참조하는 것이 좋습니다.

예, 그렇지 않으면 모델에서 데이터를 매핑하여 볼 수 있지만 JSF/EL에서도 그렇게 할 수 있습니다. 그런데 반드시 JSF @ManagedBean 일 필요는 없습니다.

예. 이 가난 :

@ManagedBean 
@RequestScoped 
public class ProductEditor { 

    private String productName; 
    private String productDescription; 
    private BigDecimal productPrice; 

    public String add() { 
     Product product = new Product(); 
     product.setName(productName); 
     product.setDescription(productDescription); 
     product.setPrice(productPrice); 
     productService.save(product); 
     return "view"; 
    } 

    // In total 6 getters and setters. 
} 

<h:form> 
    <h:inputText value="#{productEditor.product.name}" /> 
    <h:inputTextarea value="#{productEditor.product.description}" /> 
    <h:inputText value="#{productEditor.product.price}"> 
     <f:convertNumber type="currency" currencySymbol="$" /> 
    </h:inputText> 
    <h:commandButton value="Add" action="#{productEditor.add}" /> 
</h:form> 

참조와

@ManagedBean 
@RequestScoped 
public class ProductEditor { 

    private Product product; 

    @PostConstruct 
    public void init() { 
     product = new Product(); // You could also preload from DB based on some ID as request parameter. 
    } 

    public String add() { 
     productService.save(product); 
     return "view"; 
    } 

    // Only 1 getter. 
} 

<h:form> 
    <h:inputText value="#{productEditor.productName}" /> 
    <h:inputTextarea value="#{productEditor.productDescription}" /> 
    <h:inputText value="#{productEditor.productPrice}"> 
     <f:convertNumber type="currency" currencySymbol="$" /> 
    </h:inputText> 
    <h:commandButton value="Add" action="#{productEditor.add}" /> 
</h:form> 

로 또한 this JSF 2.0 tutorial에 의해 제시된 예제.

+0

와우 이것은 빠르고 명확합니다. 답변 해 주셔서 대단히 감사합니다. –