How to add other html file in thymeleaf,Is it OK ? - java

As you know,if we want to include other pages in jsp(most are header 、footer), we can write that in our jsp file:
<jsp:include page="/pages/common/header.jsp"></jsp:include>
if I want to  achieve the same function that adding some html file in thymeleaf template , how can I do? Is it OK ?

No, you can’t include jsp files in thymeleaf. But you can include other thymeleaf pages. See how :
Difference between th:insert and th:replace (and th:include)

Related

Pass parameters to jsp by <%# include file

I need to pass params to included jsp
<%# include file="second.jsp" %>
i need to use it several times in one jsp page, so i need to pass id.
I can't use jsp:include because jsp can't see struts action in this way
Can u help me? Thank you
Set them as attributes on the request object (which makes them visible to the ${var} syntax) before invoking your include.
See How to access a request attribute set by a servlet in JSP? for a similar question.

How to map server response retrieved in jsp to an iFrame

I'm using struts2 framework(java/js/html/css combo) for my webapp. I am reading a text file from server and I want to write the response to an iFrame present in the same jsp.
Flow:
(1) On click of a link, I pass the relative URL of the text file to jsp.
(2) When the jsp page loads, the java code in the jsp reads the file from server.
(3) Now this response has to be written to an iFrame present in the same jsp file
Can anyone plz help me in writing such response to an iFrame?
Thanks in advance :)
[code not tested, only a demostration of the concept]
here's some very rough idea as to how to fix your code, they definitly not the best but they should be enough to help you understand the concept.
However I'd still recommend going over the whole concept and maybe come up with a more efficent way to do what you need.
if you insist on using iframe, you need to make use of 2 seperate jsp as W3C says in "Implementing HTML Frames":
Any frame that attempts to assign as its SRC a URL used by any of its ancestors is treated as if it has no SRC URL at all (basically a blank frame).
so you'll need 2 jsp, the first one is basically what you have but the the src of the iframe changed to:
<iframe scrolling="yes" width="80%" height="200" src="second.jsp?content=<%=all%>" name="imgbox" id="imgbox">
and the second one will be something like :
<html><body><%= request.getAttribute("content") %></body></html>
From the code you've shown you forced a "content update" on the iframe by using javascript. The proper/usual way to update an iframe is to provide different input parameter to the second jsp and let it update it for you.
Finally, I'd recommend using JSTL as much as possible instead of scriptlets. It is much cleaner.
What you need to do is set the src attribute of the IFRAME to the jsp url when your link is clicked. Another way to do it is doing something like this:
<iframe src="" name="iframe_a"></iframe>
<p>W3Schools.com</p>
with the correct parameters of course

Loading page content via AJAX in JSF2

I've been looking into jsf technologies lately such as primefaces, primefaces-extensions, omnifaces .
what I am not able to find is way to fetch content via ajax for example load a form, table content and put them into div or something, I've looked at rendered attribute, but that won't work for high scale application.
Any help would be appreciated.
A handy trick for this is using a dynamic ui:include tag like:
<div id="dynamicContent">
<ui:include src="#{contentBean.content1}"/>
</div>
Where content1 is a string with path and .xhtml filename (usually something like /WEB-INF/content/page1.xhtml), and the parent div can be used to update.

Spring - Template page based on many JSP pages

How can I build an template page, based on many JSP pages with Spring?
I can not use tag, because I have to set some data to those pages first. I also do not want to implement whole template page ( header, footer ) in every JSP file because only ${content} var will be changing.
Thanks in advance
Use a jsp fragment within your jsp:
<%#include file="header.jspf" %>

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

Categories