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;