This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm new to struts framework. I'm using struts 1.2.9.
In My application I have many controllers . When the user is logged in I'm setting an session attribute like session.setAttribute("user","true"). And when User logged out I'm removing the attribute from the session.
Here My Problem is.. I want to check the user existence in session for every action. I can check the user in every action writing the code in every method in every Controller, but there are so many Controllers and so many methods. Its difficult to check in every action. Is there any way to check the user in every action before the action is executed and forward the page to some other page. So I need some guidance to check the user in a common class before the action is executed.
One way to achieve this is through inheritance and overriding. Every struts controller uses the execute() method to perform its duties. You can subclass struts Action, placing all common behavior in the execute() method and having all of your controllers subclass from this one class. The only thing about this is that each controller has its own mapping.findForward() declared in the execute() method - to make this flexible enough for all actions to use we must create an abstract method that will be used in all your controllers.... therefore, your code would have to have all controllers extend the one common class, and change the execute() method to call the abstract method.
It can look something like this:
The BaseAction class is the class all your controllers will extend:
Here is an example of one of your existing controller classes:
It will require your code to change, but once implemented you can place any common code for all controller actions in the BaseAction class without anything changing.
Things to note:
1. This will work on an Action level. Meaning that if the user should be logged in to perform the called action, it will test it.
1. ALL your current controller classes will have to extend BaseAction instead of Action.
2. ALL your current controller classes will have to change the execute() method name to performExecute()
3. If there is a controller that does not require a person to be logged you must override the isLoginRequired() method.