Question

I'm relatively new to Struts2. I've started using ModelDriven to reduce overhead in development. I wanted to write an interface to modify a property before it gets to the action but I don't see how you can access the properties of a class that implements ModelDriven for the Model.

I can see how things like validate() can work as they are in the actual action class. I changed the design to encapsulate the logic behind the service anyways but still would like to know if this is possible.

We're doing everything by ajax/json so I find modeldriven helps quite a lot - not sure if there is a better alternative though!

Edit - code example:

Trying to replace a message with a message in a template to use in an email body.

public class EmailActionImpl implements EmailAction {

private Email email=new Email();    
private EmailService emailService;

public Email getModel(){
    return email;
}
[... getters and setters ...]

    public String execute(){
    logger.info("Email action is sendind an email...");

    try{
        emailService.sendNewMail(email);
    }catch(Exception e){
        logger.error("Email not sent: " + e.getMessage());
        return "failure";
    }   
    return "success";
}
}

Email model something like this

@Entity
@Table(name="email")
public class Email {
private Long id;
private String from;
private String to;
private String message;
private String templateType;
 [...]
 }

I would like an interceptor preprocessor to replace email.message. Should look something like this but action.getMessage/setMessage aren't available.

public class SimpleInterceptor extends AbstractInterceptor {

public String intercept(ActionInvocation invocation) throws Exception {
   EmailAction action = (EmailAction)invocation.getAction();
   action.setMessage(MessageTemplateFactoryImpl(action.getMessage(), action.getTemplateType());
   return invocation.invoke();
}
}
Was it helpful?

Solution

If you still want to implement an interceptor to work on a particular set of models then you will check if the Action implements ModelDriven. Via reflection (or Apache bean utils) you can derive the particular model in question, to determine if your interceptor applies and then act on it accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top