Struts 2 Custom Tag Library - java

I created a custom tag library for my struts 2 project. Now I want to put all my source code into a jar file. It's working fine but my concern is I still need to configure my .tld file in the web xml for it to work:
<jsp-config>
<taglib>
<taglib-uri>/custom-tags</taglib-uri>
<taglib-location>/META-INF/mytaglib.tld</taglib-location>
</taglib>
</jsp-config>
Is there a way to get rid of this and configuring it in the jar file instead?

This shouldn't have anything to do with Struts2, really. To get away from configuring this in the web.xml, you should just be able to place your .tld file into a META-INF folder at the root of your jar and declare the uri inside the .tld using <uri>/custom-tags</uri>.

Related

How to specify a file path in web.xml file

i am developing an application in java and i am new to this java ee platform.I have a dataset file called "51Degrees-EnterpriseV3_2.dat" in my resource folder. how do i specify the path of this in my web.xml file and how do i retrieve the file in code?
In web.xml , you can add
<context-param>
<param-name>you can mention here localtion variable name</param-name>
<param-value>location here </param-value>
</context-param>
The other answer shows the correct way to add the parameter to your web.xml. Which for the 51Degrees V3.2 data file would look like this:
<context-param>
<description>The name of the device database.</description>
<param-name>BINARY_FILE_PATH</param-name>
<param-value>51Degrees-EnterpriseV3_2.dat</param-value>
</context-param>
As far as I know, there is not a way to get a resource which is located in the resources path (it's an XML file so the .getResource(name) method cannot be used). However, as you have a web.xml file, I assume this is a web project (i.e. .war rather than .jar)? If this is the case, then the WEB-INF directory is what you need. Files in here are also packaged up like resources, but can be more easily consumed by a .war package.
If you put your data file in src/main/webapp/WEB-INF/, which is used as the root by the web project. So the above XML example will work if the path to your data file is:
src/main/webapp/WEB-INF/51Degrees-EnterpriseV3_2.dat
As a sidenote, there is more documentation on configuring 51Degrees in a Java web app here

Apache tomcat and servlet (beginner) - 404 error, maybe classes are not found?

I'm a beginner in servlets and for a "hello world style" servlet I installed apache tomcat and created the following under it's webapps directory:
servletdir
|- index.xhtml
|- WEB-INF
|- web.xml
|- classes
|- mypackage
|- ServletClass.class
Initialized everything so I could access my servlet on localhost:
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>mypackage.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/servleturl/*</url-pattern>
</servlet-mapping>
</web-app>
and the index.xhtml contains a form that has action="/servleturl" method="get"
In theory, when I start up tomcat, I should be able to access both the servlet and index.xhtml file now through localhost:8080/servletdir/index.xhtml and submitting the form there. Reaching index is no problem, but when I submit the form, I get a 404 error for the servlet, saying that it "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".
My servlet class should not be the problem, it extends HttpServlet and overrides doGet to output a simple html via the ServletResponse parameter's getWriter().println(). Can an error in the servlet class cause a 404? Is my directory structure or web.xml wrong?
I have tried multiple directory structures and solutions, including:
flat dir structure in WEB-INF and class not being in package (no "classes" directory)
no WEB-INF, everything in servlet dir directly
wrap WEB-INF and index.xhtml in a directory named "WebContent"
keep "classes" directory but use no package, put class files directly into classes*
I have followed multiple resources including some tutorials and many questions here which are not directly related to my problem, but contain some info, but as I'm a complete newbie to this technology I can't deduce which resource is good and which isn't, that't why I tried almost everything I found.
The directory structure described at the top is directly on the disk as this structure, not in a war file, but as I can access some parts, I guess this isn't the problem.
*: note that I recompiled the java files with the according "package" at the file's top line every time the directory structure representing the package changed, that's not the problem
change action="/servleturl" method="get" to action="/servletdir/servleturl" method="get" or action="servleturl" method="get".
with action="/servleturl",you are trying to access a root relative path but you are missing the context path servletdir.
If you use the action="servleturl", you don't need to put the context path in the action as it will get it from the current url

adding directory to my servlet web-app web.xml

Basically I have servlet web-app and I have a directory out the my web-app project say D:\resources, here in my porject web.xml I need to specify that the resources that my web-app would need could be found at D:\resources, I would really appreciate if you tell me how to specify a directory to my project web.xml file.
Thanks in advance
Best way of achieving this is to configre your external resources with defining JNDI in your application server and then referring the same in your web.xml like for e.g.
<resource-description>
<res-ref-name>myAppResources</res-ref-name>
<jndi-name>myResource</jndi-name>
</resource-description>

Servlet - web.xml vs Java config

I'm migrating an old project from a web.xml approach to a complete Java-style Servlet 3.0 configuration.
But I can't understand how to translate part of the XML configuration in Java code. In particular the next snippet:
<jsp-config>
<taglib>
<taglib-uri>....</taglib-uri>
<taglib-location>....</taglib-location>
</taglib>
</jsp-config>
Any hint would be welcome!
As a secondary, more academic, question: do Servlet 3.0 API offer a full coverage of what you could do with XML, or not?
Stefano,
Since JSP 2.0, there is no need in put <taglib> tag in web.xml. From Head First Servlets and JSP book:
The Container automatically builds a map between TLD files and names, so that when a JSP invokes a tag, the Container knows exactly where to find the TLD that describes the tag.
How? By looking through a specific set of locations where TLDs are allowed to live. When you deploy a web app, as long as you put the TLD in a place the Container will search, the Container will find the TLD and build a map for that tag library.
So, all you have to do is to have a TLD file with the correct URI.
Places to put your TLD file:
Directly inside WEB-INF
Directly inside a sub-directory of WEB-INF
Inside the META-INF directory inside a JAR fi lethat’s inside
WEB-INF/lib
Inside a sub-directory of META-INF inside a JAR fi lethat’s inside
WEB-NF/lib

How to change location of static file in WAR archive?

By default static files are located in WEB-INF directory (accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
images
logo.png
I want to change the structure and use this one instead (still accessible as /images/logo.png):
foo.war
WEB-INF
web.xml
static
images
logo.png
How can I do this with web.xml?
The container will repsond with a 404 NOT FOUND error if you directly access the files under WEB-INF using HTTP GET .
But now , you said you can access WEB-INF/images/logo.png by /images/logo.png , so I think your web application most probably achieve this result by some URLRewriteFilter mechainsim or by some Java code in the servlet level (eg a filter) , or by your web application 's framework . I suggest you to check your web application to see what mechanism causes this behvaior now and configurate to your desired result accordingly.
According to http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/WCC3.html,:
A WAR has a specific directory
structure. The top-level directory of
a WAR is the document root of the
application. The document root is
where JSP pages, client-side classes
and archives, and static Web resources
are stored.
The document root contains a
subdirectory called WEB-INF, which
contains the following files and
directories:
web.xml: The Web application
deployment descriptor Tag library
descriptor files (see Tag Library
Descriptors) classes: A directory
that contains server-side classes:
servlets, utility classes, and
JavaBeans components lib: A
directory that contains JAR archives
of libraries (tag libraries and any
utility libraries called by
server-side classes).
You can also create
application-specific subdirectories
(that is, package directories) in
either the document root or the
WEB-INF/classes directory.
So the default behavior is what you're looking for. Is your document root set incorrectly to serve content from WEB-INF?
You may use a filter or URLRewriteFilter to point /images/* to /static/images/*.
If you just want your folder structure to be /static/images for development time organization purposes, but the deployment URL to be /images -- you may need to alter your build script to copy /static/** to /.
I personally would not bother whether my static files are referred as /static/images or /images -- because they would be referred in my code (only), which I have control over.
If you are using these files in CSS and that's why you wanted the path to stay the same... better keep the images under /static/css/images and have the images that are referred in the CSS here. In this way, no matter where you move your CSS folder, you would not bother spoiling your CSS.

Categories

Resources