在Controller bean中引用managedBean

时间:2013-04-08 20:36:18

标签: jsf-2 managed-bean

我想在Controller bean中传递我的Managedbean的引用作为参数。我的域名实体是品牌。我想从JSF页面组件设置品牌的属性。我想在控制器方法和此控制器addBrand方法getBrandService中传递该managedBean引用并保存相应的品牌Bean。请指导我。谢谢 :) 这是我的托管bean代码:

@ManagedBean
@RequestScoped
public class BrandBean implements Serializable{
    private static final long serialVersionUID = 1L;
    private String brandName;
    private String brandDecription;
//getters and setters
}

这是我的品牌控制器bean:     // addBrand Method将品牌实例作为参数。品牌是模型对象。

@ManagedBean
@RequestScoped
public class BrandController extends AbstractController{
    private IBrandService brandService;

    public IBrandService getBrandService() {
        brandService = new BrandService();
        return brandService;
    }

    public void setBrandService(IBrandService brandService) {
        this.brandService = brandService;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        return null;
    }
    public void addBrand(String brandName, String brandDecription){
        try{
            Brand brand = new Brand();
            brand.setBrandName(brandName);
            brand.setBrandDecription(brandDecription);
            getBrandService().addBrand(brand);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

@ManagedBean
@RequestScoped
@SuppressWarnings("serial")
public class BrandBean implements Serializable {
    private int brandId;
    private String brandName;
    private String brandDescription;
    private List<Brand> brands;
    private List<Brand> filter;


    public List<Brand> getFilter() {
        return filter;
    }
    public void setFilter(List<Brand> filter) {
        this.filter = filter;
    }
    public List<Brand> getBrands() {
        return brands;
    }
    public void setBrands(List<Brand> brands) {
        this.brands = brands;
    }
    public int getBrandId() {
        return brandId;
    }
    public void setBrandId(int brandId) {
        this.brandId = brandId;
    }
    public String getBrandName() {
        return brandName;
    }
    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }
    public String getBrandDescription() {
        return brandDescription;
    }
    public void setBrandDescription(String brandDescription) {
        this.brandDescription = brandDescription;
    }

}

@ManagedBean
@RequestScoped
public class BrandController {
    private BrandBean bean;
    @ManagedProperty(value = "#{BrandService}")
    private BrandService brandService;
public BrandController() {
        bean = new BrandBean();


    }
    }