What is the marriage between strutscx and XSLT?

Source: Internet
Author: User

Outline:

==============

I. a brief review of struts

Ii. Limitations of struts and JSP

Iii. Advantages of XSLT

Iv. strutscx: struts with XSLT

V. strutscxservlet conversion Principle

6. Construct XML output documents

VII. Conclusion

Strutscx is a plug-in of the Struts framework.Source codeOpen products are rooted in pure XML and XSLT technologies and support multiple languages. With strutscx, we can easily use standard XML and XSL technologies to generate multiple output formats such as HTML, XML, and PDF. Struts is an ideal support technology for executing such XSLT transformations.

Strutscx allows us to save and output content in different languages and encoding methods, with strutscx, you can easily process Chinese, English, German, French, Spanish, Korean, Arabic, Russian, and other languages.

I. a brief review of struts

Struts encourages the construction of an application system based on model 2. Model 2 is a variant of the basic MVC design pattern. The advantage of the MVC design pattern is that it makes the model, view and controller parts relatively independent. Struts assembles actionservlet, actionmapping, and various action classes in a central position to process all incoming httpservletrequest. Actionservlet distributes each incoming httpservletrequest to the action class, And the actionmapping object can be controlled through the Struts-config.xml configuration file, which tells actionservlet how to distribute requests.

Figure 1: Communication Process between each part of struts

Figure 1 illustrates the functions of controller: it plays a role of coordination and arbitration between the client, view, and model. Only the controller is connected to the model and view. In the middle, it is like a switch, and the model and view do not communicate directly. In this way, the model, view, and controller are separated. This is the key to successful use of struts.

Note: the logic of All controllers should be placed in the action class. Communication with other layers of the application can be conducted here. Although we can put the business logic into the controller, it is generally better not to do so. You should use the components in the model section, because these components are the places where data is actually processed. Saving data to a database should also be one of the functions of the model.

When the controller processes an httpservletrequest, it forwards the request to the view. The only task of view is to present data. In struts, view generally uses JSP technology.

In struts, all communication between the Controller and view is indirectly completed through httpservletrequest, httpsession, and servletcontext. For these operations, the preferred technology is servlet, struts directly transmits httpservletrequest to servlet -- or JSP, but the servlet container automatically converts JSP to servlet.

Ii. Limitations of struts and JSP

As an open framework, Struts allows you to use technology other than JSP to construct a view. When considering improving the view technology in struts applications, we often think of replacing JSP with other servlet technologies, such as XSLT or XSLT managed by servlet.

Figure 2 describes the application of JSP in the Struts framework. The view is constructed by the JSP custom tag Library and the JSP page. The actionform class is a valueobject object similar to JavaBean, with the set and get methods to save the customer's status. According to the MVC concept of struts, actionform is located in the middle zone between view and controller. Struts provides a complete set of dedicated tags for accessing data in actionform from JSP.

Figure 2: struts Application System

JSP effectively promotes Java applications in Web development. With the help of JSP tag library technology, JSP provides a rich and powerful tool for presentation layer development in Web and J2EE applications. However, JSP also has some limitations:

·ProgramMembers can put the application logic into JSP. However, according to the MVC design philosophy, the only purpose of JSP is to display data. Otherwise, the program structure will become complicated and difficult to manage.

· JSP does not support XML well. JSP cannot ensure that the output XML content is 100% "well-formed" (well-formed ). In this era of numerous Internet access devices that support XML, the lack of XML support is undoubtedly not a small problem.

· Every modification to JSP during the development process will cause the servlet container to convert the JSP into a servlet again. For some servlet engines, the latency caused by the development process brings a lot of trouble.

Iii. Advantages of XSLT

As a W3C standard, XSLT provides a flexible and powerful tool for converting XML data into plain text, PDF, HTML/XHTML, WML, VoiceXML, and other arbitrary XML formats. XSLT processors, such as xalan and Saxon, execute transformations through the XSL style. The XSL style itself is actually an XML document (see figure 3 ).

