Java Tutorial/JSTL/Introduction

Материал из Java эксперт
Перейти к: навигация, поиск

JSTL in XML Format

   <source lang="java">

<?xml version="1.0"?>

<jsp:root xmlns:jsp="http://java.sun.ru/JSP/Page"

 version="1.2">

<jsp:directive.page contentType="text/html"/> <jsp:directive.page import="java.util.Date, java.util.Locale"/> <jsp:directive.page import="java.text.*"/> <jsp:declaration>

 String getDateTimeStr(Locale l) {
   DateFormat df = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, l);
   return df.format(new Date());
 }

</jsp:declaration> <html> <head>

 <title>Example JSP in XML format</title>

</head> <body> This is the output of a simple JSP using XML format.

Use a jsp:scriptlet to loop from 1 to 10:

<jsp:scriptlet> // Note we need to declare CDATA because we don"t escape the less than symbol

 for (int i = 1; i <=10; i++) {
   out.println(i);
   if (i  < 10) {
     out.println(", ");
   }
 }

</jsp:scriptlet>

 Use a jsp:expression to write the date and time in the browser"s locale: 
 <jsp:expression>getDateTimeStr(request.getLocale())</jsp:expression>

<jsp:text>

 <p>This sentence is enclosed in a jsp:text element.</p>

</jsp:text> </body> </html> </jsp:root></source>





JSTL JSP XML Format Page

   <source lang="java">

<tags:xhtmlbasic xmlns:tags="urn:jsptagdir:/WEB-INF/tags"

                xmlns:jsp="http://java.sun.ru/JSP/Page"
                xmlns:fmt="http://java.sun.ru/jstl/fmt"
    xmlns="http://www.w3.org/1999/xhtml">
 <jsp:directive.page contentType="text/html" />
 <head>
   <title>JSPX - XHTML Basic Example</title>
 </head>
 <body>

JSPX - XHTML Basic Example


   <p/>
   <p/>
   <p/>
   <jsp:useBean id="now" class="java.util.Date" />
   <fmt:formatDate value="${now}" pattern="MMMM d, yyyy, H:mm:ss"/>
 </body>

</tags:xhtmlbasic></source>





Setup Environment for Development Custom Tag

MyTag.java



   <source lang="java">

package taglib; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.IOException; public class MyTag extends TagSupport {

   public int doStartTag() throws JspException 
   {
       return EVAL_BODY_INCLUDE;
   }
   
   public int doAfterBody() throws JspException 
   {
           System.out.println("Hello!");
           return SKIP_BODY;
   }

}</source>





Setup Environment for JSTL

web.xml



   <source lang="java">

<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.ru/dtd/web-app_2_3.dtd"> <web-app>

 <context-param>
   <param-name>javax.servlet.jsp.jstl.fmt.timeZone</param-name>
   <param-value>US/Central</param-value>
 </context-param>
 <context-param>
   <param-name>database-driver</param-name>
   <param-value>org.gjt.mm.mysql.Driver</param-value>
 </context-param>
 <context-param>
   <param-name>database-url</param-name>
   <param-value>
   jdbc:mysql://localhost/forum?user=forumuser</param-value>
 </context-param>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/fmt</taglib-uri>
   <taglib-location>/WEB-INF/fmt.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/fmt-rt</taglib-uri>
   <taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/core</taglib-uri>
   <taglib-location>/WEB-INF/c.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/core-rt</taglib-uri>
   <taglib-location>/WEB-INF/c-rt.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/sql</taglib-uri>
   <taglib-location>/WEB-INF/sql.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/sql-rt</taglib-uri>
   <taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/x</taglib-uri>
   <taglib-location>/WEB-INF/x.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>http://java.sun.ru/jstl/x-rt</taglib-uri>
   <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>
   http://java.jeffheaton.ru/taglib/jstl/forum</taglib-uri>
   <taglib-location>/WEB-INF/forum.tld</taglib-location>
 </taglib>
 <taglib>
   <taglib-uri>/tlt</taglib-uri>
   <taglib-location>/WEB-INF/taglib.tld</taglib-location>
 </taglib>

</web-app></source>