Struts2の新機能です。 私は開発のオーバーヘッドを減らすためにModelDrivenの使用を開始しました。 プロパティにアクションが入る前にプロパティを変更するインターフェイスを作成したかったのですが、モデルに対してModelDrivenを実装するクラスのプロパティにアクセスする方法がわかりません。Struts2 - ModelDrivenの実装 - モデルプロパティにアクセスするインターセプタを作成します。
validate()のようなものが、実際のアクションクラスのように動作することがわかります。 サービスの背後にあるロジックをカプセル化するように設計を変更しましたが、これが可能かどうかを知りたいと思っています。
私はajax/jsonですべてをやっているので、modeldrivenはかなり助けてくれます。
編集 - コード例:電子メールの本文に使用するテンプレートにメッセージとメッセージを交換しようと
。この
@Entity
@Table(name="email")
public class Email {
private Long id;
private String from;
private String to;
private String message;
private String templateType;
[...]
}
よう
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.messageを交換するインターセプタプリプロセッサをしたいと思います。 このようなものですが、action.getMessage/setMessageは使用できません。
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();
}
}
フォームから設定されているプロパティを変更したい場合は、 –
あなたは正確に何をしようとしていますか?言いにくいことを知らずに。リクエスト内の値を変更することはできますが、通常は独自の型変換プログラムを実装する方が理にかなっています。これらの両方とも、多くのケースをカバーし、アクションを検査したり、独自のインターセプタで直接値を設定することは避けてください。 – Quaternion
まもなくコード例を掲載します。 – JasonG