This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes JSF Redirection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF Redirection" Watch "JSF Redirection" New topic
Author

JSF Redirection

Vince Kennedy
Greenhorn

Joined: Aug 26, 2009
Posts: 20
I'm working on this and got everything I needed set up and running. So now I was playing around a bit more to see if I could develop a simple site.

But I'm having issues with redirection.

This is the navigation rule I'm using.



And this is the form in test.jsp


The MyBean class is just an input and output string with getters/setters and a public void action() that sets the output message. I do know the file is named test.jsp and the redirection uses /test.jsf, because that's the URL i have to use to get to the page because of the web.xml. The problem is that it won't change pages no matter what I do.

I've been reading through other pages and everyone seems to handle button clicks and page redirection a little different so I can't tell if something with a code block is wrong, or if I'm missing the whole point on navigation. Any help would be appreciated.
Abhishek Ralhan
Ranch Hand

Joined: Aug 01, 2010
Posts: 40

Hi Vince,

I don't know if the <navigation-case> works without the <from-outcome> tag. I can explain how I perform a navigation, may be this is helpful for you:

Below is one example for the same:

<navigation-rule>
<from-view-id>/view/login.jsp</from-view-id>
<navigation-case>
<from-outcome>user_auth_success</from-outcome>
<to-view-id>/view/contextmanagement.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error_page</from-outcome>
<to-view-id>/view/errorpage.jsp</to-view-id>
</navigation-case>
</navigation-rule>

Please note that your myBean.action should return a string, and if that string matches with the value in <from-outcome> tag. Then only it goes to the page within the <to-view-id> tag.


-Abhishek
I came to this world on a Learner's License
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

I don't see any actual redirection there. Just an input form that dispatches to a new form.

"from-outcome" is not required. If not supplied, the rule is unconditional. Otherwise, the rule only applies when an action method returns the matching from-outcome value.

Some things that can interfere with the dispatching process:

1. Failure of one or more fields in the form to validate (hint: include an "h:messages" element on the view containing the form).

2. Failure to find the action method. This can happen if the action method does not exist, the name is misspelled/miscapitalized, or if it doesn't have the proper signature: "public String xxxxx()". No error will display in this case. Failure is silent.


Customer surveys are for companies who didn't pay proper attention to begin with.
Vince Kennedy
Greenhorn

Joined: Aug 26, 2009
Posts: 20
Tim Holloway wrote:1. Failure of one or more fields in the form to validate (hint: include an "h:messages" element on the view containing the form).

2. Failure to find the action method. This can happen if the action method does not exist, the name is misspelled/miscapitalized, or if it doesn't have the proper signature: "public String xxxxx()". No error will display in this case. Failure is silent.

Thanks Tim, I appreciate all the help in my fumbling through JSF so far.

1. There is the h:messages element on the containing form - but do I need to set options so the errors would display? It seems all of the flags that I can set in it are for error/info styles and not displaying the errors.

2. I was using the BalusC JSF Tutorial (great set up tutorial btw), and he had turned off that warning so you could have void action Methods. I switched it the method to return a string value. Here's the code:


So to make sure I understand the redirection process, the action field of <h:commandButton> calls myBean.action, because that's what I set it to. Then the redirection is on test.jsf, so when the action is done it should automatically be moving on to hello.jsf.

Now one thing I was mildly worried about if I do /test.jsp - the page won't display, it has to be /test.jsf even though the actual file is .jsp. So on the redirect URL should it be /hello.jsp for the file name or /hello.jsf because it needs the jsf extension to be recognized?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960
    
  19

Void action methods only work in JSF2. In JSF1 you need to return a String, even if it's going to be ignored by the navigator (no rule match). And since you do want navigation, void definitely won't work. Then again, in JSF2, you wouldn't have to create a faces-config navigation rule either unless you wanted to.

Don't use the word "redirect" here, though. It means something completely different.

As far as naming goes, "test.jsf" is the URL. "test.jsp" is the View Prototype. When you add the "*.jsf" URL pattern to web.xml and map it to the FacesServlet, that causes URLs ending with ".jsf" to be routed to the FacesServlet, which is the Master Controller in the JSF MVC framework. The FacesServlet disassembles the URL according to standard rules. Those rules include removing the ".jsf" and appending a ".jsp" to produce the name of the View Prototype resource to use. The JSP resource is then processed at the appropriate time to produce the View which is returned to the user's browser.

JSF navigation is to URLs, not to resources. Therefore your rule must specify a URL, giving us:

3. If the destination URL cannot be resolved, no navigation can occur.

Actually, I thought this was supposed to result in a "404" error, but at any rate, it can't succeed.
Vince Kennedy
Greenhorn

Joined: Aug 26, 2009
Posts: 20
Tim Holloway wrote:navigation is to URLs, not to resources. Therefore your rule must specify a URL, giving us:

3. If the destination URL cannot be resolved, no navigation can occur.

Actually, I thought this was supposed to result in a "404" error, but at any rate, it can't succeed.

Well I had been playing with the faces-config.xml so the <to-view-id> value is now '/hello.jsf' - that seems to be correct.

I added the error messaging stuff to the test.jsf and the messages worked (since I had to play around with ID's or it was printing error messages, now it prints none).

test.jsf and hello.jsf are both on the same level of the WebContent folder, and test.jsf works beautifully, so the path should be working.

I was guessing for some reason the actual navigation-rule wasn't getting invoked by the buttonCommand for some reason, hence no 404 or other error. It does nothing after the button click.
Vince Kennedy
Greenhorn

Joined: Aug 26, 2009
Posts: 20
I've been playing around with this all day and reading various examples trying to figure out the problem.

I do notice when this is the setting, I get a warning that action value does not match a navigation outcome.


But my navigation rules are the same:


And if I change from-outcome to from-action with this:

It still won't navigate for me. The action method is in mypackage.MyBean.java:


Vince Kennedy
Greenhorn

Joined: Aug 26, 2009
Posts: 20
I got it to work by changing the navigation-rule to this:


So the view-id needs to reference the actual view names? i.e. the url I type is /test.jsf (and it has the servlet mapping) but for JSF redirection it needs to be the actual file name, i.e. *.jsp?

I thought you (Tim) had said it only referred to the URL, but since there's no servlet mapping in JSF navigation is that why it works with *.jsp, but *.jsf?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF Redirection
 
Similar Threads
navigation rules are not working...
commandLink param problem
I am so tired just getting start in jsf please help me please
JSF <h:commandButton action> not working
jsf newbie, navigation not working