小编典典

JSF:ManagedBean,处理业务逻辑的好地方吗?

java

我已经managedBeanfileUpload,一旦文件被上传,然后我需要调用基于从解析器下拉列表中选择什么样的价值不同的解析器,然后解析器正在创建的对象DetailsClass,其中我打电话getDetails的那个特定的类方法,这里要注意的一点是,既不parserClass也不DetailsClass是在faces-
config.xml中注册的,我的问题是

  • 如果我想维护从一个FileUpload班级到另一个班级的会话信息ParserDetailsClass那么我应该在其中定义它,faces-config.xml但是应该如何定义parserDetailsClass定义类,应该将它定义为managedBean其他名称还是其他名称?

这是代码:

在我managedBean类我有两个功能,fileUploadcallParser如图所示:

 public void uploadFile(FileEntryEvent event)
{
    FacesContext ctx = FacesContext.getCurrentInstance();
    //Setting getSession to false, container will not create new session if session is not present and return null
    HttpSession session = (HttpSession) ctx.getExternalContext().getSession(false);
    setSession(session);

    resultBean = new ResultBean();
    FileEntry fileEntry = (FileEntry) event.getSource();
    FileEntryResults results = fileEntry.getResults();
    FileEntry fe = (FileEntry) event.getComponent();

    FacesMessage msg = new FacesMessage();
    for (FileEntryResults.FileInfo fileInfo : results.getFiles())
    {
        if (fileInfo.isSaved())
        {
            File file = fileInfo.getFile();
            String filePath = file.getAbsolutePath();
            callParser(selectedItem, filePath);
        }
        resultBeanList.add(resultBean);
    }
}

private void callParser(String parserType, String filePath)
{
    if ("Delta".equals(parserType))
    {
        PositionParserDelta deltaParser = new PositionParserDelta();
        deltaParser.getQuotes(filePath);
    }
    else if ("Gamma".equals(parserType))
    {
        PositionParserGamma gammaParser = new PositionParserGamma();
        gammaParser.getQuotes(filePath);
    }
}

现在,假设我们考虑Delta Parser,所以在该课程中,我有类似以下内容:

public class PositionParserDelta extends Base
{
    List<String[]> dataList = new ArrayList<String[]>();
    ContractManager contractManager = new ContractManager();

    public PositionParserDelta()
    {
    }

    public void getQuotes(String fileName)
    {
        try
        {
            Map<String, ContractSeries> gsLookup = contractManager.getContractsMap(ContractManager.VendorQuotes.KRT);
            CSVReader reader = new CSVReader(new FileReader(fileName), '\t');
            String[] header = reader.readNext();
            dataList = reader.readAll();
            List<Trade> tradeBeanList = new ArrayList<Trade>();
            for (String[] data : dataList)
            {
                //Some Business Logic
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

我的contractManager班级看起来像

    public class ContractManager extends Base
    {
        private List<Series> List = new ArrayList<Series>();
        private ContractSeries[] SeriesList = new Series[3];
        private ListingOps listingOps;

//This line throws error as faces class not found and it makes sense because am not registering this with faces-config.xml
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);

        public List<ContractSeries> getContracts(long orgId, HttpSession session)
        {
            log.info("Inside getContracts method call");
            if (false)
            {
                //Some Logic
            }
            else
            {
                try
                {
                    //Set session and get initialContext to retrieve contractSeries data from ejb calls
                    log.info("Trying to get allContractSeries data from listingOpsBean");
                    Series[] allSeries = deltaOps.findAllSeries(true);
                    Collections.addAll(contractList, allContractSeries);
                }
                catch (Exception ex)
                {

                }
            }
            return contractList;
        }

        public Map<String, ContractSeries> getContractsMap(VendorQuotes vendorQuotes)
        { //Some Logic
    }

但是在faces-config.xml文件中,

 <managed-bean>
        <managed-bean-name>fileUpload</managed-bean-name>
        <managed-bean-class>trade.UploadBlotterBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

所以基本上我的问题是

如果假设要从中调用其他类,managedBean那么应该如何在它们中定义它们,faces- config.xml并且既然是新手JSF,那么从managedBean这些类中调用其他类并在其中具有一些业务逻辑是否被视为良好实践?

此外,我需要确保我会保持我在获得UploadFile跨越ParserContractMapping类。

也,

是否所有东西都被“注册”为faces-config中的managed-bean?


阅读 144

收藏
2020-11-26

共1个答案

小编典典

不知道,但我想每个bean被注册为<managed-bean>faces-config。在扮演特定角色时,可以将其分类为

  1. Model Managed-Bean

  2. 支持Managed-Bean

  3. 控制器托管豆

  4. 支持托管豆

  5. 实用程序托管Bean …

通过给它们适当的名称,您可以在中区分它们faces-config。根据bean服务的工作,在其中设置作用域。

2020-11-26