which one is faster?
(for static html page include)
Probably the fastest solution for static html page include would be Server Side Includes:
foo.asp
<%#LANGUAGE="VBSCRIPT"%>
<% Option Explicit %>
<!-- #include file="baz.htm" -->
baz.htm
<html>
<head />
<body>
<h1>Hello World!</h1>
</body>
</html>
Related
I would like to have dynamic value in my html file which gets Convertet to a PDF file. I've got no idea how to do that.
HTML
<!doctype html>
<html lang="en">
<head>
<title>SpringHow html to pdf</title>
</head>
<body>
<div>
<h2> --> DYNAMIC VALUE HERE <-- </h2>
</div>
</body>
</html>
JAVA
HtmlConverter.convertToPdf(new File("template.html"),new File("report.pdf"));
is it possible extend a shared view with thymeleaf?
I saw that is possible use framents but is not what I want.
Instead I want something similar to .NET MVC, with something like #RenderBody() and another view that extend the shared view by including the shared view.
You can use the Thymeleaf Layout Dialect to extend a view.
Layout page
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
...
<body layout:fragment="body">
...
</body>
</html>
Content page
In your content page, you refer to the layout (decorator) page using the layout:decorator attribute.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="layout.html">
...
<body layout:fragment="body">
<p>Actual page content</p>
</body>
</html>
It is possible to have multiple fragments in one page.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
...
<body>
<div layout:fragment="content"></div>
<footer layout:fragment="footer"></footer>
</body>
</html>
What is the best-practice for developing web-pages for both non-JS-enabled and JS-enabled pages? I am developing a Spring MVC web-application using JSP/JSTL as the view technology.
The way I work is to create a full web-page (with "html", "head", etc) tags in it. This will work on all browsers. My entire app works (however ugly-bugly) with JS disabled.
Also included in each page is some jQuery script that prettifies the page, typically turning top-level "divs" into tabs, other divs into dialogs, etc. The JS "hijacks" the underlying HTML. My JS hijacks the links and buttons, using AJAX calls to load the content into the correct dialog or tab div.
This all works nicely and I like the architecture, but I have added an optimisation such that the AJAX requests appends a "contentOnly" parameter; this is picked up by the Spring MVC view which conditionally ignores the "head", etc - i.e. it only renders the real content that the AJAX version wants. It just feels clunky.
I know I could load the entire page and discard the outer bits but this seems inefficient to load all the associated CSS and JS files which are not strictly necessary.
My question is: is there a nicer way to write this conditional formatting, should I be using a different view technology or something else, something like Velocity or FreeMarker or whatever Grails uses?
An example of my conditional: -
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:if test="${param.contentOnly == null}">
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</c:if>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<c:if test="${param.contentOnly == null}">
<%# include file="_footer.jsp"%>
</body>
</html>
</c:if>
You need to include the files the other way round.
/WEB-INF/template.jsp
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
<jsp:include page="${page}.jsp" />
<%# include file="_footer.jsp"%>
</body>
</html>
leads.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
And create a controller which sets leads as ${page} variable based on a specific request URL and forward to template.jsp instead. Since I don't do Spring, I can't answer in detail how to achieve it with Spring. But the following basic JSP/Servlet kickoff example should give the general idea:
#WebServlet(urlPatterns={"/pages/*"})
public class TemplateServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page = request.getPathInfo();
// .. Do some other preprocessing if necessary .. and then:
request.setAttribute("page", page);
request.getRequestDispatcher("/WEB-INF/template.jsp").forward(request, response);
}
}
Invoke it as http://example.com/contextname/pages/leads to get the template displayed with leads.jsp included. Finally you should be able to invoke http://example.com/contextname/leads.jsp independently to get the sole template content.
I have used that kind of approach a few years ago with Freemarker and I don't know better way to do it.
What you could do is to create JSP-tags for those conditions so that they look nicer and hides the actual implementation. Something like this:
<t:NonAjax>
<!DOCTYPE html>
<html>
<head>
<%# include file="_stylesAndScripts.jsp"%>
<title>My App Title</title>
</head>
<body>
<%# include file="_header.jsp"%>
<%# include file="_menu.jsp"%>
</t:NonAjax>
<div id="leadListPanel" class="contentPanel">
<h1>My App Title</h1>
<p>The time on the server is ${serverTime}.</p>
<table id="leadTable" class="list">
... rest of content...
<t:NonAjax>
<%# include file="_footer.jsp"%>
</body>
</html>
</t:NonAjax>
I am trying to do something like this in an aspx page:
<head runat="server">
<% #if DEBUG %>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<% #else %>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<% #endif %>
</head>
I get an error "Preprocessor directives must appear as the first non-whitespace character on a line". How can I do this?
<head runat="server">
<%
#if DEBUG
%>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<%
#else
%>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<%
#endif
%>
</head>
Works for me - note that this is based on the value of the debug attribute in the <compilation> element of the web.config.
Edit to respond to comment
Ah, so you're also adding controls to the head through the code-behind? Then you'll probably need to be adding this dynamically from the code-behind as well.
If you're happy to always serve the minified version, but want to use IntelliSense in Visual Studio you should ensure that you've installed the hotfix to enable this:
VS2008 SP1 Hotfix to Support "-vsdoc.js" IntelliSense Doc Files
This would enable you to name your non-minified version jquery-1.3.2.min-vsdoc.js and have VS read that one in while you're building the pages.
this is worked for me:
<head runat="server">
<asp:PlaceHolder runat="server">
<%
#if !DEBUG
%>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<%
#else
%>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<%
#endif
%>
</asp:PlaceHolder>
</head>
I am writing a small Sinatra-based app and would like each view to be able to insert various items into the layout, for example the page title or extra CSS/javascript references in the head.
Currently my layout (erb) looks like this (simplified):
<html>
<head>
<title>Hard Coded Title Here</title>
<link rel="stylesheet" ... />
</head>
<body>
<h1>Hard Coded Title Here</h1>
<div id="content">
<%= yield %>
</div>
</body>
</html>
Rather than having the title and CSS/JS references hard coded, I'd like to achieve something along these lines:
<html>
<head>
<title><%= yield :title %></title>
<link rel="stylesheet" ... />
<%= yield :more_head_refs %>
</head>
<body>
<h1><%= yield :title %></h1>
<div id="content">
<%= yield %>
</div>
</body>
</html>
And be able to define the content for those blocks from within each view.
Is this possible, and if so how would I go about doing it?
I came up against this issue at Railscamp recently and luckily Tim Lucas was able to point me to something he forked and worked on called sinatra-content-for. This will cover what you need.
I've found this to be the most robust solution for Rails-style 'content_for' functionality in Sinatra, especially if you're using ERB templates rather than Haml:
http://github.com/kematzy/sinatra-outputbuffer
You can just use #stylesheet in your ruby file