Skip to main content

Question on jMaki Widget server side (bean) integration- what are the optio

8 replies [Last post]
rasane
Offline
Joined: 2007-08-15
Points: 0

Question: jMaki Widget server side (bean) integration- what are the options?

There are two parts viz. a) Server to client b)client to server.

a)
I understand using beans integration as value="#{obj.property}" or service="/myservice/getValues" would load the value on a jMaki widget. the first ensures that values are substituted at load time of jsp and the second will make sure that once element is constructed on client browser, a ajax request will be made.

b)The question is about getting values back from the client. What are the options?

leaving jmaki.doAjax to fire a handcoded request to send updates and requests..
what about out of the box - using form ? Recently ntruchess's reply on thread http://forums.java.net/jive/thread.jspa?threadID=32334&tstart=0 pointing to note
https://ajax.dev.java.net/jmakieventlistenertag.html shows a way of using DynaFaces along with jMaki widgets.. Question is what else exists that is similar to this? That link and sample sounds good, but there is little or no other background explaining what is required to get this working.. I've learnt the way that with missing pieces any example will take ages to get right..

In addition Greg's reply on http://forums.java.net/jive/thread.jspa?messageID=237718&#237718 points to https://ajax.dev.java.net/servlets/ProjectDocumentList?folderID=6844&exp.... Can the source code (for the application bean class) be made available?? I am not sure if this example shows the client to server integration at all..

reading jenifferb's blog http://blogs.sun.com/jenniferb/ uses hand coded jmaki.doAjax -> not form submit based like the DynaFaces integration shows..

I will really appreciate someone shedding light on this...

Regards,
rasane

ps: I am learning, so some statements above are my understanding, not necessary facts. If you just hurray or harrumph on some facts, that would confirm or put me on on the right track :-)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
abdulkalam_selv...
Offline
Joined: 2008-04-02
Points: 0

Hai Friend Thanks For you Help

I have one more problem : Whenever i open that particular jsf file in that movement i need to fill some of jmkai date component automatically . I have those out put date in my back end constructor .

please exaplain me

Selvakumar

gmurray71
Offline
Joined: 2003-07-31
Points: 0

Hi Selvakumar,

Could you clarify your question?

-Greg

sloanb
Offline
Joined: 2003-09-14
Points: 0

In this instance you have the bean setup in application scope. For this to work does the bean have to be in application scope?

Answered my own question.... the answer is no

Message was edited by: sloanb

rasane
Offline
Joined: 2007-08-15
Points: 0

The main question here is answered..
There are some particulars with which I am facing a problem that i will raise separately..

mediii
Offline
Joined: 2007-09-03
Points: 0

hi

how can we get the selected value by the user from a scriptaculous widget autocompletion with the onkeyUp event ?

the value property is not in a JSON format its a String[] array, for example i have

