Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF file download NullPointerException with view Parameter
    primarykey
    data
    text
    <p>I hope I get this one straight:</p> <p>I try to create an <em>JSF 2.0</em> Page optimized for mobile use on <em>Tomcat 7</em>. Therefore I use <em>Mojarra 2.1.6</em> and <em>PrimeFaces</em> (Core 3.1.1 and Mobile 0.9.1).</p> <h3>My scenario:</h3> <ol> <li>User visits simplified URL, e.g. www.someurl.eu/view.xhtml/12345678</li> <li>ServletFilter rewrites Url to www.someurl.eu/view.xhtml?id=12345678</li> <li>Url Parameter gets provided to ManagedBean</li> <li>Necessery Data gets evaluated and published to my view.xhtml</li> <li>On my view.xhtml a button for download is provided</li> </ol> <h3>My problem:</h3> <p>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.</p> <h3>My ManagedBean:</h3> <pre><code>@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()); } } } </code></pre> <h3>My view.xhtml:</h3> <pre><code>&lt;h:form&gt; &lt;pm:page title="Download"&gt; &lt;pm:view&gt; &lt;pm:content&gt; ... &lt;p:commandButton value="Download" ajax="false"&gt; &lt;p:fileDownload value="#{viewBean.file}" /&gt; &lt;/p:commandButton&gt; &lt;/pm:content&gt; &lt;/pm:view&gt; &lt;/pm:page&gt; &lt;/h:form&gt; </code></pre> <p></p> <h3>Last but not least, my Filter:</h3> <pre><code>@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/") &amp;&amp; (!url.contains("?id=") &amp;&amp; !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); } } </code></pre> <p>My Filter is mapped to any URL <code>&lt;url-pattern&gt;/*&lt;/url-pattern&gt;</code></p> <p>I've also tried to set the ManagedBean to <em>@SessionScoped</em>. 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.</p> <p>Maybe I'm too blind to see the obvious solution.</p> <p>Any help would be appreciated! <strong>Thanks in advance!</strong></p> <p>P.S. The exception</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload