I have a ArrayList which contain objects of a Java Class.I want to pass that list to javascript and want to populate the data within the object in to jsp.
The ArrayList is present in the jsp.Each class object only contain 2 parameter values.Class name is 'Certificate' and it contain 'certId' and 'certName'
jsp:
<%
ArrayList getAllCertificateNamewithId=new ArrayList();
getAllCertificateNamewithId=(ArrayList)mainMap.get("getAllCertificateNamewithIdMap");%>
<input type="button" width="16" height="16" onclick="fetchAndShow('<%=getAllCertificateNamewithId %>')" />
Javascript
function fetchAndShow(certObj){
//How to fetch the list's each object and cast to java class 'Certificate'
}
List contain
[com.dto.Certificate#9b44a1, com.dto.Certificate#1b6b3ac]
Think of it this way: you use Java + JSP as a means to write a text file that you then send to the user's browser, with JSP being a simple template mechanism.
You could theory implement your own method to serialize the List with objects to a String, and then implement a mechanism on the client side to deserialize into whatever data structure you want. For instance you could write your List as JSON.
But now I am inclined to ask: do you really, really, Really, REALLY need to do this ? Exposing server certificates of any kind to the client is a big no-no.
That's not the correct way. Just because of the contents are in the same page, they can't communicate directly.
Java plays on server side and javascript on server side. A client doesn't execute the java code.
To send a Java value to javascript you have to make a server request, So the server process your request and send the results back to Javascript.
Inshort you need to make a server (AJAX) request.
I don't know whether it would be right or not but it's worth a try. Try to use GSON or JACKSON libraries to convert your java object to JSON string. Then I think that you would be able to use that JSON String data in your Javascript code and get the certID and certName wherever you want.
Related
This question's answer shows how to do what I'm doing currently:
How to pass variable from back-end to front-end in node.js ejs template
I'm passing the variable into the EJS render call, then using JSON.stringify() to convert it into a format that front-end code can use.
However is there a way to pass the data to the front-end code that doesn't expose what the data actually contains?
For a specific example, say I have an API key for something like MapBox that I'm sending into the EJS render as process.env.MAPBOX_KEY
When I use the JSON.stringify() it exposes that key as plain text for anyone who wants to inspect the page.
I would be grateful if there was a way to get something like that into the front end code in a way that no one can see what it is.
I have a list of checkboxes, with this function i get my array of checked items:
var selected = new Array();
$('#bloqCountries input:checked').each(function() { selected.push($(this).val()); });
I have a model class and i need to send by parameter this array making something like this:
[#assign searchResults =model.searchContent(selected) /]
But Javascript is in client side and Freemarker in server side...
Then i don´t know how to get this array in my model to call my modelClass..
My idea is making by the request, transforming the array in string and making a split() method in java to delete the "-" between the words
I don't know how to send this content because i´m not using ajax and I don't think its necessary.
Simply put, you can't do that. FTL template is executed on the server and only once, to produce HTML and to send it to browser. JS is executed only when browser receives and processes said HTML. By that time, FTL process is long dead.
If you want to communicate with the server, ajax is the only way to do that. You might find some inspiration and examples of using ajax w/ Magnolia at here or here.
HTH,
Jan
i have a list of values in javascript and the variable name is array with the values
['1','2','3'].here i want to assign this array values into a list in ftl file called list.
actually am trying to get checkbox value through js and assigning to a list which is the ftl file.
help me to overcome this issue.
FreeMarker runs on the server before the JavaScript runs on the client, so what you want is impossible. Or at least the way you want to do it, it is. You had to post that array back to the server (with a form or with AJAX), etc.
You have to send the list of values via ajax call and refresh a div to show new results.
since freemarker is a templating engine that runs on server to generate the html pages you will not be able to send any objects,lists, etc... to freemarker I could show you an example if you want.
I'm a novice at web programming, and right now, I am trying to write a web interface to python python program.
I'm wanting to fill in a simple form, hit submit, pass data to a python script, have the script pass data back to the same HTML file and display this data on the HTML file.
So, which way do I go about doing this? I have the HTML file done and I have my python program done, but no communication between the two. Should I use post to python using cgi or should I call javascript to create a json object and then pass that in to python and have python pass another json object back to me?
Should I use post to python
Use a form. It is simple and works. The choice between post and get depends on what you are doing with the data.
using cgi
The client submits to the server using HTTP.
How the server communicates with the Python then depends on a number of factors. CGI has the benefits of being simple and easy to set up (but it doesn't scale well).
or should I call javascript to create a json object and then pass that in to python and have python pass another json object back to me?
You could layer something like that on top of the form solution. (Remember to be progressive and unobtrusive). There isn't much benefit in using JSON for the input if you have a flat data structure though. I wouldn't bother with it unless you were dealing with complex data structures on the client.
Getting Json from the Server and displaying it in the grid is relatively straight forward. In the application (http://pssnet.com/~devone/extjs3/loadSelection5.html) I generate a dynamic grid datastore. I need to send this to the Server for further processing.
If there is a way to convert the data store to json, I can strigyfy it and send it a param...as in jQuery.
Otherthan looping through the whole datastore to build json, there seems no methods.
Thank you.
(I can guarantee that the following applies to Ext > 2.3)
Because Ext.data.Store stores an array of Ext.data.Record objects, it has no direct access to the underlying data (Ext.data.Record encapsulates the data), so, indeed, there's no direct way to do it. But Ext.data.Record itself has a public property called "data" (an object with the field:value properties), which you can collect into an array (e.g. using the Ext.data.Store#each method) and then encode with Ext.encode() to "stringify" it.