what is the correct script i must add ? :
jmaki.subscribe("/scriptaculous/autocompleter*", function(e) {........ ???

rasane
Offline
Joined: 2007-08-15
Points: 0

hi,
cognizing the fact, that values on the http stream can be only formatted strings, one way to do the same could be:
jmaki.subscribe("/scriptaculous/autocompleter*", function(e) {
//////////// begin prepare formatted string any manner you like
var formatString="{";
for (var i=0; i< e.value.length; i++)
{
formatString = formatString + e.value[i] + ":" ;
}
formatString = formatString + "}";
/////////////// end prepare formatted string any manner you like

jmaki.doAjax({
method : "POST",
url : jmaki.webRoot + "/faces/ApplicationBean-countryService.ajax",
content : { cmd : 'setValue', value : formatString}, // < this is the only change
callback: function(req) {
jmaki.log("Back from Comobox Update: response is : " + req.responseText);
},
onerror : function(m) {
jmaki.log("Error updaing Combobox data");
}
});
});

in the server side service, unpack using split on ":" character
hope that gives you ideas..

regards,
rasane
ps: i have not run the above code, there could be syntactical issues

Message was edited by: rasane

Message was edited by: rasane

gmurray71
Offline
Joined: 2003-07-31
Points: 0

Hi,

The source you are looking for is located here:

https://widgets.dev.java.net/source/browse/*checkout*/widgets/trunk/code...

Basically this is a Managed bean integrated with jMaki.

From a JSF page you can read the data via a service request with the following tag:

You will need to map the bean to the managed bean ApplicationBean in your faces-config.xml file.



ApplicationBean
jmaki.dojo.ApplicationBean
application

Pushing data back to the managed bean is done using jmaki.doAajx requests as you noticed. This is can be done from JavaScript based on the event on the widget like an 'onSelect' event. The sample has the following in it's glue.js

jmaki.subscribe("/dojo/combobox*", function(e) {
jmaki.log("Comobox Select Event : value=" + e.value);
jmaki.doAjax({
method : "POST",
url : jmaki.webRoot + "/faces/ApplicationBean-countryService.ajax",
content : { cmd : 'setValue', value : e.value},
callback: function(req) {
jmaki.log("Back from Comobox Update: response is : " + req.responseText);
},
onerror : function(m) {
jmaki.log("Error updaing Combobox data");
}
});
});

The post parameters contain a "cmd" property which is "setValue" Think of this like a struts action.

In the ApplicationBean the following code gets the call:

public void countryService(FacesContext context, AjaxResult result) {
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
// get the command
String command = request.getParameter("cmd");
result.setResponseType(AjaxResult.JSON);
if ("setValue".equals(command)) {
String value = request.getParameter("value");
setCountry(value);
result.append("{message:'success'}");
} else {
result.append(getCountries());
}
}

It will take the command and in the case of our example get the value sent in the payload and set it to the bean value. Keep in mind you will need to perform any validation checks here.

This should be all you need to do two-way communication with JSF. This functionality is built into jMaki so you don't need to get anything extra or do any extra setup to make things work.

So a little more about DynamicFaces / Listeners : When we started jMaki we worked alongside the Dynamic Faces team to make sure that jMaki would work with the future JSF client server interaction model for Ajax which is being discussed as part of the JSF 2.0 expert group. Much of the ground work for investigating what will be standard for the future JSF 2.0 spec was/is being done as part of the Dynamic Faces project. What is different between what I showed above and what is Dynamic Faces s that much of what you had to do will be done automatically. The controller code above will not be necessary and the posting would happen by default as well.

Norberts blog /posting about the event listener tag is what we plan to enable in the next major release of jMaki which should be inline with the JSF 2.0 spec ( still a work in progress).
If you would like to implement this now we can get you started but since this is an emerging feature it might have subtle changes.

The older way I described above at the begining of this post is more work but will be around for a while. In the future we do hope to move towards an listener model. We would be more than happy to help you you with either option.

Does this make things a little more clear?

-G

rasane
Offline
Joined: 2007-08-15
Points: 0

Hi,

I understand what you are saying and it helps.
To make a knowledgeable choice between listeners or hand-coded doAjax calls to send update, I need to try first. I quite like the concept of using a managed bean like you mention in the first part - I had never got that working until now - my experiments used simple JSP pages or servlets- they are not bad, but have their own place.

I had a problem getting the managed bean to load as you mention.. The steps i followed:

1. I created a new JavaServer Faces web application project in netbeans IDE. dropped at combo box in welcomeJSF.jsp with default data- deployed - it worked.
2. Then I created a new Java class under package jmaki.dojo named ApplicationBean. I copied the contents of the ApplicationBen.java you gave link to in to this file. compiled succesfully..
3. Edited my faces-config.xml with the entry you gave
4. inserted the value tag for combo "#{ApplicationBean.countries}"

(by the way the sample JSF application also has a faces-config.xml that differs in the starting tag. My faces-config.xml looked like this:


xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

The faces-config tag looks quite different and probably this is the source of my problem.. )

When I deploy or call this page, i now get the following error
->browser:
exception:
org.apache.jasper.JasperException: java.lang.ClassCastException: com.sun.el.ValueExpressionImpl cannot be cast to java.lang.String
root cause:
java.lang.ClassCastException: com.sun.el.ValueExpressionImpl cannot be cast to java.lang.String

-> in Glassfish V2
StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.ClassCastException: com.sun.el.ValueExpressionImpl cannot be cast to java.lang.String
at jmaki.runtime.IOUtil.createJavaScriptObject(IOUtil.java:126)
at jmaki.runtime.IOUtil.createJavaScriptObject(IOUtil.java:91)
at jmaki.runtime.WidgetCommonRenderer.writeAddWidget(WidgetCommonRenderer.java:273)
at jmaki.runtime.WidgetCommonRenderer.writeResources(WidgetCommonRenderer.java:153)
at jmaki.runtime.jsp.WidgetTag.doEndTag(WidgetTag.java:127)
at org.apache.jsp.welcomeJSF_jsp._jspx_meth_a_widget_0(welcomeJSF_jsp.java from :221)
at org.apache.jsp.welcomeJSF_jsp._jspService(welcomeJSF_jsp.java from :92)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:470)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:364)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)

The dtd of faces-config.xml may have changed over time, but i tried and could not find a tutorial to add managed bean differently from the steps above.. How do I get over this error?

Can you help please?

Regards,
rasane

Message was edited by: rasane

Message was edited by: rasane

Message was edited by: rasane

Message was edited by: rasane