Java object serialization of Ajax (2)

Source: Internet
Author: User
Tags foreach date object error handling object serialization serialization client
Ajax| Object Fujak Superpix72 Camera 7.2 Megapixel digital camera featuring six shooting and 3x modes optical. Silver. $299.99 The lack of self serialization Interestingly, the code in Listing 3 shows one of the main drawbacks of having JavaBean serialize itself into XML. Suppose you want to use this document to represent the customer's order history view. In this case, it is not likely to display a complete description of each item in each historical order, or to tell the customer his or her own name. However, if your application has a ProductSearch class that returns search results as a list of item beans, it may be helpful to include instructions in the XML representation of the item. Also, extra fields on the Item class that represent the current inventory level may be useful information to display in the product search view. However, regardless of whether the current inventory level is related to the current situation (for example, for the customer's order history), the field is serialized from any object graph that contains the Item. From a design perspective, this is a classic problem of coupling data models with view generation. Each bean can only serialize itself in one way, in the same way that the Ajax interaction ultimately swaps the data they do not need to exchange, making it more difficult for client code to find the information it needs from the document, as well as increasing bandwidth consumption and the client's XML parsing time. Another consequence of this coupling is that the syntax of XML cannot be separated from the Java class. For example, modifying the scenario of a customer document may affect multiple Java classes, causing them to be modified and recompiled. I'll fix these later, but first let's look at a solution to the scalability problem of self serialization: XML binding Framework. XML binding framework in recent years, several Java APIs have been developed to simplify the binding process of XML documents to Java object graphs. Most of them provide XML orchestration and disassembly, that is, they can perform bidirectional conversations between Java object diagrams and XML. These frameworks encapsulate all the work of XML processing, which means that the application code only needs to handle ordinary Java classes. They also want to provide useful accessibility features, such as document validation. In general terms, these frameworks take two different forms: code generation and object to XML mapping. I will explain each of these two ways separately. Code generation uses a code-generated framework that includes XMLBeans, JAXB, Zeus, and JBind. Castor can also use this technique. The starting point for such a framework is an XML schema that describes the document data type. Using the tools provided by the framework, you can generate Java classes that represent these schema-defined types. Finally, you use these generated classes to write applications that represent your own model data, and to serialize the data into XML through some of the supporting mechanisms provided by the framework. If the application wants to use a large XML syntax, code generation is a good way to do it. The scalability of writing custom XML serialization code on dozens of classes is eliminated. On the other hand, you no longer need to define your own JavaBean. The Java classes generated by the framework usually conform very well to the structure of XML, so it is difficult to encode them. Also, the generated classes become dumb data containers because they are generally not added to the behavior. In general, there are compromises to be made in the application code to handle the type of scenario generation well. Another drawback is that if you modify the scenario, the generated classes will also be modified, and therefore the code that surrounds them will be affected accordingly. This type of XML binding framework is most useful when data is disassembled (for example, using XML documents and translating them into Java objects). Unless you have a large data model and are likely to benefit from the generated classes, a code-generated framework can be a great killer for Ajax applications. The mapping method uses the framework of mapping methods including Castor and Apache Commons betwixt. Mapping is usually a more flexible and lightweight solution than code generation. First, you can write JavaBean as you normally do, including any behavior, and any convenient way you like. Then, at run time, you invoke the introspective choreographer in the frame and generate the XML document based on the type, name, and value of the object member. By defining the mapping file for a class, you can override the default binding policy and make recommendations to the choreographer about how the class is represented in XML. This approach is a good compromise between scalability and flexibility. You can write Java classes in the way you like, and the choreographer handles XML. Although mapping definition files are simple to write and scalable enough, mapping rules can only change the standard binding behavior, and there is always a residual coupling between the object structure and their XML representation. Eventually, you might have to make a compromise between the Java presentation or the XML format to make the mapping approach work. Data Binding Summary Dennis Sosnoski writes in-depth articles on the subject of the XML data binding API in two aspects of code generation and code mapping. If you want to study this area further, I recommend his wonderful articles on Castor and code generation frameworks. In short, code generation has lost too much flexibility and convenience, and is not very useful for typical Ajax applications. On the other hand, a mapped framework might work well, but adjust their mapping strategy just right to generate the XML needed from the object. All XML binding APIs have one major shortcoming of manual serialization techniques: The coupling of models and views. is limited to an XML representation of a type, which means that on the network the totalNeed to have redundant data transfer. A more serious problem is that the client code does not get it when the situation requires the client code to use a specialized view, so you may struggle to handle the immutable view of the given object graph. In the traditional WEB application development, the page template system is used to separate the view generation from the controller logic and model data cleanly. This approach can also be helpful in Ajax scenarios. Page template system Any common purpose of page templating technology can be used to generate XML, so that Ajax applications based on their own data model to generate any XML response documents. The bonus is that templates can be written in a simple, expressive markup language, rather than a line of Java code. Listing 5 is a JSP page that employs a customer bean and represents a custom XML view that is suitable for client code generation order history components. Listing 4. JSP <%@ page contenttype= "Application/xml" to generate order history documents%><%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" Prefix= "C"%> ${order.date}
  • Ajax: A new way to build Web apps
  • Discussion on the error handling mechanism of AJAX (2)
  • Discussion on the error handling mechanism of AJAX (1)
  • First experience. NET Ajax Brushless New technology
  • A brief analysis of Ajax development Technology in Rails system (4)


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.