JSF : ManagedBean, Good place to deal with Business Logic?

大憨熊 提交于 2020-01-11 11:43:10

问题


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

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

Here is the code:

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);
    }
}

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();
        }
    }
}

My contractManager Class looks like

    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
    }

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,

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?

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

Also,

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


回答1:


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

  2. Backing Managed-Bean

  3. Controller Managed-Bean

  4. Support Managed-Bean

  5. Utility Managed-Bean ...

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



来源:https://stackoverflow.com/questions/10283262/jsf-managedbean-good-place-to-deal-with-business-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!