• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

SPRING_MVC + JSF - h:commandButton action does call Spring bean's method

 
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all
This post has to do with SPRING MVC + JSF
I have a problem. I can not make the action method of <h:commandButton> to be invoked!
I tried every step from this link: link but nothing worked.
They told me that it is not working BECAUSE THE URL TO THE PAGE IS MAPPED BY THE DispatcherServlet. I DON'T USE FOR MAPPING THE FacesServlet. So JSF does't do the job!

I post the files:
- The "web.xml"
- "my_template.xhtml", which calls the component "center_section.xhtml"
- In the "center_section.xhtml" I "ui:insert"
- In "my_page.xml" I "ui:define"
- Spring bean "MySpringBackingBean.java"

Firstly the "MySpringBackingBean.java"
The method is at the end.
I added setters (in a site someone said that with setters method was invoked). To me it did not work.

In "my_page.xml", I have 4 efforts that I tried:

Effort 1
- In this effort I have it without <f:ajax>. I just want simply to call the method.
After that I will make it update the "h:inputText" (that is why the next efforts have the <f:ajax>)
- ONLY IN THIS CASE, because there is no <f:ajax> tag, as it is WITH the <h:form> it gives me a page with this error:
"The origin server did not find a current representation for the target resource or is not willing to disclose that one exists"
- WITHOUT the <h:form> it does not give me the error page, but (of cource) it is does not call the bean method.

Effort 2,3,4
In these cases I have <f:ajax>. Or with <h:form> or without <h:form>, because of <f:ajax> it does NOT give me the error page as in the effort 1.
What ever I did, it was not making the method to be invoked.
- I put everything in <f:ajax> in the "render" attribute the id of the form "fff"
- I put the immediate="true", as in a site they said that it is the solution
In all of the cases it did not do the job. I tried all the possible combinations.

Something strange
Accidentally, I commented h:commandButton, and the other NOT commented, like in the "Effort 4".
In every page reload (not pressing the commandButton) the method was caught in breakpoint! I thought I solved it, but
googling I learned that using the "javax.faces.FACELETS_SKIP_COMMENTS==true" in the "web.xml", the issue was not solved.
Why the commented button's action EL is evaluated and the uncommented not evaluated with page reload?
Googling I found the case in "Effort 3". With this hidden input, it causes the same as the commented commandButton, but my issue remains!

Question
ANYONE Spring expert please who uses Spring+JSF, can help me to make this action method invoked in the Spring bean (or from ManagedBean) from my <h:commandButton>?
I follow all conditions in the previous link, are not followed?
They told me that it is not working BECAUSE THE URL TO THE PAGE IS MAPPED BY THE DispatcherServlet. I DON'T USE FOR MAPPING THE FacesServlet. So JSF does't do the job!
Thanks for your patience!

Following are the relative code snippets
I use in all cases the:
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:cc="http://xmlns.jcp.org/jsf/composite"
xmlns:mc="http://xmlns.jcp.org/jsf/composite/my_components"
xmlns:p="http://primefaces.org/ui"





 
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give you credit for persistance.

BUT.

JSF only works when you use the FacesServlet. It's the master dispatcher for everything JSF. You have to use the FacesServlet to display the JSF web page (View) and you have to use the FacesServlet to handle the data that comes back from the JSF h:form. It simply won't work any other way.

This is NOT to say that JSF is a greedy framework than cannot work with other web frameworks the way that many of the old pre-web GUI systems did. It just means that the JSF parts of the webapp need JSF.

A JSF View can contain non-JSF HTML links and forms and those can dispatch to Spring Web (I've done so), Struts, raw servlets (this is how I get JSF to create Word documents, Excel spreadsheets and PDFs when I'm not using Spring Web). But a JSF h:form must interact with the FacesServlet and all of the controls in the h:form must be JSF controls or they won't work. Conversely, raw HTML forms should contain no JSF controls - they won't get processed properly.

So you cannot send a JSF h:form to a Spring MVC dispatcher - it will receive stuff that to Spring Web is garbage. And you cannot use a Spring Bean as a JSF backing bean, because JSF won't know how to locate it.

Again, the two web frameworks do work well together, but they can't both work at the same time.
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tim.

I give you credit also for your patience to help and advice me on this issue! Others they would leave me in my issue!

I will see all your writing and here and at the other post, and I will see how to structure by project so I will involve the FacesServlet to the JSF views!

