简体   繁体   English

JSF:导航

[英]JSF: navigation

I have to warn you: the question may be rather silly, but I can't seem to wrap my head around it right now. 我必须警告你:问题可能相当愚蠢,但我现在似乎无法绕过它。

I have two managed beans, let's say A and B: 我有两个托管bean,比方说A和B:

class A
{
    private Date d8; // ...getters & setters
    public String search()
    {
        // search by d8
    }
}

class B
{
    private Date d9; //...getters & setters
    public String insert()
    {
         // insert a  new item for date d9
    }    
}

and then I have two JSP pages, pageA.jsp (the search page) and pageB.jsp (the input page). 然后我有两个JSP页面, pageA.jsp (搜索页面)和pageB.jsp (输入页面)。 What I would like to do is placing a commandbutton in pageB so to open the search page pageA passing the parameter d9 somehow, or navigating to pageA directly after b.insert() . 我想要做的是在pageB中放置一个命令 按钮,以便打开搜索页面页面A以某种方式传递参数d9 ,或者在b.insert()之后直接导航到pageA What I would like to do is showing the search result after the insertion. 我想要做的是在插入后显示搜索结果。

Maybe it's just that I can't see the clear, simple solution, but I'd like to know what the best practice might be here, also... 也许只是因为我无法看到清晰,简单的解决方案,但我想知道这里最好的做法是什么,也......

I though of these possible solutions: 我想这些可能的解决方案:

  1. including **A** in **B** and linking the command button with **basearch** 包括** A ** in ** B **并将命令按钮与** basearch相关联**
  2. passing **d9** as a **hiddenInput** and adding a new method **searchFromB** in **A** (ugly!) 将** d9 **作为** hiddenInput **并在** A **中添加新方法** searchFromB **(丑陋!)
  3. collapsing the two beans into one 将两个豆子折叠成一个

JSF 1.1/1.2 raw doesn't provide an easy way to do this. JSF 1.1 / 1.2 raw不提供简单的方法。 Seam/Spring both have ways around this and there are a couple of things you can do. Seam / Spring都有办法解决这个问题,你可以做几件事。 JSF 2 should also have solutions to this once it is released. 一旦发布,JSF 2也应该有解决方案。

Probably the easiest and most expedient would be to collapse the two beans into one and make it session scoped. 可能最容易也是最方便的是将两个bean合并为一个并使其成为会话范围。 The worry, of course, is that this bean will not get removed and stay in session until the session times out. 当然,担心的是这个bean不会被删除并保持会话直到会话超时。 Yay Memory leaks! 耶内存泄漏!

The other solution would be to pass the date on as a GET parameter. 另一种解决方案是将日期作为GET参数传递。 For instance, you action method could call the 例如,你的动作方法可以调用

FacesContext.getCurrentInstance().getExternalContext().redirect("pageB?d9=" + convertDateToLong(d9));

and then get the parameter on the other side. 然后在另一侧获取参数。

You should configure the navigation flow in faces-config.xml. 您应该在faces-config.xml中配置导航流。 In ideal scenario you would return a "status" message which would decide the flow. 在理想情况下,您将返回“状态”消息,该消息将决定流程。 Read more at following link: http://www.horstmann.com/corejsf/faces-config.html http://publib.boulder.ibm.com/infocenter/rtnlhelp/v6r0m0/index.jsp?topic=/com.businessobjects.integration.eclipse.doc.devtools/developer/JSF_Walkthrough8.html 阅读以下链接: http//www.horstmann.com/corejsf/faces-config.html http://publib.boulder.ibm.com/infocenter/rtnlhelp/v6r0m0/index.jsp?topic=/com。 businessobjects.integration.eclipse.doc.devtools /开发商/ JSF_Walkthrough8.html

