我有managedBean
,fileUpload
一旦文件被上传,我需要根据从解析器下拉列表中选择的值调用不同的解析器,然后在解析器中为该特定类创建DetailsClass
调用方法的对象,这里要注意的是既不是也不是在 faces-config.xml 中注册,我的问题是getDetails
parserClass
DetailsClass
- 如果我想维护从
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
的会话Parser
。ContractMapping
还,
是否所有内容都在 faces-config 中“注册”为托管 bean?