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



我已经managedBeanfileUpload,一旦文件上传,那么我需要根据从解析器下拉列表中选择的值调用不同的解析器,然后在解析器中创建DetailsClass的对象,其中我为该特定类调用getDetails方法,这里要注意的是,parserClassDetailsClass都没有在faces-config中注册.xml, 我的问题是

  • 如果我想维护从FileUpload类到Parser类到DetailsClass的会话信息,那么我应该在faces-config.xml中定义它,但是应该如何定义parser类和DetailsClass,它应该定义为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
    }

但是在面部配置.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打电话给其他班级并有一些业务 这些课程中的逻辑被认为是良好做法?

此外,我还需要确保我保持我在ParserContractMapping课堂上获得的UploadFile课程。

所有内容是否都"注册"为人脸配置中的托管豆?

不确定,但我认为每个豆子都faces-config中注册为<managed-bean>。在具体角色上,一个班级扮演,他们可以分为

  1. Model Managed-Bean

  2. 支持托管 Bean

  3. 控制器管理的 Bean

  4. 支持托管 Bean

  5. 实用程序管理的Bean ...

通过给它们适当的名称,您可以在faces-config中区分它们。根据 bean 提供的工作,设置范围。

最新更新