Java 尽可能干净,不应包含任何涉及的业务逻辑

Java 尽可能干净,不应包含任何涉及的业务逻辑,java,jsf,Java,Jsf,基本上,您应该在创建托管bean之后和视图呈现时间之前从数据库检索一次产品列表。为了实现这一点,您可以使用@PostConstruct注释来修饰void方法,该方法将在创建bean后执行 代码: @ManagedBean @ViewScoped public class ProductManager { private List<Product> products; @PostConstruct public void init() { //you can c

基本上,您应该在创建托管bean之后和视图呈现时间之前从数据库检索一次产品列表。为了实现这一点,您可以使用
@PostConstruct
注释来修饰
void
方法,该方法将在创建bean后执行

代码:

@ManagedBean
@ViewScoped
public class ProductManager {
    private List<Product> products;
    @PostConstruct
    public void init() { //you can change the method name
        //if you manually handle the controller, initialize it here
        //otherwise, let it be injected by EJB, Spring, CDI
        //or whichever framework you're working with
        products = controller.obtainProductList();
    }
    public List<Product> getProducts() {
        //plain getter, as simple as this
        //no business logic AT ALL
        return this.products;
    }
    public void filterProductsByCategory() {
        filtered = true;
        products = controller.obtainProductListByCategory(selectedDesiredCategory);
        //I guess this method logs a message or something...
        showMessage("Filtered by selected category");
    }
}
@ManagedBean
@视域
公共类产品经理{
私人上市产品;
@施工后
public void init(){//您可以更改方法名称
//如果手动处理控制器,请在此处初始化它
//否则,让它由EJB、Spring、CDI注入
//或者你正在使用的任何框架
产品=控制器。获取产品列表();
}
公共列表产品(){
//简单的getter,就这么简单
//完全没有业务逻辑
退回本产品;
}
公共无效过滤器ProductsByCategory(){
过滤=真;
产品=控制器。按类别获取ProductListByCategory(selectedDesiredCategory);
//我猜这个方法会记录一条消息或者其他什么。。。
showMessage(“按所选类别过滤”);
}
}
更多信息


    • 默认情况下,JSF调用getter方法的次数与视图中使用的次数相同。例如,如果
      #{productManager.products
      在您的视图中出现两次,即在Facelets代码中,那么getter也将执行两次。因此,托管bean中的getter和setter方法应该尽可能干净,不应该包含任何涉及的业务逻辑

      基本上,您应该在创建托管bean之后和视图呈现时间之前从数据库检索一次产品列表。为了实现这一点,您可以使用
      @PostConstruct
      注释来修饰
      void
      方法,该方法将在创建bean之后执行

      代码:

      @ManagedBean
      @ViewScoped
      public class ProductManager {
          private List<Product> products;
          @PostConstruct
          public void init() { //you can change the method name
              //if you manually handle the controller, initialize it here
              //otherwise, let it be injected by EJB, Spring, CDI
              //or whichever framework you're working with
              products = controller.obtainProductList();
          }
          public List<Product> getProducts() {
              //plain getter, as simple as this
              //no business logic AT ALL
              return this.products;
          }
          public void filterProductsByCategory() {
              filtered = true;
              products = controller.obtainProductListByCategory(selectedDesiredCategory);
              //I guess this method logs a message or something...
              showMessage("Filtered by selected category");
          }
      }
      
      @ManagedBean
      @视域
      公共类产品经理{
      私人上市产品;
      @施工后
      public void init(){//您可以更改方法名称
      //如果手动处理控制器,请在此处初始化它
      //否则,让它由EJB、Spring、CDI注入
      //或者你正在使用的任何框架
      产品=控制器。获取产品列表();
      }
      公共列表产品(){
      //简单的getter,就这么简单
      //完全没有业务逻辑
      退回本产品;
      }
      公共无效过滤器ProductsByCategory(){
      过滤=真;
      产品=控制器。按类别获取ProductListByCategory(selectedDesiredCategory);
      //我猜这个方法会记录一条消息或者其他什么。。。
      showMessage(“按所选类别过滤”);
      }
      }
      
      更多信息


      看起来您的
      产品的getter不是一个简单的getter,即可能包含自己的业务逻辑。@LuiggiMendoza我在我的代码中发现了一些非常奇怪的东西。我将编辑这个问题以便您可以看到。请继续关注。看起来您的
      产品的getter不是一个简单的getter,即可能包含wn业务逻辑。@Luigimendoza我在我的代码中发现了一些非常奇怪的东西。我将编辑问题以便您查看。请继续关注。看起来您的
      产品的getter不是一个简单的getter,即可能包含自己的业务逻辑。@Luigimendoza我在我的代码中发现了一些非常奇怪的东西。我将编辑问题o你可以看到。请继续关注。看起来你的
      产品的getter
      不是一个简单的getter,即可能包含它自己的业务逻辑。@Luiggmendoza我在我的代码中发现了一些非常奇怪的东西。我会编辑这个问题以便你可以看到。请继续关注。工作非常出色。我希望我能更好地理解这一点,但我会继续阅读继续。谢谢你的帮助!非常棒。我希望我能更好地理解这一点,但我会继续阅读。谢谢你的帮助!非常棒。我希望我能更好地理解这一点,但我会继续阅读。谢谢你的帮助!非常棒。我希望我能更好地理解这一点,但我会继续阅读。谢谢你的帮助!
         public List<Product> getProducts()
         {
            if(filtered)
            {
                filtered = false;
                return products;
            }
            products = controller.obtainProductList();
            return products;
         }
      
      @ManagedBean
      @ViewScoped
      public class ProductManager {
          private List<Product> products;
          @PostConstruct
          public void init() { //you can change the method name
              //if you manually handle the controller, initialize it here
              //otherwise, let it be injected by EJB, Spring, CDI
              //or whichever framework you're working with
              products = controller.obtainProductList();
          }
          public List<Product> getProducts() {
              //plain getter, as simple as this
              //no business logic AT ALL
              return this.products;
          }
          public void filterProductsByCategory() {
              filtered = true;
              products = controller.obtainProductListByCategory(selectedDesiredCategory);
              //I guess this method logs a message or something...
              showMessage("Filtered by selected category");
          }
      }