How to prevent sitemesh from parsing html in textarea - java

I use sitemesh for page decoration in my web app.
I have a form where the contents of a textarea field is a complete html page.
The problem is that when Sitemesh parses this page it extracts the title, head and body from my textarea field and decorates the page with it.
<textarea name="page_content">
<!-- tags below should not be parsed by Sitemesh -->
<html>
<head>...</head>
<body>...</body>
</html>
</textarea>
It seems that the Sitemesh page parser does care that it has already seen these tags in the page.
Do you have any ideas on how to prevent Sitemesh from parsing the content of the textarea?

I solved it myself. The solution is self evident when you examine the source code for HTMLPageParser
The trick is to write a custom PageParser that adds a new state with its own set of rules i.e no rules at all:
public class CustomPageParser extends HTMLPageParser {
#Override
protected void addUserDefinedRules(State html, PageBuilder page) {
super.addUserDefinedRules(html, page);
// Ensure that while in <textarea> tag, none of the other rules kick in.
State textarea = new State();
html.addRule(new StateTransitionRule("textarea", textarea));
}
}

You can't put <tags> inside a textarea. It is completely invalid. Textarea elements are not ‘CDATA elements’ like <script> and <style>, any < you put inside them is real markup and not a string literal.
In practice browsers will usually let you get away with it (until you try to include another textarea inside, of course), but what you should be writing is:
<textarea name="page_content">
<html>
...
</html>
</textarea>

Related

Reading html from a html file and displaying it in a view through Java MVC

I understand displaying html files through MVC is not straight forward as jsps are used as the main views.
I've got as far as reading in the html file in the controller and adding the string to the model and view.
(Controller code)
String content = getStringOfFile(myHtmlFile.html);
ModelAndView mv = new ModelAndView("jspPage");
mv.addObject("content", content);
In the jsp I've used a tag to display the content of the string attached to the model and view object.
(jsp page)
<c:out value="${content}"/>
(current undesired output)
<html><body><p>Another test page 2</p></body></html>
the content is displayed, however it includes all of the tags that I wouldn't expect a jsp to render. Is there another way I can dynamically add the string to the jsp where the tags will be ignored when displaying in the browser?
escapeXml attribute did the trick:
<c:out value="${content}" escapeXml="false"/>

Extracting and processing textarea value form without changing the page

I have a JSP file in which there are two textareas and a submit button all in a form ;
<form action="" method="post">
<textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
<input type="submit" name="Send Command"/> <br/>
<textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>
this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.
I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.
what I can't find is how can I do all the things that I said above without going to a new page while using form action.
I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:
<%
DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
%>
<p>Login Successful</p>
<% } else { %>
<p>Login Failed</p>
<% } %>
also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.
p.s: I'm using tomcat and Intellij for developing this web application
and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value
Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.
If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.
You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.
You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.
Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.

JSP page with dynamic html

I've been trying to write a website in which all navigation is handled by hiding and showing divs. It is my understanding that this method is called Single Page Interface. This has worked for simple designs in the past but my current task is starting to become very troublesome using this method. How would I go about replicating the same behavior but instead of hiding and showing divs I can just have a main container div that is then populated with the desired html from the server?
Example:
<script>
$("#button").onclick(function() {
$("#a").show();
$("#b").hide();
});
</script>
<html>
<body>
<div id="a" style="display:none;">A: SOME HTML</div>
<div id="b" style="display:block;">B: SOME HTML</div>
<button id="button">Change to A</button>
</body>
</html>
(note this is a very rough example of white I'm trying to do)
But I would like the contents of a container div to change from "B" to "A" via some jsp
Could anybody point in the correct direction?
Further Explanation:
Maybe I can clarify a little better. So when the user loads the page they are presented with a section that has a table of all the existing files in a database. The user can select a file from the DB list to rename or copy. If the user wishes to rename a file, for example, they would be presented with a new display (all within the same "Tab") which will have a set of fields populated for the file that they have selected and a set of empty fields in which they can specify the new file name. Currently this changing of displays is handled by showing and hiding divs, but I would like to retrieve the html that I want to display from the server and present it. Basically mimicking the hiding and showing of divs.
As it's not completely clear to me what you're trying to do I'll give you some options:
Replace the content of a element on your page see
Since you're using a JSP, you can use server side logic to display certain fragments
You're using a JSP, use that to render some server side content
Ad 1:
(assuming jQuery) $('body').load('serverSide.html'); see http://api.jquery.com/load/
Ad 2:
<% if ("a".equals(request.getParameter("aOrB"))) { %>
<jsp:include page="/a.jspf">
<% } else { %>
<jsp:include page="/b.jspf">
<% } %}
Ad 3:
<%= request.getAttribute('content') %>
Hope that helps