The XSL style defines the rules for transforming XML data. The XSLT processor implements conversion according to these rules. In the XSL style, we can use the XPath expression to select a part of the XML document in a compact and efficient form for processing.

Figure 3: XSLT conversion Principle

The basic concept of XSLT/XPath is to separate XML data content and its representation, so that the same data can be conveniently output in different forms (or formats, it meets the needs of various display devices. That is to say, without changing the input XML document, you only need to define the corresponding XSL style for each different output format. For struts applications, this means:

· Store (description) All data in a single XML document.

· With the help of the XSL style, convert the XML document to any data format.

Of course, XML documents can be generated in the memory and do not have to have files stored on the disk. For example, you can create an XML document in the form of a Java object and then process it directly with An XSLT processor, which greatly improves the processing speed.

Iv. strutscx: struts with XSLT

Strutscx combines the advantages of struts and XSLT. It uses XSLT to replace the JSP that generates the view, but retains the controller and model of struts intact.

Figure 4 compares the differences between struts and strutscx. In the view, strutscx no longer transmits JSP, but a servlet. The servlet organizes and constructs XML documents and converts XML documents using the XSL style. If there is an actionform bean, the action object copies it to httpservletrequest and is added to the XML output document. Similar to struts, The strutscx action function coordinates the presentation layer and other layers of Web applications or J2EE applications, such as the business layer.

Figure 4: strutscx Application System

The differences between strutscx and struts are shown in the following aspects:

· In view, strutscx uses XSLT instead of JSP.

· The Internationalization of applications is not implemented by Java resource bundle technology, but by pure XML technology.

· Strutscx does not need to use any tag library to output error information. It uses W3C standard technologies such as XML and XSLT/XPath.

Although strutscx is different from struts, we can use strutscx in combination with the existing struts application. In fact, strutscx only abandons some standard STRUTS technology, replacing it with XML, XSLT, and XPath.

V. strutscxservlet conversion Principle

In strutscx, XSLT conversion is completed by strutscxservlet and several of its auxiliary classes, including strutscxdocumentbuilder and strutscxtransformer. Strutscxservlet controls the conversion as follows:

· With the help of strutscxdocumentbuilder, construct an XML document.

· Pass this document to strutscxtransformer and execute the conversion.

Strutscxdocumentbuilder obtains the information required to create an XML output document through httpservletrequest, httpsession, and servletcontext. All these work is done through a unique public method createdocument. The struts action checks the required information in httpservletrequest, httpsession, or servletcontext. The best place to store this information is where it depends. The XML output file created by strutscxdocumentbuilder contains the following information:

· Integration tier data: may include database data, data from ejbs, or data directly from other XML files.

· An (optional) actionform bean that stores the client status information.

· An (optional) actionerrors object that contains the struts error message.

· Resource attribute of strutscx, which is used in strutscx to replace Java resource bundle information.

· Httpservletrequest data.

· Httpsession data.

6. Construct XML output documents

In strutscx, the dataset value can be provided to strutscxservlet as an XML object or JavaBean. If you select the latter, you need to add one or more Javabean to an arraylist object, then it is passed to strutscxdocumentbuilder through httpservletrequest. Strutscxdocumentbuilder uses its auxiliary class strutscxbeantoelement to automatically create XML elements from the data of the JavaBean, and then add the XML elements to the XML output document.

The strutscxbeantoelement class uses model 2X as the prototype. Model 2x is a project similar to strutscx, developed by Julien mercay and Gilbert bouzeid. It analyzes JavaBean by using Java reflection API and Java beaninfo interface, generate XML elements from the set and get methods of the JavaBean.

The strutscx resource attribute is also an XML file, which is equivalent to the struts application attribute file. To enable the application to support multiple languages, each language must have the corresponding strutscx resource attribute file. The strutscx resource attribute file should contain all the international text strings required by the application. Unlike actionform and actionerrors, The strutscx resource attribute file should not be passed to strutscxservlet through httpservletrequest. Instead, it should call servletcontext once when the servlet container starts, that is, it should be done immediately after an action class is called for the first time, to ensure better performance.

The actual XSL conversion process is completed by the strutscxtransformer class. The strutscxtransformer class encapsulates the entire conversion process using JAXP. JAXP is a Java API used to parse XML-a cool API that abstracts the XSLT processor used at the underlying layer. Of course, to perform the conversion smoothly, we must provide an appropriate XSL style, which is also selected by the action class and added to httpservletrequest. The strutscxtransformer will read and parse the XSL style only when the XSL style is changed after the last read. Strutscxtransformer needs to use a helper class strutscxuriresolver. The role of strutscxuriresolver is to automatically convert the relative URI to an absolute Uri.

Strutscxtransformer also provides several useful gadgets. For example, if you want to pass the XML output document directly to httpservletresponse, you only need to add the "debugxml = true" parameter to httpservletrequest. When developing the XSL style, this feature brings a lot of convenience. We can use IE or Netscape/Mozilla to directly view the results.

Another example is to reduce the burden on the Web server and execute XSLT conversion on the client. We can set another parameter for httpservletrequest: pai_clientside = true. Strutscxdocumentbuilder adds a Processing Instruction to the XML output document and specifies the location of the XSL style file. This document is directly sent to the client browser and does not require server-side conversion.

As mentioned above, strutscx abandoned the Java resource binding technology and switched to strutscx resource attributes (strutscx-resource-properties). This is a simple XML file used to internationalize text strings of Web applications. Similar to struts, strutscx can select an appropriate file by checking the java. Lang. Local object at runtime. Using strutscx has many advantages:

Compared with the Java resource binding technology, the strutscx resource attribute allows you to verify the consistency of attribute files through the DTD or XML mode.

In XML, the content is always organized in a hierarchical manner. For example:


     
      
<Variables lang = "de"> <strings>... <day> tag </day>... </strings>... </variables>

     

In the XSL style, we can easily use W3C standard XSLT/XPath expressions (instead of JSP tags) to access the values defined in the previous example:


     
      
<XSL: value-of select = "// variables/strings/day"/>

     

Parse XML uses UTF-8 as the standard encoding mode to support access to all language characters. The strutscx resource attribute file makes full use of this advantage.

The █ strutscx resource attribute file can be placed outside the classpath. Just like for other files that do not want to be accessed directly through HTTP, strutscx resource properties files are best placed in a directory under the/WEB-INF folder.

Idea allows you to easily edit XML files in various languages using the popular XML editor. In contrast, similar support tools are lacking for binding files to Java resources.

In strutscx, all configuration files are in XML format, unlike Java attribute files and XML Attribute files in struts. Note: The strutscx resource attribute should be read to servletcontext as an org. JDOM. Document Object when an action class is called for the first time. Strutscx learns from a dedicated configuration file strutscx-config.xml where to find the strutscx resource property file, so initialization parameters used to read resource binding information in Web. XML will be dropped.

VII. Conclusion

To sum up, using XSLT in struts has many advantages:

· The web application avoids the use of JSP and tag libraries. Since a large number of declarations can be omitted, Web. XML becomes more compact and flexible.

· Use standard technologies such as XSLT and XPath.

· You cannot add the business logic to the XSL style. One of the goals of XSLT technology is to separate data, application logic, and presentation logic.

· Using XSLT, you can easily obtain various output formats: plain text, HTML, and other XML formats. It is no longer HTML-centric like JSP.

· XSLT ensures that the output content is 100% "well-formatted" XML.

Therefore, it is worthwhile to try strutscx. You can download strutscxfrom http://sourceforge.net/projects/strutscx. The downloaded package contains a complete application instance.

This article translated from: http://www.devx.com/Java/Article/11381

Original article: generate Web output in multiple formats and ages with strutscx.

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.