I have two Struts2 actions. I want to use a public method of a action in another action. Is it Ok that I can call an action through new operator and call its public method? (For the sake of reusing some common functionality)
public class OneAction extends ActionSupport{
public void doSomeThing(){
// some code
}
public String execute(){
// some code
}
}
public class AnotherAction extends ActionSupport{
public String execute(){
/* is it ok?*/
new OneAction().doSomeThing();
}
}
(shudder) I would not. I’d refactor the common functionality into a separate class that both can call. But that class would be a POJO, not an Action.
I’d be careful about embedding functionality in Actions. I think they’re firmly in the web tier camp. You should do little more than validate and bind parameters into objects, hand them off to POJOs for processing, and package results for return.