How to print random value next to original value using jstl? - java

I am using JSTL to push the CSS in HTML pages
like if CSS name is abc.css I want to append timestamp next to CSS due to caching issue abc.css?time=21223233232 using JSTL
<c:forEach var="css" items="${styles}">
<link type="text/css" rel="stylesheet" href="/<c:url value="${css}"/>" />
</c:forEach>
Currently, I am using this way to push CSS to HTML page
how can I use the same code to append timestamp parameter?

You need to get the base URL and the time-part as variables. Then you´ll have to "concatenate" that, like ...href="${css}?time=${time}"/>

Related

Read an html attribute value from a Thymeleaf fragment

i'm using thymeleaf as the template engine on a Java - Spring web application which is already a completed website, what i'm working now is to introduce some <meta> tags in the header to optimize the interaction with social network platforms.
My goal is to accomplish this modifying as little as possible of the templates code, and to have some flexibility on which values goes into the tags content.
Right now the templates are structured as follows:
Layout.html which contains a couple fragments for head and body.
<head th:fragment="common_head(title, links, scripts)" th:assert="${!#strings.isEmpty(title)}">
...
</head>
<body th:fragment="common_body(content, body_end)">
...
</body>
And the templates for the actual pages are something like this.
<head th:replace="common/layout :: common_head(~{:: head/title}, ~{}, ~{:: head/script})">
<title>My page title</title>
<script>
console.log('some page specific JS code');
</script>
...
</head>
<body th:replace="common/layout :: common_body(~{ :: body/content }, ~{ :: body/bottom })">
<div class="wrapper" th:fragment="content">
some content here
</div>
<th:block th:fragment="bottom">
more content here
</div>
</body>
I know i can just add another parameter to common_head fragment and pass the <meta> tags to it the same way i'm doing with title, scripts, etc. but i was thinking on another approach which will lead to less repetition of code to inject the values into the header.
In the layout common_head fragment i have this:
<meta property="og:title" th:with="og_value = ~{this :: %og_title/text()}" th:content="${og_value ne null ? og_value : 'Lorem ipsum'}">
<meta property="og:description" th:with="og_value = ~{this :: %og_description/text()}" th:content="${og_value ne null ? og_value : 'Lorem ipsum'}">
<meta property="og:image" th:with="og_value = ~{this :: %og_image}" th:content="${og_value ne null ? og_value : 'path-to-default-img'}">
The idea is to use fragment selectors to pick the right content to inject from the page template, this way in a page i can just mark a tag (maybe a div, a paragraph, something relevant for that template) with th:ref="og_description" and it's value will become the content of the <meta> tag in the head.
This works really well for og_title and og_description tags, but the problem arises with the og:image meta tag, in which i need to inject the value of the src attribute of an <img> tag marked with th:ref="og_image".
I couldn't find any way to read an attributes value from the fragment, is there a way to do this?.
I can see that the selected fragment is actually an instance of org.thymeleaf.standard.expression.Fragment but i don't see any method i can use to access the html attributes in it.
If this is not technically possible, is there a better approach to this use case?

Including dynamic pages in JSP

I've a Java WebApp. I have put some attributes inside the context and init params to get them when I needed.
Content of head.jspf:
<link href="${initParam['bootstrap_css_cdn']}" rel="stylesheet" media="screen">
<link href="${applicationScope['css_dir']}basic.css" rel="stylesheet" media="screen">
From index.jsp, if I do this:
<jsp:include page="WEB-INF/jspf/head.jspf" />
it works perfectly!
But if I do this:
<jsp:include page="${applicationScope['headURL']}"
it doesn't work at all (the "headURL" variable is a string with the right URL). I mean, the jspf is included but, for example, the following code is written in the final html code literally:
${applicationScope['css_dir']}
What am I doing wrong?
#JBMizet wrote in a comment:
JSPF files are not compiled. They're supposed to be included statically, not dynamically (i.e. with <%#include %>). Change the extension to .jsp if you want a dynamic include.

How to write content before and after the output of a JSP

