问题
I have managedBean
for fileUpload
, once file is uploaded then i need to call different parsers based on what value is selected from the parser dropdown and then in parser am creating object of DetailsClass
where am calling getDetails
method for that particular class, thing to note here is that neither parserClass
nor DetailsClass
is registered in faces-config.xml, my question here is
- If i want to maintain session information from
FileUpload
class toParser
class toDetailsClass
then I should be defining it infaces-config.xml
but how shouldparser
class andDetailsClass
be defined, should it be defined asmanagedBean
or like something else?
Here is the code:
In my managedBean class i have two function, fileUpload
and callParser
as shown:
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);
}
}
Now, let say we consider Delta Parser
, so in that class I have something like:
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();
}
}
}
My contractManager
Class looks like
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
}
But in faces-config.xml file,
<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>
So basically my question is,
if suppose am calling other classes from
managedBean
then how should they be defined infaces-config.xml
and since am new toJSF
, is calling other classes frommanagedBean
and having some business logic in those classes considered good practice?
Also i need to make sure that i maintain session that i obtain in UploadFile
across Parser
and ContractMapping
class.
Also,
Is everything is 'registered' as managed-bean in faces-config ?
回答1:
Not sure but I think every bean is registered as <managed-bean>
in faces-config
. On the specific role, a class plays, they can be categorized into
Model Managed-Bean
Backing Managed-Bean
Controller Managed-Bean
Support Managed-Bean
Utility Managed-Bean ...
By giving them appropriate name you can distinguish them in faces-config
. As per the work served by bean , set there scopes.
来源:https://stackoverflow.com/questions/10283262/jsf-managedbean-good-place-to-deal-with-business-logic