How to bind Java object to Javascript control - java

I am using the Sencha framework and am creating controls using Javascript. I want to fetch some data from the database using Java, but am uncertain on how to bind that Java object to the Sencha-created controls. What can I try?

'Sencha created controls'?
Check Sencha API for the controls - checkbox, gridpanel etc..
You may need to use a store config in most cases.

Just use Sencha GXT: "Sencha GXT is the fastest, most powerful way to create rich web-based applications using Java."

Suppose you want to create a combobox in sencha, then do it as:
var javaData = = [
[1, 'item1']
,[2, 'item2']
];
var combo = new Ext.form.ComboBox({
store: new Ext.data.SimpleStore({
id:0
,fields:
[
'myId', //numeric value is the key
'myText' //the text value is the value
]
,data: javaData
})
,valueField:'myId'
,displayField:'myText'
,mode:'local'
});
replace javaData var value with your data from server.
Ensure that the data is in json format to run this example.
For more: http://www.sencha.com/learn/combobox-faq/

What your ExtJS store does is an Ajax call to a certain page on your java application. That page will return data formatted in JSON so that your ExtJS store can parse that data.
Basically it doesn't mather what your back end technology is. You just need to make sure your page returns json in the following format:
{
data: [], //Array of json data
success: true, //true when your call was handled succesfull
total: 10 //The total items of a certain object
}
So in your case your JSP page will display the result in the JSON format as explained above, your ExtJS store will be configured to call that certain JSP page.

Related

Webix load combo get all values into array

I'm loading some data into a hidden Webix combo box via URL with two parameters. After loading, I want to store all values from the combo box into an array. But this doesn't work. If it is possible to load data from URL to an array without loading it first into a combo, how does this work? I use Spring ResourceMapping annotation in the controller.
You can use a webix DataCollection which work as any webix data component but without any graphical representation :
var store = new webix.DataCollection({ url:"data.php" });
Another solution is to manually do ajax request :
webix.ajax().get('data.php', function(txt, data) { console.log(data.json()); });

Javascript changes dissapear when going back from servlet to JSP

Good evening,
I have a form on a JSP page that's connected to a servlet, that form has some dynamic parts using JavaScript like adding a row to a table or adding a text field based on the selected option on a select element, Actually my problem is that I have some validations on the servlet-side, so when I go to servlet to check the (National ID) for example if there's any problem or any violations to my validation I force to get back to the form using :
if (dbm.MatchIdNumber(Candidate.getRegNumber(), Candidate.getNationalID()) == false) {
out.println("<script>\n"
+ " alert('Your National Id does not match your Registration Number');\n"
+ "</script>");
out.println("<script>\n"
+ " window.history.go(-1);\n"
+ "</script>");
}
What happens is when I get back to the form I lose all the JavaScript changes, Which's very important.
I've been reading for a while that using ajax might be the optimal solution for me, but here is my questions:
Is there a way to call a java method from JavaScript or JQuery before getting to servlet without using ajax !?!
Is there a way to get back from the servlet to the jsp page with the ability to keep all the JavaScript Chages !?
If not !!, How to use ajax in my case ?!
Thank you so much
No. JavaScript runs on the user's browser and your Java code runs on your webserver, so basically the only way to communicate between the two is via HTTP requests.
If you don't want to use AJAX, you could provide all of the relevant info when you submit the form to the server for validation. You could pass all the info you need to re-generate the form as it was, like which new fields are there and such
First, you'll need to add a new webservice to your Java webapp which performs validation. To achieve this, you could either add additional logic to your servlet (so that it looks for a request parameter like "doValidation=1" and performs validation if it's there) or write a different servlet that handles validation itself. You'll need to decide the format it should expect the form data in and how it should return the validation information.
On your frontend page, you'll need to modify the behavior of the form so that, when you need to do validation, it performs a request to this webservice and passes along the form data. I would probably do this with jQuery and do something like jQuery.ajax(...) and pass the contents of the form as a JSON object.
When your validation servlet returns data from the ajax call, you'll need to update the form based on the data it provides. If I was doing it, I would probably just have the servlet return a JSON object like {errorMessage:"..."} and I would use jQuery to add an element to the form containing the text of the validation error when it occurs. If the servlet returns an empty string or JSON object or something, I would consider it a validation success.

Use java (android app) to pull from a wordpress category post (not using wp templates)

