当你有1000甚至更多字段要更新数据库,怎么办?看我的

来源:互联网 发布:windows insider没有了 编辑:程序博客网 时间:2024/04/29 22:32

试想如果我们的一个对象有1000个参数而我们要修改其中的500个参数,那么我们该怎么做呢,我们要么在页面中设置500个隐藏域要么在service层中用上面的方法重新设置500遍?是不是很烦!别烦。。。

我们还可以这样,小冬子现提供一下思路供参考,大家有新的方法可以探讨。谢谢!

我们可以用interceptor来实现,在Action中,我们一ModerDriven的方式来接受参数,因为我们要修改对象的500个属性,所以在Aciton中接受到的参数就有500个那么这个时候这个对象的另外500个属性的值就为空了,因为在得到这个对象的时候是用getModel方法得到的,所以我们可以在页面的500个参数传递过来之前,把这个对象先查询出来然后页面的参数传递过来之后会覆盖掉对象里原来的值而剩下的500个属性的值就保存在对象中了,这个时候在进行更新就可以了。是不是很棒!

使用struts2拦截器轻松更新对象多个属性,方法如下:

1、定义action

package net.rytong.action;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import net.rytong.entity.User;import net.rytong.service.IUserService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;@Component/** * zhou_dong * 更新用户的action */public class UserUpdateAction extends ActionSupport implementsModelDriven<User> {private static final long serialVersionUID = 1L;@Autowiredprivate IUserService userService;private User user;//先从数据库查出对象public void preUpdate(Integer id){user  =  userService.getById(id);}//model模型底层更改userpublic User getModel() {return user;}//更新到数据库public String update(){userService.update(user);return "success";}}

2、定义拦截器

package net.rytong.interceptor;import net.rytong.action.UserUpdateAction;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;/** *  * @author zhou_dong *自己定义的拦截器,在model模型拦截器前配置 */public class MyInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L;@Overridepublic String intercept(ActionInvocation invocation) throws Exception {//拦截更新方法if("update".equalsIgnoreCase(invocation.getProxy().getMethod())){//拦截更新用户的actionif(invocation.getAction() instanceof UserUpdateAction){UserUpdateAction action = (UserUpdateAction) invocation.getAction();String[] ids = (String[]) invocation.getInvocationContext().getParameters().get("id");action.preUpdate(Integer.parseInt(ids[0]));}}return invocation.invoke();}}

3、struts.xml文件中的配置

<package name="mytest" extends="struts-default" namespace="/">    <interceptors>        <!-- 定义拦截器 -->        <interceptor name="myinterceptor" class="net.rytong.interceptor.MyInterceptor" />        <!-- 定义拦截器堆栈 -->        <interceptor-stack name="myStack">                <interceptor-ref name="exception"/>                <interceptor-ref name="alias"/>                <interceptor-ref name="servletConfig"/>                <interceptor-ref name="i18n"/>                <interceptor-ref name="prepare"/>                <interceptor-ref name="chain"/>                <interceptor-ref name="debugging"/>                <interceptor-ref name="scopedModelDriven"/><!--因为页面提交过来的参数是在modelDriven拦截器中进行赋值操作的,所以我们的拦截器必须定义在它的前面否则我们把要修改的值给覆盖掉了 -->                <interceptor-ref name="myinterceptor"/>                <interceptor-ref name="modelDriven"/>                <interceptor-ref name="fileUpload"/>                <interceptor-ref name="checkbox"/>                <interceptor-ref name="multiselect"/>                <interceptor-ref name="staticParams"/>                <interceptor-ref name="actionMappingParams"/>                <interceptor-ref name="params">                  <param name="excludeParams">dojo\..*,^struts\..*</param>                </interceptor-ref>                <interceptor-ref name="conversionError"/>                <interceptor-ref name="validation">                    <param name="excludeMethods">input,back,cancel,browse</param>                </interceptor-ref>                <interceptor-ref name="workflow">                    <param name="excludeMethods">input,back,cancel,browse</param>                </interceptor-ref>            </interceptor-stack>            </interceptors>    <!-- 使用拦截器 -->    <default-interceptor-ref name="myStack"/>

喜欢了就赞一个!谢谢!



0 0