JSF MVC设计问题 [英] JSF MVC design question

查看:101
本文介绍了JSF MVC设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSF支持bean设计问题。现在,我的支持bean正在持有UI显示信息以及业务模态数据。人们建议模型和观点应该分开。所以创建不同的bean持有UI显示数据是有好的主意,并且支持bean有引用吗?

解决方案


那么创建不同的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);
returnview;
}

//总共6个getter和setter。
}

 < H:形式> 
< h:inputText value =#{productEditor.productName}/>
< h:inputTextarea value =#{productEditor.productDescription}/>
< h:inputText value =#{productEditor.productPrice}>
< f:convertNumber type =currencycurrencySymbol =$/>
< / h:inputText>
< h:commandButton value =Addaction =#{productEditor.add}/>
< / h:form>






这是更好的

  @ManagedBean 
@RequestScoped
public class ProductEditor {

private Product product;

@PostConstruct
public void init(){
product = new Product(); //您还可以根据ID作为请求参数从DB预加载。
}

public String add(){
productService.save(product);
returnview;
}

//只有1个getter。
}

 < H:形式> 
< h:inputText value =#{productEditor.product.name}//>
< h:inputTextarea value =#{productEditor.product.description}/>
< h:inputText value =#{productEditor.product.price}>
< f:convertNumber type =currencycurrencySymbol =$/>
< / h:inputText>
< h:commandButton value =Addaction =#{productEditor.add}/>
< / h:form>

另见由这个JSF 2.0教程


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?

解决方案

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.

这篇关于JSF MVC设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