As far as passing the values from one page to another is concerned you can use backing beans. 就将值从一个页面传递到另一个页面而言,您可以使用支持bean。 More about backing beans here: http://www.netbeans.org/kb/articles/jAstrologer-intro.html http://www.coderanch.com/t/214065/JSF/java/backing-beans-vs-managed-beans 有关支持bean的更多信息, 访问http//www.netbeans.org/kb/articles/jAstrologer-intro.html http://www.coderanch.com/t/214065/JSF/java/backing-beans-vs-managed -豆子

Hope i have understood and answered correctly to your question 希望我已经理解并正确回答了你的问题

Way to share values between beans 在bean之间共享值的方法

FacesContext facesContext = FacesContext.getCurrentInstance();
Application app = facesContext.getApplication();
ExpressionFactory elFactory = app.getExpressionFactory();
ELContext elContext = facesContext.getELContext();
ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class);
return valueExp.getValue(elContext);

In above code "expression" would be something like #{xyzBean.beanProperty} Since JSF uses singleton instances, you should be able to access the values from other beans. 在上面的代码中,“expression”类似于#{xyzBean.beanProperty}因为JSF使用单例实例,所以你应该能够访问其他bean的值。 If you find more details on this technique, I am sure you'll get what you are looking for. 如果你找到关于这种技术的更多细节,我相信你会得到你想要的东西。

  1. Add commandButton action attribute referencing to B'insert method 添加引用B'insert方法的commandButton操作属性

<h:commandLink action="#{b.insert}" value="insert"/>

  1. In B'insert method,add d9 parameter as request parameter. 在B'insert方法中,添加d9参数作为请求参数。 Then return an arbitrary string from insert method. 然后从insert方法返回一个任意字符串。

FacesContext fc = FacesContext.getCurrentInstance(); FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getRequestMap().put("d9", d9); fc.getExternalContext()。getRequestMap()。put(“d9”,d9);

  1. Then go to faces context and add navigation from B to A with "from-outcome" as the arbitrary String you returned from insert method. 然后转到faces上下文并添加从B到A的导航,其中“from-outcome”作为从insert方法返回的任意String。 But don't add redirect tag to navigation tags as it will destroy the request coming from B and the parameter you added (d9) will be cleared. 但是不要将重定向标记添加到导航标记,因为它会破坏来自B的请求,并且您添加的参数(d9)将被清除。

<from-outcome>return string of insert method</from-outcome>
<to-view-id>address of A</to-view-id>

  1. Then you might get the "d9" in A class by fetching it from request map at its constructor or in a place where its more appropriate (getters). 然后你可以在A类中获取“d9”,方法是从构造函数的请求图中或在更合适的地方(getter)中获取它。 You might add it into a session scope or place it to a hidden variable if you want to keep track of it later. 如果要稍后跟踪它,可以将其添加到会话范围中或将其放置到隐藏变量中。

in class A, when page is navigated, A should be initialized as it will be referenced. 在A类中,当导航页面时,应该初始化A,因为它将被引用。

FacesContext fc = FacesContext.getCurrentInstance(); FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getRequestMap().get("d9", d9); fc.getExternalContext()。getRequestMap()。get(“d9”,d9);

Sorry i cant give full code, as i have no ide at here, its internet machine at work. 对不起,我不能给完整的代码,因为我在这里没有ide,它的互联网机器在工作。 I could not give details therefore. 因此我无法提供细节。

In my opinion, the simplest way is 3-rd option - have both query and insert methods in same class. 在我看来,最简单的方法是第3个选项 - 在同一个类中同时使用查询和插入方法。 And you can do something like that: 你可以这样做:

public String query () {
//... 
}

public String Insert() {  
 //insert
return Query(); }

If your classes are managed Beans you can load class A from class B and call A.query() in your insert method at the end. 如果你的类是托管Beans,你可以从B类加载A类,并在最后的insert方法中调用A.query() Also class A can have A级也可以

<managed-bean-scope>session</managed-bean-scope>

parameter in faces-config.xml and it wouldn't be instantiated again when loaded. faces-config.xml中的参数,加载时不会再次实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM