JSF MVC设计问题

发布于 2024-11-01 18:52:20 字数 118 浏览 0 评论 0原文

我有一个 JSF 支持 bean 设计问题。现在,我的支持 bean 保存 UI 显示信息和业务模式数据。人们建议模型和视图应该分开。那么创建不同的 bean 来保存 UI 显示数据并让支持 bean 引用它是个好主意吗?

I have a JSF backing bean design question. right now, my backing bean is holding UI display information and also business modal data. people suggest that model and view should be separated. so is it good idea to create different bean holding UI display data and have backing bean have reference to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女猫性小仙女 2024-11-08 18:52:20

那么创建不同的 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>

此 JSF 2.0 教程

so is it good idea to create different bean the holding UI display data and have backing have reference to it?

Yes, otherwise you keep mapping the data from model to view yourself while you can also just let JSF/EL do that. It does by the way not necessarily need to be a JSF @ManagedBean.

E.g. this is poor:

@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.
}

with

<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 is better

@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.
}

with

<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>

See also the examples as presented by this JSF 2.0 tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文