Jstl configuration and use in Tomcat

Source: Internet
Author: User
Tags tld

1. Install jstl in Tomcat
Prerequisite OS: win2003; tomcat5.5; tomcat has been configured.

1. Prepare jstl
To begin
Decompress and become a jakarta-taglibs-standard-1.1.2

2. Prepare the Web Development Directory
For example, in the default directory % tomcat_home %/webapps/root of my web directory system, the working directory is root, and you can also customize the working directory, but make sure that the WEB-INF/lib, WEB-INF/classes are created under the working directory

3. Copy the. jar file.
Copy two jar files under jakarta-taglibs-standard-1.1.2/lib/: Standard. jar and jstl. Jar files to/WEB-INF/lib/under the working directory

4. Copy the. TLD File
Copy 15 TLD files under jakarta-taglibs-standard-1.1.2/TLD/to "working folder/WEB-INF/TLD"

5. Create a web. xml file under/WEB-INF/under the working directory:
<? XML version = "1.0" encoding = "ISO-8859-1"?>

<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
Version = "2.4">
 
<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/fmt </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/FMT. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/fmt-1_0 </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/fmt-1_0.tld </taglib-location>
</Taglib>
<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/fmt-1_0-rt </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/fmt-1_0-rt.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/core </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/C. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/core-1_0-rt </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/c-1_0-rt.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/core-1_0 </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/c-1_0.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/ SQL </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/SQL. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/sql-1_0 </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/sql-1_0.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/sql-1_0-rt </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/sql-1_0-rt.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/x </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/X. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/x-1_0 </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/x-1_0.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/x-1_0-rt </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/x-1_0-rt.tld </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/fn </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/fn. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/permittedTaglibs </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/permittedtaglibs. TLD </taglib-location>
</Taglib>

<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/scriptfree </taglib-Uri>
<Taglib-location>/WEB-INF/TLD/scriptfree. TLD </taglib-location>
</Taglib>
</Web-app>
6. Create a file named test. jsp
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
<HTML>
<Head>
<Title> test your first jstl webpage </title>
</Head>
<Body>
<C: Out value = "Welcome to test your first webpage using jstl"/>
</BR> your browser is: </BR>
<C: Out value = "$ {header ['user-agent']}"/>
<C: Set Var = "A" value = "David O 'davies"/>
<C: Out value = "David O 'davies" escapexml = "true"/>
</Body>
</Html>

7. Display Results:
Welcome to test your first webpage using jstl.
Your browser is:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sailbrowser;. Net CLR 1.1.4322) David O 'davies
8. Notes
The main problem is the path problem. I have configured it several times and it cannot be successful, that is, I have mistaken the working directory and mistaken the project directory as the working directory, configuration files under the project directory should be configured in the working directory. Of course, you can change the working directory to the project directory to solve the problem.
9. Common Problems and prompts:
Org. Apache. Jasper. jasperexception: the absolute URI: http://java.sun.com/jstl/core cannot be resolved in either web. xml or the jar files deployed with this application
One of the main reasons for this problem is the lack of standard under the/WEB-INF/lib/working directory. jar and jstl. JAR file, another possibility is web. no tag is correctly configured in XML.
According to TLD or attribute directive in Tag file, attribute value does not accept any expressions
A JSP exception occurs during application deployment and running. When jstl library is used: According to TLD or attribute directive in Tag file, attribute value does not accept any expressions, it may be because the jsp2.0 version is used, and the standby version of The jstl core library (RT Library) is not used. There are two solutions:

A. modify web. xml.

<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4">
Changed to Version 2.3

<! Doctype web-app public "-// Sun Microsystems, Inc. // DTD web application 2.3 //" http://java.sun.com/dtd/web-app_2_3.dtd ">
<Web-app>
B. Use the jstl core RT Library

The jstl core library has two types of taglib pseudocommands. The Rt library relies on the traditional jsp request-time attribute value instead of El for implementation (called El library. jsp2.0 will support El)

JSP using <% @ taglib uri = http://java.sun.com/jstl/core prefix = "C" %> in Version 2.3 can be, in version 2.4 will not work, is it version incompatible?

As long

<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "C" %>
Change

<% @ Taglib uri = http://java.sun.com/jstl/core_rt prefix = "C" %>
No problem

 

Iii. Use of tags

C Standard tag Library
Taglib-http://java.sun.com/jstl/core
Basics:
1. Introduce the C tag Library to the JSP page:
<@ Taglib uri = "http://java.sun.com/jstl/core" prefix =" C "> // introduce the tag library prefix to C.
2. c tag library tag list

C tag library example
Tag Name usage
<C: Choose>
<C: foreach>
<C: fortokens>
<C: If>
<C: Import>
<C: otherwise>
<C: Out> output the object value to jspwriter
<C: param>
<C: Redirect>
<C: Remove> delete a variable or attribute
<C: URL>
<C: When>
<C: Set> 1. Used to set a value in a specific scope (request, session, application, etc.)
2. Set attributes of an object
<C: Catch> capture tags embedded in it and throw an exception

(1) <C: Catch> tag
Purpose: capture tags embedded in it and throw an exception
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix =" C "%>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <title> C: Catch tag demonstration </title> <Body>
<C: Catch Var = "myexception"> // set the exception handle, just like exception e in Java.
<%
String STR = "ABC ";
Int I = integer. parseint (STR); // The conversion here produces an exception because the ABC string cannot be converted to a value.
%>
</C: Catch>
Exception: <C: Out value = "$ {myexception}"/>
</Body>
</Html>

(2) <C: Set> label
Purpose: 1. Set a value in a specific scope (request, session, application, etc.)
2. Set attributes of an object
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix =" C "%> // introduce the tag library and set the prefix
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head> <title> demo C: Set tag </title> <Body>
<C: Set Var = "objinrequest" value = "abcstr" scope = "request"/>
// Equivalent to request. setattribute ("objinrequest", "abcstr ")
<C: Out value = "$ {objinrequest}"/>
// Obtain the objinrequest attribute in the request and output the value to jspwriter
</Body>
</Html>

User. Java // This is the definition of a JavaBean object
Package dev;
Public class user {
Public int ID;
Public String username;
Public String password;
// Public type getter ....
// Public void setter ....
}
// JSP page
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix =" C "%>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<JSP: usebean id = "userhandle" class = "Dev. User"/>
<HTML>
<Head> <title> demo C: Set tag </title> <Body>
<C: set target = "$ {userhandle}" property = "Id" value = "123"/>
<C: set target = "$ {userhandle}" property = "username" value = "lindeqiang"/>
<C: set target = "$ {userhandle}" property = "Password">
This is my password // note that the value of the password here can also be assigned to the <C: Set TAG body.
// <C: set target = "$ {userhandle}" property = "Password" value = "123456"/>
</C: Set>
ID: <C: Out value = "$ {userhandle. ID}"/> <br>
User: <C: Out value = "$ {userhandle. Username}"/> <br>
PWD: <C: Out value = "$ {userhandle. Password}"/> <br>
</Body>
</Html>

3. <C: Out> label
Purpose: output the object value to jspwriter.
Attribute:
Attribute description
Attribute type description
Value Object Calculation Expression
Whether the escapexml Boolean is converted to the character entity code. The default value is true.
Default Object D when the value object does not exist (null), the default value is output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.