Including dynamic pages in JSP - java

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.

Related

Is there a way to declare a root resource for each jsp?

I have multiple spring MVC project that I will need to combine into one...
but say I have two folders of static resources that conflict, so i need to keep them separate:
What i do is declare two resource mapping:
<mvc:resources location="/resources/web1" mapping="/web1/**"/>
<mvc:resources location="/resources/web2" mapping="/web2/**"/>
and in my web1 jsp I do:
<link href="/web1/css/test.css" rel="stylesheet">
<link href="/web1/css/test1.css" rel="stylesheet">
<link href="/web1/css/test2.css" rel="stylesheet">
but what if i need to group my jsp, say i have web1a.jsp, web1b.jsp, web1c.jsp, and i want them all to use the web1/ resource mapping.
Is there a faster way to declare it, like a property that i can put on top of the JSP to declare that the root source is web1 instead of manually adding "web1/" for each tags? Something like:
all jsp that uses web1,
<%#root="web1/" %>
<link href="/css/test.css" rel="stylesheet">
<link href="/css/test1.css" rel="stylesheet">
<link href="/css/test2.css" rel="stylesheet">
so that i could retain the original reference without modifying each individual tags
thanks

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

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}"/>

JSP cannot find css files while using ResourceHandlerRegistry.addResourceHandler (Java based webConfig)

This is where i put my css file:
This is how i register this resource:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
This is how i references this css from the JSP:
<link type="text/css" href="/css/bootstrap.css" rel="stylesheet"/>
I can see the JSP page with content but without styling.
I think the error is while referencing but i cant find it. I have tried also these, but does not work:
<link type="text/css" href="../css/bootstrap.css" rel="stylesheet"/>
<link type="text/css" href="/resources/css/bootstrap.css" rel="stylesheet"/>
<link type="text/css" href="<%=request.getContextPath() %>/css/bootstrap.css" rel="stylesheet"/>
<link type="text/css" href="<%=request.getContextPath() %>/resources/css/bootstrap.css" rel="stylesheet"/>
Any idea?
EDIT: Now i have moved the css folder to WebContent. It seems like this:
It still does not work. What should i register for the ResourceHandler?
registry.addResourceHandler("/WEB-INF/**").addResourceLocations("/WEB-INF/"); ?
How should i reference the css from the JSP?
The resource handler doesn't look for resources on the class path, by default. It looks for them in the webapp.
Create a folder called resources and put it in /src/main/webapp. Then put your css, js, etc. folders in there.
The files under src cannot be accessed on default condition , you should put these assets into WebContent directory.
Resources usually means config proppertis like properties and xml, they will be used by the
java code, assets usually mean that can be accessed by the browser directly,Notice that WEB-INF is protected,you need to put your css directory under WebContent, except for WEB-INF
for exampel,you can put your css directory under /WebContent/assets/
And add these code at the head of your jsp pages
<%
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ request.getContextPath();
%>
<link type="text/css" href="<%=basePath%>/assets/css/bootstrap.css" rel="stylesheet"/>
It's not good to use relative path!!

Unable to understand how Playframework works

I installed playframework and have a question.I looked at the helloworld tutorial but it seems to use groovy.
#(message: String)
#main("Welcome to Play 2.1") {
#play20.welcome(message, style = "Java")
}
The first line is the function definition. What does play20 stand for. I am really new to Scala and I cant make head or tail out of it.
#(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>#title</title>
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="#routes.Assets.at("images/favicon.png")">
<script src="#routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
</head>
<body>
#content
</body>
</html>
This is just standard HTML which accepts html content and a title string. But how is this file getting called from the index.scala.html?
The #play20.welcome() part calls a Scala method, not that different from Java.
As for the HTML templates, they're compiled into Scala classes as well, a bit like JSP is compiled into servlets.
The example you are refering to sounds like it's about Play 1, while the framework you are trying out is play 2, which is a rather different thing. Play 2 has it's own template engine.
The # is the symbol that signals you're going to start a Scala expression. Like < ?php ? > or <% %> for intance in other langauges. The only difference is that you don't have a trailing symbol, because the template engine stops parsing at the end of the expression and automagicly returns to evaluating the template as html.
play20 is an object that is in scope for the template engine, like things in java.lang are in in scope in a regular java file. E.g. String.
In this case play20 is like a class with a static method in Java.
In this tutorial you have good simple introduction to how to use the Play 2 framework

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