1

我希望我能直截了当地说:

我尝试在Tomcat 7上创建一个针对移动使用优化的JSF 2.0页面。因此我使用Mojarra 2.1.6PrimeFaces(Core 3.1.1 和 Mobile 0.9.1)。

我的场景:

  1. 用户访问简化 URL,例如 www.someurl.eu/view.xhtml/12345678
  2. ServletFilter 将 Url 重写为 www.someurl.eu/view.xhtml?id=12345678
  3. 将 URL 参数提供给 ManagedBean
  4. 必要的数据被评估并发布到我的 view.xhtml
  5. 在我的 view.xhtml 上提供了一个下载按钮

我的问题:

通过单击按钮,当 ManagedBean 设置为 @RequestScoped 时,服务器会向我显示 NPE。我的假设是,通过单击按钮会触发一个新请求,我的 URL 参数会丢失并且下载文件不再可用。我不知道我是否正确,但如果是这样,我不知道如何优雅地解决这个问题。

我的托管豆:

@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());
        }
    }
}

我的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>

最后但并非最不重要的是,我的过滤器:

@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);
    }
} 

我的过滤器映射到任何 URL<url-pattern>/*</url-pattern>

我还尝试将 ManagedBean 设置为@SessionScoped。如果我这样做,下载工作正常,但由于会话范围的 ManagedBean 中的持久数据,用户无法更改 URL。

也许我太盲目了,看不到明显的解决方案。

任何帮助,将不胜感激!提前致谢!

PS 例外

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)
4

1 回答 1

2

使用视图范围。

@ManagedBean
@ViewScoped

也可以看看:

于 2012-03-06T13:38:38.497 回答