我managedBean
爲fileUpload
,一旦文件被上傳,然後我需要根據從解析器下拉列表中選擇什麼樣的價值,然後在解析器正在創建的DetailsClass
對象,其中我打電話getDetails
方法爲特定的類,事情打電話給不同的解析器這裏需要注意的是,無論是parserClass
也不DetailsClass
faces-config.xml中註冊,我的問題在這裏JSF:ManagedBean,處理業務邏輯的好地方?
- 如果我想保持從
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
類獲取會話。
此外,
是一切「註冊」在臉上,配置管理,豆?
我的問題是否清楚或存在一些混淆,請告訴我,我可以在問題中添加更多肉類。 – Rachel 2012-04-23 16:11:50