JSF:ManagedBean,好地方处理业务逻辑?(JSF : ManagedBean, Good

2019-07-29 02:46发布

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

  • 如果我想保持从会话信息FileUploadParserDetailsClass那么我就应该将其定义在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
    }

但在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类。

也,

是一切“注册”在脸上,配置管理,豆?

Answer 1:

不知道,但我想每个bean被注册为<managed-bean>faces-config 。 在特定的角色,一类游戏,它们可分为

  1. Model Managed-Bean

  2. 备份管bean

  3. 控制器管理豆

  4. 支持托管bean

  5. 电力公司管理的豆...

通过给他们适当的名称,你可以区分它们faces-config 。 按照由豆服务的工作,设置有作用域。



文章来源: JSF : ManagedBean, Good place to deal with Business Logic?