Java Tutorial/JSTL/Introduction — различия между версиями

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

Версия 17:44, 31 мая 2010

JSTL in XML Format

<?xml version="1.0"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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. 
<br />
<div>Use a jsp:scriptlet to loop from 1 to 10: </div>
<jsp:scriptlet>
// Note we need to declare CDATA because we don"t escape the less than symbol
  for (int i = 1; i &lt;=10; i++) {
    out.println(i);
    if (i  &lt; 10) {
      out.println(", ");
    }
  }
</jsp:scriptlet>

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

<jsp:text>
  &lt;p&gt;This sentence is enclosed in a jsp:text element.&lt;/p&gt;
</jsp:text>
</body>
</html>
</jsp:root>





JSTL JSP XML Format Page

<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>
    <h1>JSPX - XHTML Basic Example</h1>
    <hr/>
    <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>





Setup Environment for Development Custom Tag

MyTag.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;
    }
}





Setup Environment for JSTL

web.xml



<?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>