"JSP" JSP self-defined label and MODEL1, MODEL2 standard

Source: Internet
Author: User

After JSP2.0 to support their own definition of the label, is now generally jsp2.4 version number, so there is no need to consider the issue of the version number.

It can be used directly. Despite the general development process, very few of them define JSP tags themselves. But through a JSP to define their own label learning. Can understand to a certain extent struts and other frameworks to change the good JSP tags to the mechanism of unrecognizable. Just as jquery is essentially JavaScript, SSH is actually JSP. In fact, there is nothing, and a simple example can explain exactly how the tags are defined in the JSP.

The so-called MODEL2 standard can be achieved by defining labels on their own. No server running code can appear in the Web page that the display layer view gives to others.

Assumed to have appeared. Is the kind of MODEL1 standard.


I. BASIC OBJECTIVES

Defines a clock tag, which also requires an ID to be passed in.

After. Suppose you use this tag in a JSP to pass in an ID. The current time is displayed on the page with the incoming ID. For example, as seen in:



II. Basic Preparation

This thing does not have to be introduced into whatever jar. The Web. xml file can take advantage of the eclipse for Java EE Self-generated, remove useless things, and do not configure whatever things, after JSP2.0 with the definition tag library properties, such as the following see, Web. XML self-generated method can refer to the " Javaweb "Eclipse for Java EE new WebProject self-generated web. xml" (Click to open link)

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "></web-app>

Site Directory structure For example, in the Web-inf under the new tags directory, then our tags are tags:, the name of this directory determines the future tag header, this directory is our tag library. Theoretically the tags can be put anywhere, but. In order not to let the user of practical mind to visit, so still put in Web-inf:



Third, the production process

1, this time relates to a relatively rare, suffix named. tag file, right click just built the tags directory, choose New->other ..., create a new tag file.


2. In the popup dialog box. Enter tags, choose JSP tag, click Next, named Clock, because Clock.tag in tags inside, later call this tag on Tags:clock. The name of your directory and file name determines the name of the tag after which you can start editing clock.tag.


3. clock.tag file As seen below, this file is used to explain what this file is for. This defines the Tag:clock action, OGNL expression inside the s:if tag, that is, in the S directory below a If.tag file, and then write this if.tag run the work, first output the current time, time output Java code and "Java" The same is the thought of System.currenttimemillis (click the Open link). Did not change, and then put the incoming ID to

<!--changed the code of the page header to Utf-8, introducing the corresponding Java Package--><%@ tag language= "java" pageencoding= "Utf-8" import=, Java.text.SimpleDateFormat "%><!--this label will introduce the ID number--><%@ attribute name=" id "%><%// Suppose you want to use incoming parameters in a JSP script to write Request.getattribute ("a");//Output Time Out.println (new SimpleDateFormat ("yyyy years mm DD Day Hh:mm:ss E"). Format (New Date (System.currenttimemillis ())). ToString ());%><!--Assume that this is a direct output, then write-->
4, in index.jsp on the Tags:clock label passed in the number of "Hello." You will see the execution effect. This forms the so-called MODEL2 Web programming rules, that is, the normal Web page must not appear no matter what the code of execution ... Say what can be very good to improve the reusability of Web page code.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%><%@ taglib prefix=" tags "tagdir="/web-inf/tags "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


"JSP" JSP self-defined label and MODEL1, MODEL2 standard

Related Article

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.