A robust starter web application to ease Java webapp development.

Home | Tutorials | Demos | Issues

call to JSF action from default.jsp

View: New views
6 Messages — Rating Filter:   Alert me  

call to JSF action from default.jsp

by pedro_burglin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi All,

Does anyone know how to call a JSF action from AppFuse's non-JSF default.jsp?

I implemented a shopping cart component UI in default.jsp so it would be presented in most screens of the application. In this UI component I added a link (and later replaced with a form and a button with similar results) to the Checkout screen. Everything was working fine until I had to implement logic to determine if the Checkout link in this UI component would direct the user to the Checkout screen or to another screen based on logic implemented in an Action class.

The problem is that I can't find how to make the Checkout button (or link) in default.jsp to use the JSF action and the navigation rules defined in faces-config.xml. It always send the user to the Checkout screen without calling any logic in the Checkout managed bean.

I have another screen with a button to the Checkout screen. This screen uses JSF to contact the managed bean and navigation rules and it works fine. My problem is just with default.jsp.

Digging for entries about similar issue I found this link:
http://www.nabble.com/Editing-default.jsp-td9267623s2369.html#a9267623
I believe the discussed solution does not work (yet?) and I was really looking for a simpler solution, like how to use html/javascript in default.jsp to use JSF's managed bean and navigation rules, if possible...

Any help really appreciated,
Pedro Burglin

Re: call to JSF action from default.jsp

by mraible :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here's what we use in editProfile.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">

<head>
    <meta name="menu" content="UserMenu"/>
</head>

<f:view>

<h:form id="userProfile">
    <h:commandLink action="#{userForm.edit}" id="edit"/>
</h:form>

<script type="text/javascript">
    // Might be able to remove proprietary call if I can get
@PostConstruct to work
    oamSubmitForm('userProfile','userProfile:edit');
</script>

</f:view>
</html>

On Mon, Jul 7, 2008 at 2:25 PM, pedro_burglin <pedro_burglin@...> wrote:

>
> Hi All,
>
> Does anyone know how to call a JSF action from AppFuse's non-JSF
> default.jsp?
>
> I implemented a shopping cart component UI in default.jsp so it would be
> presented in most screens of the application. In this UI component I added a
> link (and later replaced with a form and a button with similar results) to
> the Checkout screen. Everything was working fine until I had to implement
> logic to determine if the Checkout link in this UI component would direct
> the user to the Checkout screen or to another screen based on logic
> implemented in an Action class.
>
> The problem is that I can't find how to make the Checkout button (or link)
> in default.jsp to use the JSF action and the navigation rules defined in
> faces-config.xml. It always send the user to the Checkout screen without
> calling any logic in the Checkout managed bean.
>
> I have another screen with a button to the Checkout screen. This screen uses
> JSF to contact the managed bean and navigation rules and it works fine. My
> problem is just with default.jsp.
>
> Digging for entries about similar issue I found this link:
> http://www.nabble.com/Editing-default.jsp-td9267623s2369.html#a9267623
> I believe the discussed solution does not work (yet?) and I was really
> looking for a simpler solution, like how to use html/javascript in
> default.jsp to use JSF's managed bean and navigation rules, if possible...
>
> Any help really appreciated,
> Pedro Burglin
> --
> View this message in context: http://www.nabble.com/call-to-JSF-action-from-default.jsp-tp18325804s2369p18325804.html
> Sent from the AppFuse - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: call to JSF action from default.jsp

by pedro_burglin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Matt,

Thanks for the quick reply. I tried your suggestion and I still cannot make it work properly.
Perhaps if I provide more details you or someone else may be able to help me on that. In default.jsp I added something like this:

<form id="shoppingCart" name="shoppingCart" method="post" action="#" enctype="application/x-www-form-urlencoded">

<input id="shoppingCart:checkout" name="shoppingCart:checkout" type="submit" value="Checkout" onclick="oamSubmitForm('shoppingCart','shoppingCart.checkout');" />

</form>

Even after I added the oamSubmitForm it is still not calling the checkout method in the shoppingCart bean and is always directing the user to wherever I set the form action to, instead of using the navigation rules defined in faces-config.xml.

Another possible approach to solve this problem would be to setup faces-config.xml to always run a specific Action before presenting the Checkout screen, regardless of where the user is coming from. I am quite new to JSF, do you know if and how such setup could be implemented?

Thanks again,
Pedro Burglin


pedro_burglin wrote:
Hi All,

Does anyone know how to call a JSF action from AppFuse's non-JSF default.jsp?

