gpt4 book ai didi

java - JSF : ManagedBean, 处理业务逻辑的好地方?

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:44 27 4
gpt4 key购买 nike

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

  • 如果我想维护从 FileUpload 类到 Parser 类到 DetailsClass 的 session 信息,那么我应该在 faces-config.xml 中定义它,但是应该如何定义 parser 类和 DetailsClass ,它应该定义为 managementBean 还是其他东西?

这是代码:

在我的 ManagedBean 类中,我有两个函数,fileUploadcallParser,如下所示:

 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>

所以基本上我的问题是,

if suppose am calling other classes from managedBean then how should they be defined in faces-config.xml and since am new to JSF, is calling other classes from managedBean and having some business logic in those classes considered good practice?

此外,我需要确保在 ParserContractMapping 类中维护在 UploadFile 中获得的 session 。

另外,

Is everything is 'registered' as managed-bean in faces-config ?

最佳答案

不确定,但我认为每个 bean 都注册为 <managed-bean>faces-config 。就具体角色而言,一个类所扮演的角色,可以分为

  1. Model Managed-Bean

  2. 支持托管 Bean

  3. Controller 托管 Bean

  4. 支持托管 Bean

  5. 实用程序托管 Bean ...

通过给它们适当的名称,您可以在 faces-config 中区分它们。根据 bean 服务的工作,设置范围。

关于java - JSF : ManagedBean, 处理业务逻辑的好地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10283262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com