My situation - I want to write a feature for an android app using java to pull the the_title() and the_content() fields of the latest post in a specific category of a wordpress site. I don't use wordpress themes - I just use php inserts in my site's html to place various wordpress items in various places on the page.
I have not tried using xml or rss yet, but it seems to me to put extra steps in the process to do something as simple and short as what I want to do.
Using java, can't I call to a specific html file that has php inserts that pull the items I want from the wordpress database - the html pulls all the wordpress data items and the java simply pulls the strings from the html and show in my app?
the_title() and the_content() are PHP functions, so you can't call them from Java per se. You could write a web service that returns these values as JSON, which you can then use in your Android app.
// define hook for ajax functions
function core_add_ajax_hook() {
// don't run on admin
if ( !is_admin() ){
do_action( 'wp_ajax_' . $_REQUEST['action'] );
}
}
add_action( 'init', 'core_add_ajax_hook' );
// function to return latest title and content as JSON
function latest_post() {
// array for values
$json = array();
// get values for JSON array
if (have_posts()) : the_post();
// put values into JSON array
$json['the_title'] = get_the_title();
$json['the_content'] = get_the_content();
endif;
// encode to JSON and return
echo htmlentities(json_encode($json), ENT_NOQUOTES, 'UTF-8');
}
// hook function on request for action=latest_post
add_action('wp_ajax_latest_post', 'latest_post');
You can then get this info by making a request to:
http://yoursite.com/wp-load.php?action=latest_post

how to change the value of textfield + jQuery + Struts2

I have two textfield A , B : i want to do something like when i enter something in textfield A,this value will be use it in some action and result will be displayed in textfield B without clicking the submit button using ajax.
how can i do it please ? note that i am a using struts2
Most of the information has already been provided by #alexanderb and i believe that Jquery is good way to go, now lets come to the second half of your question about using AJAX in your code. there are few ways you can send results from your action class.
Return JSON from your action class and use above code.
Use stream result type in your S2 code and place the data in the textfield.
Still i believe JSON with Jquery is good way to go which not only provides you the feasibility to easily extend functionality in future but also provide a clean way.Struts2 provides a plugin which can convert the data being send from your action class to JSON and all you will be left to parse the JSON data in your UI to fill the text-box.For details how to work with JSON in s2 refer to JSON plugin for detail
S2-json-plugin
With JSON plugin your flow will be
Call your Action class on specific event in text box.
Configure your action to return JSON data using S2-JSon plugin.
Action will return JSON to the Jquery code.
Parse the JSON data and fill the text box with the value
It should be easy. Suppose you have action that takes value as string and return some string back, availble on '/app/service' url.
You can create such code for that:
$(function() {
$('#text_1').on('keyup', function() {
var value = $(this).val();
$.post('/app/service', JSON.stringify(value), function (r) {
$('text_1').text(r);
});
});
});

Creating a free form BIRT report using Scripted Datasource

I am trying to create a free form BIRT report. The report is not consisted to rows which have the same columnNames in each row.
Instead, it is a free form report, which will be of the following form.
"Name: {FirstName} {LastName} Addess : {Address}
Phone# {Phone#}
....
....
"
I am using a scripted datasource, which essentially returns the Map containing the name value pairs of {FirstName, LastName, Address, Phone, and other fields}..
But I am not sure how to set the variables and how do I get the FirstName, LastName etc.
Should I try to use dynamic text.
I don't know of any way in which BIRT can handle non row related data.
Here's my open script of the dataset.
open:
util = new Packages.test.ReportsUtil();
reportsVO = util.getReportVO("ABC");
in fetch:
if(currentrow < totalrows) {
dataSetRow["FirstName"] = reportsVO.getPropValue("identity.FirstName");
dataSetRow["LastName"] = reportsVO.getPropValue("identity.LastName");
currentrow++;
} else {
return (false);
}
But I am not sure of how do I get access to the FirstName and LastName in the main layout page.
Thank you
The goal of a scripted data source is to allow you to leverage the logic inherent in your data model and benefit from any business rules that manipulate that data. In the end it still wants the data to be formed into a rather traditional row-based set.
You mention dynamic text and I think this would be a great use for the Java-based event handlers. You can use the logic in the Java object you had bound to the scripted data source to instead tie to an event in the life cycle of the report and get your non-relational data that way.
You can even call your Java object directly from a JavaScript event handler (much easier to plug into via the IDE) using the JS "Packages" construct.
There are many examples to help you get this done at the BIRT Exchange.
I did something similar (BIRT 3.7) but I used row["colName"] instead of dataSetRow["colName"] and that seems to work. I have my data in a list, and then each list item is a grid. I set the data binding in the list to the data set. The grid is able to see the value as row["colName"].

Categories