I implemented a shopping cart component UI in default.jsp so it would be presented in most screens of the application. In this UI component I added a link (and later replaced with a form and a button with similar results) to the Checkout screen. Everything was working fine until I had to implement logic to determine if the Checkout link in this UI component would direct the user to the Checkout screen or to another screen based on logic implemented in an Action class.

The problem is that I can't find how to make the Checkout button (or link) in default.jsp to use the JSF action and the navigation rules defined in faces-config.xml. It always send the user to the Checkout screen without calling any logic in the Checkout managed bean.

I have another screen with a button to the Checkout screen. This screen uses JSF to contact the managed bean and navigation rules and it works fine. My problem is just with default.jsp.

Digging for entries about similar issue I found this link:
http://www.nabble.com/Editing-default.jsp-td9267623s2369.html#a9267623
I believe the discussed solution does not work (yet?) and I was really looking for a simpler solution, like how to use html/javascript in default.jsp to use JSF's managed bean and navigation rules, if possible...

Any help really appreciated,
Pedro Burglin

Re: call to JSF action from default.jsp

by pedro_burglin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Quick update: I tried the JSFPageParser and configuration steps to make sitemesh decorators JSF-aware as described here (http://forums.opensymphony.com/thread.jspa?threadID=36939&messageID=72427#72427) but with no luck. I still got a blank screen with no errors when I tried to use JSF tags in the default.jsp even after following these steps...

I am still looking for a workaround to this issue, any help greatly appreciated.
Pedro Burglin

pedro_burglin wrote:
Hi All,

Does anyone know how to call a JSF action from AppFuse's non-JSF default.jsp?

I implemented a shopping cart component UI in default.jsp so it would be presented in most screens of the application. In this UI component I added a link (and later replaced with a form and a button with similar results) to the Checkout screen. Everything was working fine until I had to implement logic to determine if the Checkout link in this UI component would direct the user to the Checkout screen or to another screen based on logic implemented in an Action class.

The problem is that I can't find how to make the Checkout button (or link) in default.jsp to use the JSF action and the navigation rules defined in faces-config.xml. It always send the user to the Checkout screen without calling any logic in the Checkout managed bean.

I have another screen with a button to the Checkout screen. This screen uses JSF to contact the managed bean and navigation rules and it works fine. My problem is just with default.jsp.

Digging for entries about similar issue I found this link:
http://www.nabble.com/Editing-default.jsp-td9267623s2369.html#a9267623
I believe the discussed solution does not work (yet?) and I was really looking for a simpler solution, like how to use html/javascript in default.jsp to use JSF's managed bean and navigation rules, if possible...

Any help really appreciated,
Pedro Burglin

Re: call to JSF action from default.jsp

by Cens :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Pedro,
I will show you my solution, but first I must make a consideration.
The use of @PostConstruct annotation will work only in a full J2EE
Application server (e.g. JBoss or WebSphere), not in a Servlet
container (like Tomcat or Jetty).
Indeed I did not test this, but this annotation is part of EJB-Session
specification and therefore available only with a full EJB container
(Please note that EJB-Persistence annotations, instead, can be used
without any container, even in a bare Java application).

My solution is this:
- create a servlet that you can invoke by any non-JSF page
- fill it with the following code

        private abstract static class InnerFacesContext extends FacesContext {
                protected static void setFacesContextAsCurrentInstance(
                                FacesContext facesContext) {
                        FacesContext.setCurrentInstance(facesContext);
                }
        }

        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {
                ServletContext servletContext = this.getServletContext();
                FacesContext ctx = getFacesContext(req, resp);
                Application app = ctx.getApplication();
                //get an instance of your backing-bean form
                MyForm form = (MyForm) app.createValueBinding(
                                "#{myForm}").getValue(ctx);
                //Invoke the action!!!
                form.edit();
                NavigationHandler navigationHandler = ctx.getApplication()
                                .getNavigationHandler();
                navigationHandler.handleNavigation(ctx, "#{anyaction}", "edit");
//the second parameter may be anything
                ctx.renderResponse();
                req.getRequestDispatcher("myForm.html").forward(req, resp);
        }

        private FacesContext getFacesContext(ServletRequest request,
                        ServletResponse response) {
                FacesContext facesContext = FacesContext.getCurrentInstance();
                if (facesContext != null)
                        return facesContext;
                FacesContextFactory contextFactory = (FacesContextFactory) FactoryFinder
                                .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
                LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder
                                .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
                Lifecycle lifecycle = lifecycleFactory
                                .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

                facesContext = contextFactory.getFacesContext(this.getServletContext(),
                                request, response, lifecycle);
                InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);
                UIViewRoot view = facesContext.getApplication().getViewHandler()
                                .createView(facesContext, "/myForm.html");
                facesContext.setViewRoot(view);
                return facesContext;
        }