I need to make the 2 frameworks to work together, not to force them to do the same thing...

Thanks again for now!
I will leave the posts open, so I will get more info.

Thomas
 
Tim Holloway
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To a degree, though, JSF and Spring Web do do the same thing.

The difference is that JSF is optimized to deal with data-entry forms using Inversion of Control on POJOs, whereas Spring Web is a more general web framework. So in many cases, you can swap JSF for Spring Web and vice versa.

But not all.

I prefer JSF for forms-based processing because it automatically deals with MVC and data entry error validation and reporting. On the other hand, JSF is not a good framework for taking the information I've just collected and returning it as an XML file download. For that I'd use Spring Web. That's not a problem. JSF pages can reference Spring web URLs using the h:outputLink tab, which is designed specifically to navigate to non-JSF URLs. Likewise, a Spring Web application component can route to JSF for form-based data processing, since a JSF URL is just another URL that happens to match the URL pattern for the FacesServlet. Under the hood, Session and Application scope objects are equally accessible to both Spring Web and JSF - and that includes ManagedBean objects in Session or Application scope.

Which is why I can enter data on a JSF form, store the entered data in session, then go from the JSF page to a Spring Web URL which pulls the entered data from the session and outputs an XML file via a JSP or a PDF via a PDF generator component.

There's no trickery, magic, or kludging here. It's all straightforward and entirely blessed usage of JEE. The only constraints are, as I said, that on a JSF page, you need JSF resources and logic, and on a Spring page you need spring resources and logic. And if you separate your business logic out of the GUI Model objects, you can migrate a data-entry page from/to JSF and Spring Web with relatively little effort.
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tim

Thanks a lot for the info.

Yesterday I was reading articles and pdf about Spring and JSF.
They were writing that SpringMVC does not support so much JSF, but Spring WebFlow DOES support.

I will try to do it this way and I will see!

Your info are valuable to me!

Thomas
 
Tim Holloway
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't have said Spring Web "supports" JSF (or the opposite) so much as that they can work together without interfering with each other. But they definitely do work together!
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tim

I am reading the online tutorials about this!

i will let you know the result...

Thomas
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tim

I did it!

I put out the following code from my Spring Controller...



I just let the JSF to display the page with its own FacesServlet...
And I changed accordingly the url of the page

I undertsand that the Spring MVC will be for the other requests for other resources.... Not for the pages.
In my version I put and @ManagedBean and @Component with @Autowired as the online tutorials display, but I need to configure and OPTIMIZE all previous better.
For example the url, the 2 annotations... everything!
I need to try it with the Spring WebFlow that they said works ok with JSF.

SpringMVC for requesting all kind of resources. JSF is displaying its pages!

Thanks a lot for now
Thomas
 
Tim Holloway
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations! Have a cow!
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help Tim to understand the diffrence and the usage in each framework!

Yesterday I created a Filter so I will filter the urls...
You type: localhost:8080/myapp/my-page
and the Filter calls the: localhost:8080/myapp/real/path/to/my_page.xhtml.

With respect Thomas
 
Tim Holloway
Saloon Keeper
Posts: 23411
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find it hazardous to invoke JSF via an ".xhtml" extension. If the servlet mapping fails, then instead of invoking JSF, the raw XHTML View Template will be returned to the client and you won't get proper JSF behaviour.

My preference is to use an extension like "*.jsf", so that a URL of  localhost:8080/myapp/real/path/to/my_page.jsf will be broken down as follows:

1. Client sends URL request to port 8080 on localhost (webapp server)
2. Webapp server listening on port 8080 matches "/myapp" to its list of deployed webapp contexts and routes the request to that webapp.
3. The webapp matches "/real/path/to/my_page.jsf" to the FacesServlet, which then decodes that path to locate the View Template (/real/path/to/my_page.xhtml).
4. The FacesServlet then uses the View Template as the basis for running the JSF lifecycle and returns the JSF response.

You can use a filter to transparently rewrite selected URLs to target JSF, but you can also use HTML redirection pages.
 
Thomas Mylonas
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tim

I was somewhere without internet and that is why I delayed.

Thanks a lot for the analysis of the subject.
I will study all of your notes here.
To me they are valuable.

Thanks a lot a again
Thomas
 
I can't take it! You are too smart for me! Here is the tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic