I have a basic Java Tag (called PluginTag), which extends TagSupport. This tag adds some behaviour to the calling JSP using the JspWriter instance, e.g.
this.pageContext.setAttribute("plugins", someBehaviour);
I would like to extend this tag, so that it injects HTML meta data into HEAD of the html document. So as explained, the tag has a JspWriter, and not much else...
Also, by the time PluginTag is invoked, another tag will have written the HEAD and any META data out. The trick is I cant update this tag to do my work - and in any case would like the PluginTag to handle my META data, if possible.
I seen a few things like apache HtmlElement, but dont think they are applicable from the context of a Tag.
Thanks.
It's impossible to access HTML document which is formed outside the custom tag. The reason is that previously formed HTML could have been flushed to the user-agent already while other has not been formed yet.
Another way to change shipped to the client and rendered HTML document is to use a custom tag which includes some JavaScript that changes the needed HTML-document elements.
Related
I want to convert a page into a real HTML string, with <html>, <body>, etc..., not XML. I only see the asXml() function, which often changes many things in the structure.
Also note that I've performed modifications to the page after fetching it and I want those modifications to be present in the output as well.
How can I do that? Thank you so much.
So let me check if I got it right:
You fetched a page
You performed modifications to the page (EG: modifying nodes in it)
You want a valid HTML page containing the previous modifications as a String
page.asXml() will not help. This will return a valid XML file as a String rather than a valid HTML file.
page.getWebResponse().getContentAsString() will not help either. This will returned the response that the server gave you as it is (without any modification that you have made).
There is no other method that would return a string with a valid HTML String.
However, you could try using page.save(file). That would save the page the modified page to a file as HTML. Sadly, I don't think there is a method that receives an OutputStream so you're most likely to have to save the file to a file system and then get it back.
Probably, you could take a look at the HTMLUnit source and see how that method is implemented. Maybe adding your own save method is not that complex :)
i have a requirement where i need to display some fields on the JSP. These fields are dynamic in nature, meaning, for ex:, if i changed some value in the dropdown, some fields will be hidden and some other fields might come. I dont want to write Javascripts for show/hide of divs, rather want logic to be coded somewhere at server side.
I have an idea of implementing a custom tag library, but i wnat if i could get an out of the box solution.
any new suggestions or solutions are welcomed.
You had better do it in JavaScript. Having said that, you can send AJAX request to get the new form fields based on the input provided. For example, have a <div> to set the HTML coming from the server.
Use struts framework , there are some tags which can hide and show fields based on values
Logic/logicout tags example
If you want to use a web framework, try Struts 2. It provides tags like <s:if test="some ognl expression" ...> to selectively render html content.
Otherwise you could just go with the JSTL core tags, which provide a <c:if text="some Java EL expression" ...> and a <c:choose ...> tag ( Example ).
Remember to reload the page after changing select box values in order to update the UI.
For this some JavaScript might be needed.
DOM (Document Object Model) in Javascript is very powerful and cross browser.
to remove a node on UI
1.removeChild(nodename)
to add a node on UI
2.elementNode.insertBefore(new_node,existing_node)
I used it. it works well.
more information on DOM.
http://www.w3schools.com/dom/default.asp
I'm fetching data from different RSS / ATOM feeds and sometimes the HTML data I receive contains HTML tags but they dont have close tags or some other issues and it screws up the page layout / styling.
Somethings there is class name / id clash. Is there any way to sanitize it?
If anybody can point me to some reliable Javascript / Java implementation.
You can give JTidy a try.
JTidy can be used as a tool for cleaning up malformed and faulty HTML.
Another option is HTML Cleaner
HTML found on Web is usually dirty, ill-formed and unsuitable for further processing. For any serious consumption of such documents, it is necessary to first clean up the mess and bring the order to tags, attributes and ordinary text. For the given HTML document, HtmlCleaner reorders individual elements and produces well-formed XML. By default, it follows similar rules that the most of web browsers use in order to create Document Object Model. However, user may provide custom tag and rule set for tag filtering and balancing.
I have used NekoHTML with great success. It's just a thin layer over the Apache parser that puts it into error-correcting mode, which is a great architecture as every time Apache gets better so does Neko. And there's no huge amount of extra code.
I want to parse a document that is not pure xml. For example
my name is <j> <b> mike</b> </j>
example 2
my name is <mytag1 attribute="val" >mike</mytag1> and yours is <mytag2> john</mytag2>
Means my input is not pure xml. ITs simliar to html but the tags are not html.
How can i parse it in java?
Your examples are valid XML, except for the lack of a document element. If you know this to always be the case, then you could just wrap a set of dummy tags around the whole thing and use a standard parser (SAX, DOM...)
On the other hand if you get something uglier (e.g. tags don't match up, or are spaced out in an overlapping fashion), you'll have to do something custom which will involve a number of rules that you have to decide on that will be unique to your application. (e.g. How do I handle an opening tag that has no close? What do I do if the closing tag is outside the parent?)
There are few parsers that take not well formed html and turn it into well formed xml, here is some comparison with examples, that includes the most popular ones, except maybe HTMLParser. Probably that's what you need.
AJAX is a very powerful tool so I am struggling with it :-).
Is there any way or API(in java) so that I can get the HTML code which is generated by AJAX?
Generally, AJAX make use of inner HTML code and hence this inner HTML code is missing when I look into the page source of a page.
e.g click here
Just see the section OTHER NEWS. The content is populated by AJAX. When I look into the page source the code is not there.
I need this HTML code through a java program. How can I get it?
To have a Java application use the content received via AJAX, you need to first find the URLs from where the content is getting called from. In case this it would be http://itm2083.com/get_wwo_content.php?featureGroupId=8355&featureDisplayLimit=1&sponsorName=vortalx&wwoDivCounter=5&domainUrlForWWo=http://item2083.com/&featureImgDisplay=FLAG_TRUE&featureGroupImageWidthLimit=200&featureGroupDefaultImageUrl1=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl2=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl3=http://wwo.itmftp.com/75x75.gif
The featureGroupId= parameter has 5 IDs: 8355, 8359, 8367, 8369, 8429. Use these to pull the content from the Other News box.
The featureDisplayLimit= parameter determines how much content is pulled from the server.
If you want the nice HTML as well, the Java app will have to recreate it, as the HTML rendered on the site is created by JavaScript code.