- in faces-config.xml, remove        <redirect/> in the navigation-case

Please let me know if it works for you.
It seems to me that the servlet code is somewhat redundant, but now I
cannot do trials to simplify it.
Still this approach do not work if the page contains a <t:popup>, I
had to remove it from my page.
It has to do with the fact that using a servlet that forwards to a JSF
page removes from the page header the necessary javascript for the
popup.
If someone has a solution please tell me.

Vincenzo Caselli
gtalk: vincenzo.caselli@...



On Tue, Jul 8, 2008 at 5:21 AM, pedro_burglin <pedro_burglin@...> wrote:

>
> Quick update: I tried the JSFPageParser and configuration steps to make
> sitemesh decorators JSF-aware as described here
> (http://forums.opensymphony.com/thread.jspa?threadID=36939&messageID=72427#72427)
> but with no luck. I still got a blank screen with no errors when I tried to
> use JSF tags in the default.jsp even after following these steps...
>
> I am still looking for a workaround to this issue, any help greatly
> appreciated.
> Pedro Burglin
>
>
> pedro_burglin wrote:
>>
>> Hi All,
>>
>> Does anyone know how to call a JSF action from AppFuse's non-JSF
>> default.jsp?
>>
>> I implemented a shopping cart component UI in default.jsp so it would be
>> presented in most screens of the application. In this UI component I added
>> a link (and later replaced with a form and a button with similar results)
>> to the Checkout screen. Everything was working fine until I had to
>> implement logic to determine if the Checkout link in this UI component
>> would direct the user to the Checkout screen or to another screen based on
>> logic implemented in an Action class.
>>
>> The problem is that I can't find how to make the Checkout button (or link)
>> in default.jsp to use the JSF action and the navigation rules defined in
>> faces-config.xml. It always send the user to the Checkout screen without
>> calling any logic in the Checkout managed bean.
>>
>> I have another screen with a button to the Checkout screen. This screen
>> uses JSF to contact the managed bean and navigation rules and it works
>> fine. My problem is just with default.jsp.
>>
>> Digging for entries about similar issue I found this link:
>> http://www.nabble.com/Editing-default.jsp-td9267623s2369.html#a9267623
>> I believe the discussed solution does not work (yet?) and I was really
>> looking for a simpler solution, like how to use html/javascript in
>> default.jsp to use JSF's managed bean and navigation rules, if possible...
>>
>> Any help really appreciated,
>> Pedro Burglin
>>
>
> --
> View this message in context: http://www.nabble.com/call-to-JSF-action-from-default.jsp-tp18325804s2369p18331105.html
> Sent from the AppFuse - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: call to JSF action from default.jsp

by pedro_burglin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Okay, I finally made it work. I will explain how I did it here if other people find the same problem I was facing.

First of all thanks to Matt and Cens for your replies.

I cannot use @PostConstruct since I need the web app to be compatible with servlet engines like Tomcat and Jetty.

Cens, your solution below looks very interesting, I did not try it yet but by using it you can bridge non-JSF forms with JSF actions and also force it to use JSF navigation rules! Thanks a lot for sharing it!

However, since I had little time (as usual) to fix the issue yesterday I tried something completely different (and simpler, in theory). I moved the form from default.jsp to a file called shoppingCart.xhtml. I then added code to include this new file in the pages where I wanted it to show up.

I had to tweak some few CSS files for it to be placed in the right position but finally I got it to call JSF action beans and navigate correctly!

This is the code I had to add to the pages where I wanted it to be presented, just after the head tag:

<div id="sub">
        <ui:include src="/common/shoppingCart.xhtml">
            <ui:param name="tableName" value="shoppingCartUI"/>
        </ui:include>
</div>

For it to look right I also had to change default.jsp and the JSF pages:
1. removed <div id="main"> from default.jsp
2. added <div id="main"> into every screen, just after the head tag (and also after the <div id="sub"> block above, if present). Sure we have to close this new div in the proper location, just before the </f:view>.

I know it was some manual work but it finally works :D

Thanks all,
Pedro Burglin


Hi Pedro,
I will show you my solution, but first I must make a consideration.
The use of @PostConstruct annotation will work only in a full J2EE
Application server (e.g. JBoss or WebSphere), not in a Servlet
container (like Tomcat or Jetty).
Indeed I did not test this, but this annotation is part of EJB-Session
specification and therefore available only with a full EJB container
(Please note that EJB-Persistence annotations, instead, can be used
without any container, even in a bare Java application).

My solution is this:
- create a servlet that you can invoke by any non-JSF page
- fill it with the following code

...