Spring framework tagging library beginner's Guide

Source: Internet
Author: User
Tags tld
Spring is an application framework that serves all layers: it provides bean configuration basics, AOP support, JDBC extraction framework, and abstract transaction support. It has a very notable feature: at a certain level, if you do not need spring support, you can use only some of its functions instead of the string class. From its design philosophy, you can see that string helps you achieve true separation between the logic layer and the web layer.

Compared with EJB, spring is a Lightweight J2EE application development framework. The lightweight mentioned here refers to the Spring framework itself, rather than spring can only be applied to lightweight application development. The lightness of spring is reflected in the infrastructure of its framework and its support and Assembly capabilities for other application tools. Compared with the giants like EJB, spring reduces the risks between different technical layers. EJB has a strong cohesion. For example, Data Persistence Layer Management, transaction management, and lifecycle management are all handed over to the EJB container for management, the white box feature of cohesion makes it necessary to discard some controllability and trust container capabilities. Spring considers how to "not build wheels" and how to assemble these wheels to make them rotate better. For example, you can use hibernate for Data Persistence Layer Management and jakartacommonlogging for log management. A more appropriate metaphor is: "We need to pin a few nails. To achieve this goal, the hammer manufacturer has produced a series of hammers, large and small. Each hammer has a set of complicated methods to use, it's a headache for us. To solve the problem of too many hammers, we can use several hammers through spring, but it seems that there is only one hammer ". (Note: I have seen this article on the Internet. I personally think it is very vivid to cite this metaphor here.) There are also some simple summary about this framework: "Spring has several main parts: bean + applicationcontext: searches, manages, assembles, and uses system components in a unified and IOC manner, replacing all factories, Persistence frameworks, and Web frameworks. The "J2EE framework" is rodjohnson's vision, and he hopes that spring will provide a base-to-top comprehensive infrastructure support for J2EE Lightweight solutions ."

The above is a brief introduction to a new framework, which is said to be the development direction of J2EE in the future. I used to discuss the best J2EE combination on the forum. I heard that this jstl + Struts (or webwork) + spring + hibernate combination is very promising. If you are interested, go to the springframework Chinese forum. The following is the subject of this article.

Spring tag library introduction and usage:

This article focuses on the tag library provided by spring and Its Usage:

When you use Spring framework in your project, you can select the tag library that comes with Spring framework for the presentation layer. Of course, this is not your only choice. You can choose another tag library or template replacement technology. Spring framework does not provide a rich tag library for applications. The markup library provided by Spring framework is only available for use, and it is not all-encompassing. This is different from struts's rich tag library. In fact, this is in line with rodjohnson's starting point, neither making wheels ". Of course, the Spring framework tag library has its own characteristics, which can be a useful supplement.

The spring tag library is classified as follows:

1. Spring: hasbinderrors

2. Spring: bind

3. Spring: Transform

4. Spring: Message

5. Spring: htmlescape

6. Spring: Theme

Next we will introduce the usage in detail:

1. Spring: hasbinderrors

Corresponds to the processing class of the org. springframework. Web. servlet. tags. binderrorstag tag library.

This tag provides the errors used to bind the object. If this tag is used, errors about this object will be displayed on the page. The prerequisite for using this tag is to use the <spring: bind> tag first, and the <spring: hasbinderrors> tag cannot be used to indicate the object state, it can only bind attributes of objects and objects. The usage is as follows:

<Spring: hasbinderrors name = "priceincrease">

<B> Please fix all errors! </B>

</Spring: hasbinderrors>

Through this simple example, let's take a look at the tag attributes.

Name: name of the bean to be checked. This attribute is required.

This is a simple usage, so we will not introduce the errors variable it contains. For more information, see the English document. The same below.

2. Spring: bind

Processing class corresponding to org. springframework. Web. servlet. tags. bindtag tag Library

This tag is used to assign values to attributes of a bean or bean. It is usually used with form, which is equivalent to action. It specifies the form to be submitted to the class or class attributes.

The path attribute is required to specify the path of the class to be transferred. Example:

<Form method = "Post">

<Table width = "95%" bgcolor = "f8f8ff" border = "0" cellspacing = "0" cellpadding = "5">

<Tr>

<TD alignment = "right" width = "20%"> increase (%): </TD>

<Spring: bind Path = "priceincrease. Percentage">

& Lt; TD width = "20%" & gt;

<Input type = "text" name = "percentage" value = "<C: Out value =" $ {status. Value} "/>">

</TD>

& Lt; TD width = "60%" & gt;

<Font color = "red"> <C: Out value = "$ {status. errormessage}"/> </font>

