Struts フレームワークを使用して構築する Web サイトがあります。
以前にデータベースに保存した値に変更が加えられたかどうかを追跡したいと思います。だから私はアクションフォームとエンティティクラスの間で値を比較したい.
アイデアは、このようなものです。
public class CompareValue extends org.apache.struts.action.Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MyActionForm actionFrom = (MyActionForm) form;//form/view which contains new value that are going to be save to the database
EntitiesJpaController entitesController = new EntitiesJpaController();
EntitiesClass entities = entitesController.findEntitiesById(actionFrom.getId());//get data previously save to database
if(entities!=null){
if(!entities.getName().equals(actionForm.getName())){
System.out.println("Update applicant name");
}
if(!entities.getAddress().equals(actionForm.getAddress()){
System.out.println("Update applicant address");
}
if(!entities.getNrIcNo().equals(actionForm.getNrIcNo()){
System.out.println("Update applicant social security no");
}
/*log the changes somewhere*/
EntitiesClass entitiesWithNewValue = new EntitiesClass();
BeanUtil utilBean = new BeanUtil();
utilBean.copyProperties(entitiesWithNewValue, actionForm);//copy new value from actionForm to entitiesClasee
entitesController.edit(entitiesWithNewValue);//save new value to database
}
return mapping.findForward(SUCCESS);
}
}
しかし、エンティティ クラスに 3 つのフィールドしか含まれていない場合は、問題になりません。そのため、エンティティ クラスのすべてのフィールドを反復処理し、その値をアクション フォームの新しい値と比較できるソリューションが必要です。