4
我有一个JSF支持bean设计问题。现在,我的支持bean持有UI显示信息以及商业模式数据。人们建议模型和观点应该分开。那么创建不同的bean来持有UI显示数据并且让后台bean有引用它是不错的主意?JSF MVC设计问题
我有一个JSF支持bean设计问题。现在,我的支持bean持有UI显示信息以及商业模式数据。人们建议模型和观点应该分开。那么创建不同的bean来持有UI显示数据并且让后台bean有引用它是不错的主意?JSF MVC设计问题
因此,创建不同的bean持有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.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>
这是更好的
@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.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>
见还有this JSF 2.0 tutorial提供的例子。
哇这很快很清楚,非常感谢你的回答 – 2011-04-18 17:05:25