不同项目中 JSF 页面之间的数据传输

发布于 2024-10-03 14:01:17 字数 3103 浏览 2 评论 0原文

两个单独的项目(可能是不同的服务器)中的两个 JSF/JSF 页面和关联的托管 bean 将它们命名为 PageA.jsp 和 PageB.jsp,并使用 BackingBeanA.java 和 BackingBeanB.java(同样,单独的项目)

期望是从 PageA.jsp 重定向到 PageB.jsp 并传递一个对象 (FilterData)

尝试了以下操作:

BackingBeanA.java

public String startPageB() {

    try {

    FilterData filterData = ...

        FacesContext facesContext = FacesContext.getCurrentInstance();

        HttpServletRequest request = (HttpServletRequest) 
            facesContext.getExternalContext().getRequest();
        request.setAttribute("FILTERDATA",filterData);

        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setRequest(request);

        externalContext.redirect("http://localhost:8080/PageB/");

    } 
    catch (IOException e) {
        e.printStackTrace();
        System.out.println(e);
    }

    return "redirectedData";
}

BackingBeanB.java(在单独的项目中,可以是单独的服务器)

public String getBeanAData() {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) 
        facesContext.getExternalContext().getRequest();

    FilterData newFilterData = (FilterData) 
        request.getAttribute("FILTERDATA");

... do stuff
return null;
}

结果: 然而, getBeanAData 被调用: filterData 在 BackingBeanA 中不为 null,但在 BackingBeanB 中为 null - 数据未传输。

关于如何正确进行数据传递有什么想法吗?


我应该在原来的问题中包含什么:

Java 1.6.0_22-b03 JSF 1.2 JSTL 1.2 日食 3.6.0(赫利俄斯) Tomcat 6.0.28(也需要在Weblogic上运行) IE 7.0.5730.13 Firefox:3.6.12

如果可能的话,尝试是纯 JSF,不需要 HTTP(但可能),没有 JavaScript(句号)。

我如何解决这个问题(目前已经足够好了):

从父网页 -

...

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        String initialUrl = externalContext.getInitParameter("RedirectUrl");

        String requestDataA = "?ValueA=" + activity.getID();
        String requestDataB = "&ValueB=" + activity.getName();

        redirectUrl = initialUrl + requestDataA + requestDataB;

        externalContext.redirect(redirectUrl);
...

从重定向的网页 -

...
        String ownerName = (String) FacesContext.getCurrentInstance().
           getExternalContext().getRequestParameterMap().get("ValueB");

        String itemId = (String) FacesContext.getCurrentInstance().
           getExternalContext().getRequestParameterMap().get("ValueA");
...

结果是这对于字符串来说效果很好。现在这样就可以了。

如果可能的话,我稍后想要什么:

The same thing, but passing an object.  
I know I cannot do that on the request line, but I thought there was a way
similar to a standard HTTP setup where a request attribute is set and the
destination page gets it with a doPost method (do I have this wrong?).

The BalusC answer indicates this is not possible.  
So is it really not possible to have a JSF page redirect (starup or whatever) 
to an external page, and pass it an object without going to shared storage?

谢谢, 约翰

Two JSF/JSF pages and associated managed beans in two separate projects (maybe different servers)
Name them PageA.jsp and PageB.jsp with BackingBeanA.java and BackingBeanB.java (again, separate projects)

Desire is to redirect from PageA.jsp to PageB.jsp and pass an object (FilterData)

The following was tried:

BackingBeanA.java

public String startPageB() {

    try {

    FilterData filterData = ...

        FacesContext facesContext = FacesContext.getCurrentInstance();

        HttpServletRequest request = (HttpServletRequest) 
            facesContext.getExternalContext().getRequest();
        request.setAttribute("FILTERDATA",filterData);

        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setRequest(request);

        externalContext.redirect("http://localhost:8080/PageB/");

    } 
    catch (IOException e) {
        e.printStackTrace();
        System.out.println(e);
    }

    return "redirectedData";
}

BackingBeanB.java (in a separate project, could be separate server)

public String getBeanAData() {

    FacesContext facesContext = FacesContext.getCurrentInstance();

    HttpServletRequest request = (HttpServletRequest) 
        facesContext.getExternalContext().getRequest();

    FilterData newFilterData = (FilterData) 
        request.getAttribute("FILTERDATA");

... do stuff
return null;
}

Result:
getBeanAData is called, however:
filterData was not null in BackingBeanA, but was null in BackingBeanB - data was not transferred.

Any ideas as to how to properly do this data passing?


What I should have included in the original question:

Java 1.6.0_22-b03
JSF 1.2
JSTL 1.2
Eclipse 3.6.0 (Helios)
Tomcat 6.0.28 (needs to run also on Weblogic)
IE 7.0.5730.13
Firefox: 3.6.12

Attempt is to be pure JSF if possible, no HTTP desired (but possible), no JavaScript (period).

How I solved the problem (good enough for now):

From parent web page -

...

        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();

        String initialUrl = externalContext.getInitParameter("RedirectUrl");

        String requestDataA = "?ValueA=" + activity.getID();
        String requestDataB = "&ValueB=" + activity.getName();

        redirectUrl = initialUrl + requestDataA + requestDataB;

        externalContext.redirect(redirectUrl);
...

From redirected web page -

...
        String ownerName = (String) FacesContext.getCurrentInstance().
           getExternalContext().getRequestParameterMap().get("ValueB");

        String itemId = (String) FacesContext.getCurrentInstance().
           getExternalContext().getRequestParameterMap().get("ValueA");
...

Result is that this works great - for strings. This is OK for now.

What I would like for later if possible:

The same thing, but passing an object.  
I know I cannot do that on the request line, but I thought there was a way
similar to a standard HTTP setup where a request attribute is set and the
destination page gets it with a doPost method (do I have this wrong?).

The BalusC answer indicates this is not possible.  
So is it really not possible to have a JSF page redirect (starup or whatever) 
to an external page, and pass it an object without going to shared storage?

Thanks,
John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2024-10-10 14:01:17

重定向基本上指示客户端创建一个全新的请求。原始请求,包括其所有参数和属性都将丢失。另外,请求属性不会发送给客户端。

如果这些 webapp 上下文不是共享的(可以在服务器级别配置,例如在 Tomcat 中,使用 crossContext 关键字查看),那么最好的选择是将其作为请求参数传递。如果数据太大而无法作为参数发送,则将其存储在共享数据源(例如数据库)中的某个位置,并将其标识符作为参数传递。

externalContext.redirect("http://localhost:8080/PageB?filterData=" + filterData);

在另一侧,您可以将其设置为托管属性。

public class BackingBeanB {

    @ManagedProperty(value="#{param.filterData}")
    private String filterData;

    // ...
}

(注意,我假设是 JSF 2.0,对于 JSF 1.x,您需要在 faces-config.xml 中使用 >

A redirect basically instructs the client to create a brand new request. The original request, including all of its parameters and attributes will get lost. Plus, request attribtues are not been sent to the client.

If those webapp contexts are not shared (which is configureable at server level, in for example Tomcat, look around using crossContext keyword), then your best bet is to pass it as request parameter. If the data is too large to being sent as parameter, then store it somewhere in a shared datasource (e.g. database) and pass its identifier as parameter instead.

externalContext.redirect("http://localhost:8080/PageB?filterData=" + filterData);

In the other side you can then set it as managed property.

public class BackingBeanB {

    @ManagedProperty(value="#{param.filterData}")
    private String filterData;

    // ...
}

(note, I am assuming JSF 2.0, for JSF 1.x you need <managed-property> in faces-config.xml instead)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文