1

New developer here - I have a good grasp of basic Java, but I'm less (although somewhat) familiar with Javascript/jQuery and JSF 2.0.

I've taken a look at this question/response: How do the different technologies used for programming webapplications in Java work together? but I'm looking for more of an integrated look at how these the technologies in particular interact.

I'm currently working on a UI rewrite of an existing app. Most of my work is with JSF/Primefaces in .xhtml files, although there is some work with backing beans and Javascript functions. In particular, it would be great if someone could give an example or two showing how and why Javascript is accessed from JSF (and why it is used over Java).

Community
  • 1
  • 1
Jeff Levine
  • 2,083
  • 9
  • 30
  • 38

2 Answers2

2

JSF pages (.xhtml) are uses mostly JSF tags that can be bound via various attributes to you JAVA code (managed beans), eventually all those JSF tags are being translated to simple HTML tags (just do a view source and inspect the final result.

Now to the js/jQuery , after the page is loaded (page on load / jQuery ready) you can manipulate the html page (UI) with the js code that can be executed on page load / ready etc... or as a result of clicking on some element in your page.

Among others you can trigger JSF with js / jQuery by doing stuff like $('#someId').click(); where someId is an id of some JSF element

<h:commandButton id="someId"
            action="#{myBean.myAction}">
            <f:ajax execute="@form" render="@form"></f:ajax>
        </h:commandButton>

Remember eventually all JSF elements / PrimeFaces elements are translated into simple HTML elements (in Primefaces a relevant js code is being included into your page for their UI elements to work)

That's it...


JSF does have hooks for calling js code, its mostly done along with calling ajax, for example you can display a loading div before ajax begin and hide it after ajax end (google about jsf ajax begin complete success)

Calling JSF actions from js - for example if you have some jquery plugin which contains some data and that you want to submit to you managed bean - in this case you can place all that plugin data (some JSON) into hidden text input and than fire a jsf ajax call that execute that json to your server),

Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Awesome, thanks. So does JSF call JavaScript or vice versa? That is, does JavaScript call/manipulate the HTML produced by JSF (rather than JSF calling JavaScript)? Or would I put in my JSF an action, etc that references JavaScript? – Jeff Levine Oct 23 '14 at 23:48
  • 1
    @JeffLevine , updated my answer a bit – Daniel Oct 24 '14 at 10:25
1

Java is used in Back End . For Example In a typical web application you have login page which is simple html or jsp. How would you know that username and password is correct,so you write java code and put some logic which does that. JavaScript can be used for many reasons it's an synchronous language means data can be manipulated without refreshing the url. Or it can be used to validate such as special characters not allowed such things.

Lokesh
  • 313
  • 2
  • 12