JSP Template tutorial. How do I use a jar file - java

I am trying to follow the tutorial for JSP Templates at:
http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates/
I am struggling to understand the <%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %> tag.
What is the template.tld file, where does this come from?
I have tried to download the Resourses file of source code but this just contains a src.jar file. How do I use this? I can't even open the file!?! How do I see the source code example?

It's in the jstl.jar. You need standard.jar and jstl.jar in your WEB-INF/lib for JSTL.
You can get what you need here:
http://tomcat.apache.org/taglibs/
Download the standard tag library and all its JARs; put them in your WEB-INF/lib directory.

A uri, a uniform resource identifier is not necessarily pointing to any existing resource. It is an identifier, compare with url which is a uniform resource locator which helps you locate something. In many XML contexts for instance, it simply declares a namespace.
Sometimes the uri does however point to a real resource that you can read to get more information.
In this case you are declaring a namespace template to use in your jsp code (such as <template:dosomething/>) - the uri matches a uri declaration for whatever is implementing your tag functionality and can be found inside a jar (jstl.jar or standard.jar, can't remember which).

Related

What's Thymeleaf "prefix" relative to?

I'm just starting using Thymeleaf and I'm trying to understand the setPrefix() method in the template resolver. There are cases that I understand, like setPrefix("classpath:/com/package") or setPrefix("file:folder") (relative to working directory).
But I still don't understand what, for example, setPrefix("/views/") refers to. Where Thymeleaf expects the views folder to be? Does the / at the beginning means that is an absolute path? If so, where's the 'root'?
Does it depends of the specific TemplateResolver? In my instance, I'm using the SpringResourceTemplateResolver.
If you are using SpringResourceTemplateResolver, the prefix is relative to the root of the war file (or the web-app base directory when unpacked).
When using Maven as build system, the source folder for this is src/main/webapp. For example, if you setPrefix("/views/") your templates should be put in src/main/webapp/views/.
Hint: Use WEB-INF directory to protect from end-users retrieving the templates directly.

Linking Files And Directory Structuring

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

What are files located in *.jar/META-INF/*.*?

I found that c.tld file of jstl-1.2.jar is located in META-INF/*.* inside this jar. How to access these files from code in this jar? Just with getResourceAsStream("/META-INF/c.tld")? Or META-INF has special processing rules?
It's ok to get the content of the c.tld using getResourceAsStream. Just do not change it and write back to the file.
https://stackoverflow.com/a/70253/32090
tld files are related to Tag libraries. These files will specify when you use a tag in JSP, which class to be executed and which method to be executed. You can access TLDs using URI attribute of taglib directive in JSP.
JSTL tutorial on how to use these tag libraries is available at : http://www.roseindia.net/jstl/introduction.shtml

directory structure in Tomcat 5(s)

I was wondering if it was possible to maintain a directory structure within the application folder in side webapps folder, i.e. I have created a folder called ITC357, which acts as my application folder which carries all my files, so the directory path is as follows:
C:/Program Files/Tomcat5/webapps/ITC357
I'm doing an assignment and I would like to deploy that ITC357 in a separate folder can I do this? if so how?
It is not clear what you are asking:
If you asking if it is possible to have directories inside C:/Program Files/Tomcat5/webapps/ITC357 then the answer is "Yes".
If you are asking if it is possible to put your webapps file in a separate directory then the answer is "Inadvisable". The tomcat framework looks for certain files within the webapps/<name> tree; e.g. a context.xml file, a web.xml file, classes / JARs, etc. You could code your servlet to look for other things in other places, but this causes various problems with deployment (and undeployment) and security.
If that doesn't cover it, please clarify your question.
If you are writing JSPs, then the URL of the JSP will include any subdirectories that you create. So if under ITC357 you create a subdirectory called "foo", and in that subdirectory you have a JSP called "bar.jsp", and your context name is "plugh", then the URL of that JSP will be "http://whateverserver.com/plugh/foo/bar.jsp".
If you are using servlets, then it is up to your code to decide what to do with any URLs passed to it. You could map "http://whateverserver.com/plugh/foo/twisty.do" to "c:/program files/tomcat5/webapps/ITC357/WEB-INF/classes/foo/twisty.class", or you could map it to someplace totally different. (I prefer to map to a package name that matches the URL unless there's good reason to do otherwise.)

Deploying a jar in Jboss

I have some .jspf files that are fragments which I include in a new .jsp file. The reason they are fragments is that they are reused across multiple jsps with some additional components.
My issue now is that I want to use these .jsps in 2 different .war files.
So I created a new .jar file which includes these jspf, now I am trying to deploy this jar so that I am then import it in my new jsp which is inside 1 of the 2 wars.
I am not able to deploy the jar succesfully in Jboss 4.2. I am using Eclipse ide.
Any ideas on this? If there is an alternate approach I would appreciate any ideas.
Thanks for this suggestion, I have followed this idea and deployed my war1 which contains my .jspf files.
So in my war2 on a jsp I do:
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf" var="easyPayName"/>
<%# include file="easyPayNamePaymentOption.jspf" %>
However I am not able to render this page with the included fragment, I am not sure how to address the imported jspf. I ahve tried several different ways like:
<%# include file="/sharedComponents/easyPayNamePaymentOption.jspf" %>
and also using var name like:
<%# include file="#{easyPayName}" %>
However it keeps looking inside the current war. How can I tell it to include the newly imported fragment and display it?
Thanks in advance.
I can see what you're trying to do, but this isn't going to work with a JAR file. All JSP files (including JSPF) have to inside a WAR, not a JAR.
The simplest solution is to put copies of the JSPF files into each WAR that needs to use them. Assuming that you don't want to do this, then there is an alternative, called a cross-context WAR.
By default, JBoss allows its webapps to request resources from each other. For example, say webapp1 (context path /app1) wants to import JSPF /my.jspf from webapp2 (context path /app2). You can use JSTL to do this, from inside webapp1:
<c:import context="/app2" url="/my.jspf"/>
So if you were to create a "shared" WAR file containing your JSPF files, and deployed this to JBoss, then your other webapps could use the above technique to include the contents of the JSPFs into their own JSPs.
edit: I've read your updated question, and I don't understand why you added var="easyPayName" to the <c:import> tag. All that's doing is importing the contents of easyPayNamePaymentOption.jspf and storing it in a variable called easyPayName, which seems completely unnecessary.
I think perhaps you're associating <c:import> with a java import? If so, don't - they're completely different. <c:import> should really have been called <c:include>, because that's what it does.
Just keep it simple, remove the attribute, and just have
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf"/>
That's all you need to do - it will include the content of easyPayNamePaymentOption.jspf directly in the JSP.

Categories

Resources