</TD>

</Spring: bind>

</Tr>

</Table>

<Br>

........................ Omitted

</Form>

3. Spring: Transform

Corresponding to Org. springframework. web. servlet. tags. transformtag indicates the processing class of the library. This tag is used to convert the attributes in the form that do not correspond to the attributes in bean one by one. It is usually used with <spring: bind>. <Spring: Transform> marked as <spring: bind> provides better support.

The attributes are as follows:

Value: required. It is the same as the Bean class pointed to by the current <spring: bind> flag. Is the name of the object class you want to convert.

VaR: not required. This string is used to bind the output result to page, request, session, or application scope. It is output to JSP by default.

Scope: not required. When the prerequisite var must be set. The value can be page, request, session, or application.

4. Spring: Message corresponds to the processing class of the org. springframework. Web. servlet. tags. messagetag tag library.

This tag is used to help springframework support internationalization. Similar to the FMT: message tag of jstl. Of course, this tag can work well under the Local springframework framework.

The attributes are as follows:

Code: not required. Used to find the message. If it is not used, text will be used.

Text: not required. If the Code does not exist, text output is used by default. If neither Code nor text is set, the tag will be output as null.

VaR: not required. This string is used to bind the output result to page, request, session, or application scope. It is output to JSP by default.

Scope: not required. When the prerequisite var must be set. The value can be page, request, session, or application.

5. Spring: htmlescape

Corresponding to org. springframework. Web. servlet. tags. htmlescapetag tag library processing class

Not commonly used. Skipped here

6. Spring: Theme

Corresponding to the processing class of the org. springframework. Web. servlet. tags. themetag tag Library

Not commonly used. Skipped here

After learning these usage, let's take a look at a specific example.

The configuration steps are as follows:

Directory structure:

-Spirngapp

WEB-INF

-Classes

-Lib

-Src

-War

1. Copy spring. tld to the WEB-INF directory.

2, copy spring. jar to the WEB-INF \ Lib package

Example:

Configure the taglib attribute in Web. xml,CodeAs follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype web-app public '-// Sun Microsystems, Inc. // DTD web application 2.3 // en' http: // java.sun.com/dtd/web-app_2_3.dtd'>

<Web-app>

<Servlet>

<Servlet-Name> springapp </servlet-Name>

<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>

<Load-on-startup> 1 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-Name> springapp </servlet-Name>

<URL-pattern> *. htm </url-pattern>

</Servlet-mapping>

<Welcome-file-List>

<Welcome-File> index. jsp </welcome-File>

</Welcome-file-List>

<Taglib>

<Taglib-Uri>/spring </taglib-Uri>

<Taglib-location>/WEB-INF/spring. TLD </taglib-location>

</Taglib>

</Web-app>

The JSP page code is as follows:

<% @ Include file = "/WEB-INF/JSP/include. jsp" %>

<% @ Taglib prefix = "Spring" uri = "/Spring" %>

<HTML>

<Head>

<Title> <FMT: Message key = "title"/> </title>

</Head>

<Body>

<H1>

<FMT: Message key = "priceincrease. Heading"/>

</H1>

<Form method = "Post">

<Table width = "95%" bgcolor = "f8f8ff" border = "0" cellspacing = "0" cellpadding = "5">

<Tr>

<TD alignment = "right" width = "20%"> increase (%): </TD>

<Spring: bind Path = "priceincrease. Percentage">

& Lt; TD width = "20%" & gt;

<Input type = "text" name = "percentage" value = "<C: Out value =" $ {status. Value} "/>">

</TD>

& Lt; TD width = "60%" & gt;

<Font color = "red">

<C: Out value = "$ {status. errormessage}"/>

</Font>

</TD>

</Spring: bind>

</Tr>

</Table>

<Br>

<Spring: hasbinderrors name = "priceincrease">

<B> Please fix all errors! </B>

</Spring: hasbinderrors> <br>

<Input type = "Submit" alignment = "center" value = "execute">

</Form>

<A href = "<C: URL value =" hello.htm "/>"> Home </a>

</Body>

</Html>

The <spring: bind> flag is often used to bind <input> form elements for submission to priceincrease. java. In fact, priceincrease. Java Contains classes for getter and setter methods. When a user presses the button to submit, the value in the form is actually put into priceincrease. Java by the framework. <Spring: bind> it is often used with form. this $ {status. errormessage} and $ {status. value} is a special variable defined by the framework. They are used to display error messages and data in the current form. So far today, please refer to the document for detailed usage of the Spring framework tag library.

Author's blog: Http://blog.csdn.net/legendinfo/

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.