The converter in Struts 2.0 (Converter)

Source: Internet
Author: User
Tags dateformat html form i18n

In my past Struts 1.x project experience, there is a question that arises occasionally-when creating formbean, what should be a string or other type for a property?

Unlike developing a traditional desktop application, Web applications are actually distributing a different host (and, of course, the same host, but rarer) on the two processes that cross each other. This reciprocal intersection is based on HTTP, and they are all strings. In other words, the data that the server can receive from the user can only be a string or a character array. In objects on the server, these data are often of different types, such as date, integer (int), floating-point number (float) or custom type (UDT), and so on, as shown in Figure 1. Therefore, we need the server side to convert the string to the appropriate type.

Figure 1 UI and server object relationships

The same problem occurs when using the UI to present server data. The HTML form control differs from the desktop application in that it can represent an object, whose value can only be a string type, so we need to convert a particular object to a string in some way.

To achieve these transformations, there is a magician in Struts 2.0 who can help you--converter. With it, you don't have to repeat this code over and over again:

Date birthday = dateformat.getinstance (dateformat.short). Parse (strdate);

<input type= "text" value= "<%= dateformat.getinstance (dateformat.short). Format (birthday)%>"/>

OK, now let's take a look at an example.

Converters--hello World

At the end of my last article, "Internationalizing in Struts 2.0 (i18n) Your application," I gave an example that allows users to easily switch languages, and the following examples are similar but different implementations.

First, create and configure a default resource file, as in the first example of internationalizing (i18n) your application in Struts 2.0;

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.