Normally I create html text dynamically using javascript. See sample
var element1 = document.createElement("input");
element1.type = "text";
element1.name = "txtbox[]";
element1.className = "textfield"
element1.size = "15"
element1.value = "something"
cell2.appendChild(element1);
But my problem is create struts 1.3 text field dynamically using javascript.I try this but not ok especially elements.value instead of Struts 1.3 property attribute.Is it possible to create struts 1.3 text field using javascript not jquery?
Struts tags have to be processed on the server before it is sent back to the client machine browser as HTML. JavaScript can only manipulate HTML tags on the client Side.
Related
I implemented a SOA service using other services(I'm using java language). I use servlet to display an html table with the processed results. Is it also possible to send json with html? or in other words is it possible to send two different things in the response of the doPost method of httpservlet?
If you want your html page to access the json you could embed the data within a script tag:
<script type="application/javascript">
var mydata = {"foo":"bar"};
</script>
I'm trying to send some rendered HTML to the backend, through ajax, using javascript or jquery, but I can't find the way to do it.
I'm trying to export that html to pdf using itext.
Is there any way that I can achieve this, sending the data as a String?.
Many thanks.
(I'm using Java and Spring on the backend)
You can send you HTML as a JSON object. Here a quick example.
var elm = document.getElementById('yourElement');
var value = elm.innerHTML;
var objToSend = JSON.stringify(value);
// now send using ajax
...
I am using Struts 2 and JavaScript. I want to edit a parameter value or add parameter to URL with JavaScript to make dynamic parameter. I can not set value for param why this not have id. Any other form of do?
var urle = document.getElementsByName("vari");
urle.value = 5;
<s:url var="urlex" action="actionDo"><s:param name="vari" value=""/></s:url>
Struts tags are JSP tags that compiled and executed on server, JavaScript is executed on client browser. You can't access the server from javascript code without making ajax request. But you don't need too. Use
var url = '<s:url var="urlex" action="actionDo">' +'?vari=' + val;
You have to set jsonObject when this action class has been called. So according to set parameter it will set parameter dynamic.
I'm trying to get value from Action using Ajax request, but I'm having problem in using struts tag in javascript to show the value.
Javascript code:
dojo.xhrPost({
url: "dashboard"
,content: myParameterscomp
, handle: function(response, ioargs) {
if (ioargs.xhr.status == 200)
{
var data = "<s:property value='#request.consumptionData'/>"
console.log(data);
}
}
,error: function (data) {
handleError(data.description);
}
});
Java code:
Map request = (Map)context.get("request");
request.put("consumptionData", 43);
I'm getting the value of data as <s:property value='#request.consumptionData'/> on console instead of 43. I'm using struts2. My javascript code is in JSP file. Could anyone please tell me how can I show the value?
You seems to be calling /dashboard page via Ajax from your homepage. And expecting it to send you request attribute consumptionData. This won't work as your JSP does not contain required data. You need to put data in JSP and then fetch the same in Ajax. Convert your response to JSON. The simplest way of doing this would be to put following like of code in your Ajax response JSP.
Dashboard.jsp
{"consumptionData": < "<s:property value='#request.consumptionData'/>"}
And in main page when you load this JSP via ajax, you can parse this JSON output and use data in Javascript.
var json = JSON.parse(response);
var data = eval(json.consumptionData);
Code in browser accepts JSON. You could serialize you request as JSON and embed in you JavaScript file. For example:
var data = <%= toJson(request.get("consumptionData")) %>
If the data is simplely Integer values, you can even directly put the value in the code:
var data = <%= request.get("consumptionData") %>
The syntax could be vary (I'm not familiar with struts tag), but the idea is the same.
I'm trying to get some values from a site but these values only appears when I use a Browser, like Mozilla. When I use the Jsoup I can get the HTML from the site but without values, only with the tags.
This is the site I'm trying to parse:
http://www.submarinoviagens.com.br/Passagens/selecionarvoo?Origem=nat&Destino=mia&Data=05/11/2012&Hora=&Origem=mia&Destino=nat&Data=09/11/2012&Hora=&NumADT=1&NumCHD=0&NumINF=0&SomenteDireto=0&Cia=&SelCabin=&utm_source=&utm_medium=&utm_campaign=&CPId=
I'm trying to get the values that appears inside these span tags:
If I access the previous URL from a web browser I can see the following values: '', 'R$ 2634,22' and 'R$ 2634,22', but when I use the following code the values disapears.
URL url = new URL("http://www.submarinoviagens.com.br/Passagens/selecionarvoo?Origem=nat&Destino=mia&Data=05/11/2012&Hora=&Origem=mia&Destino=nat"+
"&Data=09/11/2012&Hora=&NumADT=1&NumCHD=0&NumINF=0&SomenteDireto=0&Cia=&SelCabin=&utm_source=&utm_medium=&utm_campaign=&CPId=");
Document doc = Jsoup.parse(url, 100000);
String title = doc.title();
System.out.println(doc.toString());
If I try to see the source code via Mozilla Firefox the values disapears too.
But If I use the firebug plugin I can see them.
Thank's for the help!
The website uses JavaScript to populate all of the values you are trying to parse. You will have to use a library that can compute the javascript within the page. Not sure if there is one though.
anyone else?
Htmlunit is a headless browser that renders Javascript and should be able to present this page correctly.