JSF:ManagedBean,处理业务逻辑的好地方吗? [英] JSF : ManagedBean, Good place to deal with Business Logic?

查看:65
本文介绍了JSF:ManagedBean,处理业务逻辑的好地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have managedBean for fileUpload, once file is uploaded then i need to call different parsers based on what value is selected from the parser dropdown and then in parser am creating object of DetailsClass where am calling getDetails method for that particular class, thing to note here is that neither parserClass nor DetailsClass is registered in faces-config.xml, my question here is

  • 如果我想维护从FileUpload类到Parser类到DetailsClass的会话信息,那么我应该在faces-config.xml中定义它,但是应该如何定义parser类和DetailsClass?被定义为managedBean还是其他?
  • If i want to maintain session information from FileUpload class to Parser class to DetailsClass then I should be defining it in faces-config.xml but how should parser class and DetailsClass be defined, should it be defined as managedBean or like something else?

这是代码:

在我的managedBean类中,我有两个函数,分别为fileUploadcallParser,如图所示:

In my managedBean class i have two function, fileUpload and callParser as shown:

 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,所以在该类中,我有类似的东西:

Now, let say we consider Delta Parser, so in that class I have something like:

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文件中,

But in faces-config.xml file,

 <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>

所以基本上我的问题是,

So basically my question is,

如果假设要从managedBean调用其他类,那应该怎么做 它们是在faces-config.xml中定义的,并且由于是JSF的新功能,因此 从managedBean调用其他类并从事一些业务 这些课程中的逻辑被认为是好的做法?

if suppose am calling other classes from managedBean then how should they be defined in faces-config.xml and since am new to JSF, is calling other classes from managedBean and having some business logic in those classes considered good practice?

我还需要确保我保持在UploadFileContractMapping类中在UploadFile中获得的会话.

Also i need to make sure that i maintain session that i obtain in UploadFile across Parser and ContractMapping class.

也是

是否所有内容都已在"faces-config"中注册"为托管Bean?

Is everything is 'registered' as managed-bean in faces-config ?

推荐答案

不确定,但是我认为每个bean在faces-config中都注册为<managed-bean>.在扮演特定角色时,可以将其归类为

Not sure but I think every bean is registered as <managed-bean> in faces-config. On the specific role, a class plays, they can be categorized into

  1. Model Managed-Bean

支持Managed-Bean

Backing Managed-Bean

控制器托管Bean

支持Managed-Bean

Support Managed-Bean

Utility Managed-Bean ...

Utility Managed-Bean ...

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

By giving them appropriate name you can distinguish them in faces-config. As per the work served by bean , set there scopes.

这篇关于JSF:ManagedBean,处理业务逻辑的好地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