[Logo] Mentawai In Action
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Moderation Log] Moderation Log   [Register] Register / 
[Login] Login 
Bypassing the action  XML
Forum Index » Hello Mentawai
Author Message
saoj



Joined: 22/12/2007 07:20:02
Messages: 22
Offline

In this chapter I want to give you a taste for POJO actions. In a web application, actions are meant to process the HTTP request and to invoke the business model of your application. A common misconception is to assume that your action is implementing your business model. Although this is possible, it is not recommended for serious applications for the reason that it will couple (tie) your business model to the framework being used, making testing and re-use in another application or environment more difficult. It is considered a good practice to keep your business model as decoupled as possible from the framework being used as well as from the rest of your application.

The common scenario is to have your actions performing a link, a glue or a bridge between the HTTP web request and the application business model. However, in some cases but not in all cases, the role of the action becomes predictable as it is just passing data back and forth to the business model. That's the reason, in my opinion, that frameworks like JSF want to make web development become desktop development by eliminating the need for actions and making use of event-based components instead.

Although Mentawai is an action-based framework (in contrast of a component-based or event-based framework), it allows the use of an action to be bypassed when desired. In our previous example we have coded the action HelloMenta.java and, because it was a simple example, we did not code any business logic outside the action. A more desirable approach would have been to have a separate class implementing the business model. I like to call it HelloService, but you can call it HelloFacade or HelloLogic or anything else if you prefer.

The code for our examples/helloworld/service/HelloService.java can look like this:



Note that it is not coupled to any Mentawai class or annotation. It is a simple POJO (Plain Old Java Object) that implements the logic of our application.

Now let's modify our action to make use of the HelloService class (our business model):



Notice how our action now creates an instance of HelloService to perform its duties. You can now say that the action is using the business model and not implementing it.

If you stop for some time to contemplate our code (yes, source code is a form of art!) you may come to the conclusion that the action here is not really necessary. It is just passing data back and forth to the business model. Let's try to eliminate our action and use the HelloService as a Mentawai Pojo Action. In the application manager, let's specify that our action will be the HelloService.class and not the HelloMenta.class anymore.



Note that we are specifying the action name as "/HelloMenta" because if we don't specify anything Mentawai will assume by convention that the name is the same as the action class, in this case "/HelloService". We want to keep the same action name so that we don't need to change it inside our JSP page (HTML form).

You don't need to, but you can now delete the file HelloMenta.java. We are bypassing the action and using our HelloService.class as our POJO action! Try to execute the web application with the latest changes and you will get the following exception when submitting the HTML form:


What happened here? Clearly the username was null when the method hello() from HelloService was called. But why? Probably because nobody called the method setUsername to set this variable before the method hello() was called. Remember that this was a duty of our action HelloMenta, which is not in the picture anymore. But don't dispair, Mentawai can easily solve this injection problem for you through its InjectionFilter. We will come back to the topic Filters later on, but for now it should be enough for you to know that a filter gets executed before the action so that you can perform any task before the action gets executed. Let's add a global filter that is applied to all actions in our application manager:



We have added two global filters. They are global because they are not associated with any action in particular, so they will be applied to all actions defined in the application manager. The job of the InjectionFilter is to inject all values from the action input inside the action (or POJO action). That's exactly what we want, in other words, we want the username value inside the action input to be injected inside the HelloService POJO action. The job of the OutjectionFilter is to take (or outject) all properties from the HelloService POJO action and to place them in the action output. That's exactly what we want, in other words, we want to take the "time" and "username" properties of HelloService and place them in the action output so that the tag mtw:out can display them in the JSP.

Redeploy your web application with the latest changes and double check that everything is working! Great! We have just used a POJO action with Mentawai! Before we conclude this chapter, let's make some improvements to the HelloService class.

You should know that the InjectionFilter is capable of injecting a value straight into a public or private instance variable, in other words, it does not need the setter method (setUsername) to perform the injection. So let's get rid of that method.



Another possibility is that you want to pass the action input values as parameters of the HelloService method hello(). Let's give it a try:



We will come back to all these neat features in later chapters, but for now you should realize that Mentawai offers you a great deal of flexibility to do things the way you want. You can download the complete war file for our final HelloWorld application by clicking here.
 
Forum Index » Hello Mentawai
Go to:   
Powered by JForum 2.1.8 © JForum Team