English 中文(简体)
JSF - create a generic confirmation page (dynamic navigation)
原标题:
  • 时间:2009-12-15 16:28:25
  •  标签:
  • java
  • jsf
  • el

I ll try to explain my problem. I use the JSF 1.2 implementation of IBM in a very very rigid environment (company layer, and strict constraints -don t add library etc.-).

I want to create a simple page for confirmation that can be reused (kind of generic). For example : 1-page1.jsp > click on button 2-pageConfirmation.jsp > 2 buttons Yes or No 3-click on No > go back / 3bis-click on Yes > go to someAction then go to page2.jsp

So I wanted to store in session the things I need, where I come from, where I want to go. But here s the problem : How can I invoke the action I want? The name is dynamic. Let s suppose I m in the controller after I click on "Yes", I want to call the "myMethodAction" from its name which I stored in session. I thought about evaluating it using the expression-language api, but i m blocked because I can t access the ELContext, and am not allowed to use the el package (el-api.jar).

To sum-up, after the click, I m trying to invoke an action/method and then redirect to a page, instead of calling a page.

Do anyone have some magic ideas? Or am I just totally wrong?

Thanks in advance.

最佳回答

You can create a fully independent managed bean ConfirmationBean and put it in the request scope. You can access it in the "parent" bean by under each the managed property injection facility or just create it yourself and put it in request map.

The first way requires lot of work in faces-config.xml if you want to associate it with lot of other beans, so I don t think this is very attractive.

The second way only requires a bit of (reuseable) code in the action method. I ll give an example:

public String submit() {
    ConfirmationBean conformationBean = new ConfirmationBean();
    confirmationBean.setOutcome("outcome"); // Set navigation case outcome where it should return back.
    FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("confirmationBean", confirmationBean);
    return "confirm";
}

and the confirm.jsf should have the "outcome" in a hidden field, e.g.

<h:selectBooleanCheckbox value="#{confirmationBean.confirm}" />
<h:inputHidden value="#{confirmationBean.outcome}" />
<h:commandButton value="confirm" action="#{confirmationBean.submit}" />

the action method of ConfirmationBean should look like:

public String submit() {
    if (confirm) {
        return outcome;
    } else {
        return "confirm";
    }
}

or if you re a fan of ternary operators ;)

public String submit() {
    return confirm ? outcome : "confirm";
}
问题回答

So I come with two options : -Override the navigation management of JSF like this solution to enable EL in the to-view-id tag

(next solution in the next message, limited because I m new on stackoverflow)

Second solution: -Call the action (string stored in the confirmationBean in session) invoking EL like this solution

That could be useful for some people here, but unavailable for me as I m not allowed to import libraries (el-api).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签