How To Stop Tomcat/Java Wrapping My Output

I've got a JSP page that I want to return a fragment of HTML. The trouble is that whenever I request the JSP, something is attempting to make the HTML more valid by wrapping <html> tags around it. I don't want it to do this though as it will be used in a variety of other places.
For an example, the following JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script src="${applicationConfig.javascriptUrl}update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p>
Will result in the following HTML:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><script src="http://fisher.mycompany.com:8080/my-app/includes/js/update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p></html>
I really don't want those <html> & <head> tags there and would like to get rid of them but have no idea where this is happening to turn it off. Does anyone have any clues?
* Edit *
To give a little more information on what I am trying to achieve. This JSP will check a variety of things and form a piece of HTML. This HTML can then be included into other applications via a web service call.
Servlets can return any content type including javascript and images, not just HTML. Tomcat should not wrap jsps in extraneous tags. I put the snippet you suggested in a jsp, minus the taglib which I don't have set up, and got back exactly the HTML that I put in.
Can you tell us more about your environment? Are you using tomcat? Are you using some kind of framework?
Servlets are HTML factories. They expect to send a valid HTML page down to a browser to be rendered. You can't "get rid of it" without breaking the whole model.
Your original concept of sending a snippet that's "used in a variety of other places" is flawed. You sound like to want to set some data that might be used in other places - that's valid - but I don't see how wrapping it in markup matters.
Only the JSP should be using the marked up data. JSPs are all about display. I'd rethink what you're doing and attack how you want to share the data, not the markup.
One approach it might work,
Create HTML files as you required valid HTML,
and use servlet to returns response, servlet should read HMTL File and return
its contents as String, like XML respones from servlet
hopes thats helps

Which is the best Wicket component for rendering arbitrary HTML?

I am implementing a simple markdown wiki using Apache Wicket. The wiki would typically render any arbitrary HTML based on what the user has entered.
I am a bit confused about which Wicket component would be best suited to render such arbitrary HTML.
I tried the Label component but it does not render lists properly, neither does the MultilineLabel (which puts breaks instead of the regular list HTML).
Thanks for any help.
UPDATE: The Label component works perfectly. It was my mistake that I was not able to get it to work earlier. It was a combination of some bad stylesheets and late night coding. Thanks for the helpfull answers. As suggested, I am also going to check out some WYSIWYG editors, which actually might work out better than markdown. Visural Wicket seems especially promising.
If what you want to render is not big, or is already represented as a String, Label will work well, just call label.setEscapeModelStrings(false); to ensure it prints the string as is.
But, if your HTML content is generated dynamically, or read from an InputStream/Reader, and you don't want to keep it in memory, you could use WebComponent directly, and override the method onComponentTagBody(). This way, you write directly to the response, instead of filling a in-memory buffer, transform it to a String, and then write to the response (which happens if you use Label).
Sample code, for both cases:
HomePage.java
public class HomePage extends WebPage {
public HomePage() {
add(new Label("label", "<ul><li>test</li><li>test</li><li>test</li><li>test</li><li>test</li></ul>")
.setEscapeModelStrings(false));
add(new WebComponent("html") {
#Override
protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
Response response = getRequestCycle().getResponse();
response.write("<ul>");
for (int i = 0; i < 5; i++)
response.write("<li>test</li>");
response.write("</ul>");
}
});
}
}
HomePage.html
<html xmlns:wicket="http://wicket.apache.org">
<body>
<h2>Label</h2>
<div wicket:id="label"></div>
<h2>WebComponent</h2>
<div wicket:id="html"></div>
</body>
</html>
It is Label, call Component.setEscapeModelStrings(false) though to render the raw html your model returns.

Categories