I have JPSs that represent Components. I want to have these component JSPs write some HTML before and after the contents of the JSP is executed.
component.jsp
<#page session="false">
<%= "hello " + "world" %>
when this JSP/servlet is rendered, I want it to render:
<div class="component">
hello world
</div>
I want to be able to create various "wrappers", and depending on the JSP, include wrap the contents of the JSP with the correct content. If I want to change/augment the wrapper down the road, I want to only do it in one place (Could be 100s of components).
Can i do something with <#page extends="..."> possibly?
Thanks
What do you want is named: tag files. Introduced on JSP 2.0
With this approach you can write JSP tags using jsp, therefore you need to create a folder named WEB-INF/tags, and create a 'normal' jsp within this folder.
The tag that you want to create needs to have the following start instruction:
<%#tag description="tag description" %> in order to indicate this is a tag.
To use it you will need to reference the tags you want to use with the following instruction: <%# taglib tagdir="/WEB-INF/tags" prefix="custom"%>
So, you can do something like:
WEB-INF/tags/myTag.tag
<%#tag description="hello tag" %>
<%#attribute name="name" required="false" type="java.lang.String"%>
<html><head></head><body>
<h1>Hello <%=name%></h1>
<jsp:doBody />
</body>
index.jsp
<%# taglib tagdir="/WEB-INF/tags" prefix="custom"%>
<custom:myTag name="My Name">this is the content</custom:myTag>
The result will be a page printing
<html><head></head><body>
<h1>Hello My Name</h1>
this is the content
</body>
This is a terrible idea. JSPs with scriptlets are 1998 technology. No one writes these anymore.
If you must write JSPs, you're better off using JSTL and something like SiteMesh or Tiles to composite pages.
An even better idea would be to start moving towards something that might allow you to easily run a mobile solution alongside your web app. Services and templates would be my preference over JSPs.

Regex to find <link rel="stylesheet" tag

I need to find the link tag using Regex.
I have this line in my html file.
<link rel="stylesheet" href="<c:url value="/styles/folders/masterTree.css" />" type="text/css" media="screen, print" />
I need a regex expression to find this.
This is not a homework. I need this as part of my office requirement.
Thanks to all in advance.
Using regex to parse html can be problematic as most (x)html is not actual valid.
Because of all the edge cases you end up with it breaking before long.
You don't specify what language you are developing in but if you are working in .net I would suggest looking into using HtmlAgilityPack:
http://runtingsproper.blogspot.com/2009/09/htmlagilitypack-article-series.html
You shouldn't. A real HTML parser is the only reliable way to parse HTML.

How to open print version of the site in a new page with jsf?

I need to make a link which opens print version of current page in a new tab. I already have correspondent css-file. But I don't know how to specify when this file should be used instead of standard.
The simplest way is quite good. If I was using JSP I would simply add get parameter to print-link URL. Is there any way to achieve similar results with jsf?
Use EL to specify the CSS file dynamically, here's an example which checks the presence of the print request parameter (thus, <h:outputLink value="page.jsf?print" target="_blank"> would suffice):
<link rel="stylesheet" type="text/css" href="${not empty param.print ? 'print.css' : 'normal.css'}" />
You can also retrieve it as a bean proprerty the usual JSF way:
<link rel="stylesheet" type="text/css" href="<h:outputText value="#{bean.cssFile}" /> " />
If you're on Facelets instead of JSP, then you can also use unified EL in template text:
<link rel="stylesheet" type="text/css" href="#{bean.cssFile}" />
If you actually don't need a "print preview" tab/page, then you can also just specify the media attribute in the CSS link and let the link/button invoke window.print() during onclick instead of opening in a new tab.
<link rel="stylesheet" type="text/css" href="normal.css" media="screen, handheld, projection" />
<link rel="stylesheet" type="text/css" href="print.css" media="print" />
When the page is about to be printed, the one specified by media="print" will automatically be used instead.
You can add get parameters to any JSF link by using the f:param tag.
<h:outputLink value="/somepage.xhtml" target="_blank">
<h:outputText value="Link to Some Page"/>
<f:param name="someparam" value="somevalue">
</h:outputLink>
This will render something basically like this:
Link to Some Page
You can add multiple params with more f:param fields. Alternatively, if it's static, you can just add it as part of the outputLink itself.
<h:outputLink value="/somepage.xhtml?someparam=somevalue" target="_blank">
<h:outputText value="Link to Some Page"/>
</h:outputLink>
The problem, of course, being that you cannot do this and trigger server-side events. I've yet to figure out how to do this from a POST back and get it in a new tab.

Categories