1

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

  • 如果我想维护从FileUpload一个类到另一个Parser类的会话信息,DetailsClass那么我应该在其中定义它,faces-config.xml但是应该如何定义parser类和DetailsClass定义,它应该被定义为managedBean还是像其他东西一样?

这是代码:

在我的 managedBean 类中,我有两个函数,fileUpload如图callParser所示:

 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 中“注册”为托管 bean?

4

1 回答 1

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.

于 2012-04-23T15:40:03.423 回答