我有managedBean
fileUpload
,一旦文件被上传,我需要根据从解析器下拉列表中选择的值调用不同的解析器,然后在解析器中创建{{1}的对象在那里调用该特定类的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中,所有内容都被“注册”为managed-bean吗?
答案 0 :(得分:1)
不确定,但我认为每个bean都在<managed-bean>
中注册为faces-config
。关于特定角色,一个类玩,它们可以分类为
Model Managed-Bean
支持Managed-Bean
Controller Managed-Bean
支持Managed-Bean
Utility Managed-Bean ...
通过给出适当的名称,您可以在faces-config
中区分它们。根据bean提供的工作,设置范围。