问题
I hope I get this one straight:
I try to create an JSF 2.0 Page optimized for mobile use on Tomcat 7. Therefore I use Mojarra 2.1.6 and PrimeFaces (Core 3.1.1 and Mobile 0.9.1).
My scenario:
- User visits simplified URL, e.g. www.someurl.eu/view.xhtml/12345678
- ServletFilter rewrites Url to www.someurl.eu/view.xhtml?id=12345678
- Url Parameter gets provided to ManagedBean
- Necessery Data gets evaluated and published to my view.xhtml
- On my view.xhtml a button for download is provided
My problem:
By clicking the button the server shows me a NPE when the ManagedBean is set to @RequestScoped. My assumption is that by clicking the button a new request is fired, my URL-parameter gets lost and the download file is not available anymore. I don't know if I'm right but if so I don't know how to solve this one elegant.
My ManagedBean:
@ManagedBean
@RequestScoped
public class ViewBean {
// Elements
...
private StreamedContent file;
...
// Getter and Setter
...
public StreamedContent getFile() {
invalidateSession();
return file;
}
public void setFile(StreamedContent file) {
this.file = file;
}
...
// Init
@PostConstruct
public void init() {
//read URL-Parameter
urlSdbac = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
// validate
if(urlSdbac == null || urlSdbac.equals("") || urlSdbac.length() != 8) {
urlSdbac = "invalid";
}
// get Data
else {
SdbCustomerData cd = getFileMetadata(urlSdbac);
if(cd == null) {
this.name = "invalid code";
this.language = "no language options found";
}
else {
...
// prepare File for Download
this.file = new Helper().getWebDavFile(cd.getCid(), cd.getName());
}
}
}
My view.xhtml:
<h:form>
<pm:page title="Download">
<pm:view>
<pm:content>
...
<p:commandButton value="Download" ajax="false">
<p:fileDownload value="#{viewBean.file}" />
</p:commandButton>
</pm:content>
</pm:view>
</pm:page>
</h:form>
Last but not least, my Filter:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
String url = request.getRequestURI();
Pattern sdbac = Pattern.compile("[A-Za-z0-9]{8}");
Matcher fit = null;
// check Page
if(url.contains("view.xhtml/") && (!url.contains("?id=") && !url.endsWith("view.xhtml")) ) {
String code = url.substring(url.indexOf("view.xhtml/")+11, url.length());
// validate Param?
fit = sdbac.matcher(code);
if(fit.matches()) {
// redirect
response.sendRedirect("/getSDS/view.xhtml?id=" + code);
}
else {
// redirect
response.sendRedirect("/getSDS/view.xhtml?id=invalid");
}
}
else {
chain.doFilter(req,res);
}
}
My Filter is mapped to any URL <url-pattern>/*</url-pattern>
I've also tried to set the ManagedBean to @SessionScoped. If I do so the download works fine but the user isn't able to change the URL because of the persitent data in the session scoped ManagedBean.
Maybe I'm too blind to see the obvious solution.
Any help would be appreciated! Thanks in advance!
P.S. The exception
java.lang.NullPointerException
org.primefaces.component.filedownload.FileDownloadActionListener.processAction(FileDownloadActionListener.java:58)
javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
javax.faces.component.UICommand.broadcast(UICommand.java:300)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
eu.qualisys.getsds.filter.SDBACParamFilter.doFilter(SDBACParamFilter.java:53)
回答1:
Use the view scope.
@ManagedBean
@ViewScoped
See also:
- How to choose the right bean scope?
来源:https://stackoverflow.com/questions/9584736/jsf-file-download-nullpointerexception-with-view-parameter