我已经managedBean为fileUpload,一旦文件被上传,然后我需要调用基于从解析器下拉列表中选择什么样的价值不同的解析器,然后解析器正在创建的对象DetailsClass,其中我打电话getDetails的那个特定的类方法,这里要注意的一点是,既不parserClass也不DetailsClass是在faces- config.xml中注册的,我的问题是
managedBean
fileUpload
DetailsClass
getDetails
parserClass
FileUpload
Parser
faces-config.xml
parser
这是代码:
在我managedBean类我有两个功能,fileUpload并callParser如图所示:
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,所以在该课程中,我有类似以下内容:
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班级看起来像
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这些类中调用其他类并在其中具有一些业务逻辑是否被视为良好实践?
faces- config.xml
JSF
此外,我需要确保我会保持我在获得UploadFile跨越Parser和ContractMapping类。
UploadFile
ContractMapping
也,
是否所有东西都被“注册”为faces-config中的managed-bean?
不知道,但我想每个bean被注册为<managed-bean>在faces-config。在扮演特定角色时,可以将其分类为
<managed-bean>
faces-config
Model Managed-Bean
支持Managed-Bean
控制器托管豆
支持托管豆
实用程序托管Bean …
通过给它们适当的名称,您可以在中区分它们faces-config。根据bean服务的工作,在其中设置作用域。