ActionListener and Navigation Problem

I am using ActionListener and after getting values I am forwarding the

page, but its getting to same page, below is my bean code.

/**

* this method edit the survey

* @return

*/

public String doEditSurvey(ActionEvent event){

//first get the question ID,questionType ,Question and Releated Answers

String surveyQuestionID = (String) event.getComponent().getAttributes().get("surveyQuestionID");

System.err.println("the question id is :"+surveyQuestionID);

String surveyQuestionDesc = (String) event.getComponent().getAttributes().get("surveyQuestionDesc");

String questionType = (String) event.getComponent().getAttributes().get("questionType");

ArrayList surveyQuestionList = (ArrayList) event.getComponent().getAttributes().get("surveyQuestionList");

Iterator it = surveyQuestionList.iterator();

while(it.hasNext()){

SelectItem selectItem = (SelectItem)it.next();

setAnswer(selectItem.getLabel());

setAnswer("\n");

}

setQuestion(surveyQuestionDesc);

setSelectedType(questionType);

return "questionTemplate";

}

and my facesconfig.xml file

<navigation-rule>

<navigation-case>

<from-outcome>questionTemplate</from-outcome>

<to-view-id>/content/questionSelection.jsp</to-view-id>

</navigation-case>

</navigation-rule>

so when I return this string , Its not getting their, any idea .

Thanks

Srikanth

[1546 byte] By [srikanthga] at [2007-11-27 2:51:26]
# 1

An ActionListener does not return a String. However, an action method does. =)

This is a very easy thing to misunderstand about commandButtons and commandLinks. There are two ways to directly execute some actions after a submit click: ActionListener and action method. So lets talk about the differences.

The action method does not accept an ActionEvent. In fact, it doesn't accept any parameters at all, but it does return a String. The String returned from an action method is used in the faces-config.xml to determine the next page to view. Typically, the actions that happen in an action method are done to determine what page should be viewed next and to configure the state for the next page (if necessary).

The ActionListener does accept an ActionEvent, but doesn't return a String. An ActionListener does not get to determine the next page to be viewed. Instead, it performs any miscellaneous actions that you might need to execute after a "submit" click. ActionListeners can be used to configure the state for the next page... But typically, it really is for miscellaneous operations. For example, I have an application with an ActionListener that performs some logging of the current status of my app.

On the commandLink/Button, the action method is tied to the action attribute and the ActionListener is tied to the actionlistener attribute. If you do not need to determine the next page, or setup the application for the next page, then you can simply supply a String to the action attribute. i.e. action="questionTemplate". If you do that, you do not need to create an action method.

Hope this helps,

CowKing

IamCowKinga at 2007-7-12 3:24:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
> An ActionListener does not return a String. However,> an action method does. =)This is partly true. An action method can also be declared as void.
BalusCa at 2007-7-12 3:24:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Balu, their is any way I get get working using ActionListener.ThanksSrikanth
srikanthga at 2007-7-12 3:24:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Yes. Navigate manually:public void actionListener(ActionEvent event) throws IOException {

// Implement.

// Then, at the end:

FacesContext context = FacesContext.getCurrentInstance();

context.getExternalContext().redirect("page.jsf"); // Redirect to page.jsf

context.responseComplete(); // Finalize response, avoid IllegalStateExceptions.

}

BalusCa at 2007-7-12 3:24:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

my friend an action listener method is always called before action method.always remeber whenever an event is throw it would be handled by eventListener method first and then control is transferred to the other methods.

Therefore what ever changes you wanna do the your MODEL layer you can.Utilmately the action method is called to returned by action method as an outcome to fwd/redirect the page to another...

here is an example for you

Sample JSP View:

==============

<f:view>

..............................................

...............................................

..............................................

...............................................

<h:form>

................................................

...............................................

..............................................

...............................................

..............................................

...............................................

<h:commandButton actionlistener="#{UiBean.actionListen}" action="#{UiBean.action}" value="Sample Button" />

..............................................

...............................................

..............................................

...............................................

</h:form>

................................................

...............................................

</f:view>

ManagedBean:

============

public class UiManagedBean{

private String property;

.............................................

.............................................

.............................................

.............................................

.............................................

.............................................

.............................................

public void actionListen(ActionEvent ae){

/*

calling model layer or requesting for a specific service from model

and updating model accordingly

*/

//first get the question ID,questionType ,Question and Releated Answers

String surveyQuestionID = (String) event.getComponent().getAttributes().get("surveyQuestionID");

System.err.println("the question id is :"+surveyQuestionID);

String surveyQuestionDesc = (String) event.getComponent().getAttributes().get("surveyQuestionDesc");

String questionType = (String) event.getComponent().getAttributes().get("questionType");

ArrayList surveyQuestionList = (ArrayList) event.getComponent().getAttributes().get("surveyQuestionList");

Iterator it = surveyQuestionList.iterator();

while(it.hasNext()){

SelectItem selectItem = (SelectItem)it.next();

this.setAnswer(selectItem.getLabel());

this.setAnswer("\n");

}

this.setQuestion(surveyQuestionDesc);

this.setSelectedType(questionType);

}

public String action(){

....................................

.....................................

....................................

return "questionTemplate";

}

.............................................

.............................................

.............................................

.............................................

}

faces-config.xml:

==============

<navigation-rule>

<managed-bean-name>UiBean</managed-bean-name>

<managed-bean-class>UiManagedBean</managed-bean-class>

<managed-bean-scope>request</managed-bean-scope>

<navigation-case>

<from-outcome>questionTemplate</from-outcome>

<to-view-id>/content/questionSelection.jsp</to-view-id>

</navigation-case>

</navigation-rule>

Hope that might Help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-12 3:24:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thanks Baluc, its worked, I really thanks to u for helping lot.ThanksSriaknth
srikanthga at 2007-7-12 3:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thanks RahulSharna , for solution I got it by two ways.Thanks for your help. good solution.
srikanthga at 2007-7-12 3:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
If you don't need the action method at all, you can also put the navigation case in the action.<h:commandButton actionListener="#{myBean.doEditSurvey}" action="questionTemplate" />
BalusCa at 2007-7-12 3:24:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...