concept type conversion in category java

appears as: type conversion, type conversion, The type conversion, type conversions
Struts 2 in Action

This is an excerpt from Manning's book Struts 2 in Action.

What kinds of work should be done in interceptors? Logging is a good example. Logging should be done with the invocation of every action, but it probably shouldn’t be put in the action itself. Why? Because it’s not part of the action’s own unit of work. It’s more administrative, overhead if you will. Earlier, we charged a framework with the responsibility of providing built-in functional solutions to common domain tasks such as data validation, type conversion, and file uploads. Struts 2 uses interceptors to do this type of work. While these tasks are important, they’re not specifically related to the action logic of the request. Struts 2 uses interceptors to both separate and reuse these cross-cutting concerns. Interceptors play a huge role in the Struts 2 framework. And while you probably won’t spend a large percentage of your time writing interceptors, most developers will find that many tasks are perfectly solved with custom interceptors. As we said, we’ll devote all of chapter 4 to exploring this core component.

Now that we’ve covered the action and interceptor components, you should have a good idea of how the major chunks of server-side logic execute in the Struts 2 framework. We’ve avoided poking our noses into the details of two of the more important tasks that the framework helps us achieve: data transfer and type conversion. We’ve been able to avoid thinking about these important tasks because the framework automates them so well. This’ll continue to be true for large portions of your development practice. However, if we give a small portion of our energy to learning how the data transfer and type conversion actually works, we can squeeze a whole lot more power out of the framework’s automation of these crucial tasks.

In order to utilize the built-in type conversion, you just need to build an OGNL expression that targets a property on the ValueStack. The OGNL expression will either be the name of your form field, under which the parameter will be submitted in the HTTP request, or it’ll be somewhere in your view-layer tags, such as one of the Struts 2 JSP tags. Again, our current discussion will focus on the data’s entry into the framework as request parameters. Chapter 6 will focus on the tag point of view. However, this is mostly a matter of convenience; OGNL serves the same functional roles at both ends of the request processing—its expression language navigates our object graph to locate the specified property, and the type converters manage the data type translations between the string-based HTTP world and the strictly typed Java world. Data in, data out? It doesn’t matter. The type conversion and OGNL will be the same.