0

我有一个使用 struts 框架构建的网站。

我想跟踪以前保存在数据库中的值是否有任何更改。所以我想比较 actionForm 和实体类之间的值。

这个想法是这样的,

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 个字段,那么对我来说不是问题。所以我想要解决方案,我可以迭代到实体类中的每个字段并将值与来自 actionform 的新值进行比较。

4

0 回答 0