Spring mvc验证在新表单期间工作但在尝试更新时失败显示验证

时间:2016-07-05 09:10:33

标签: java spring hibernate validation spring-mvc

我有一个输入表单写入add&更新用户信息的目的。 相同的提交表单可以执行添加&正确更新,只是问题加剧,如果我试图故意提交空数据/错误的格式,验证可以显示消息,同时尝试添加新用户,但不是更新用户。 验证包括NotEmpty / Email / typeMismatch等...

允许以相同形式显示验证消息的任何帮助? 为什么尝试添加可以显示,但不能在更新期间添加? 我刚刚开始使用SpringMVC,希望有人帮忙。

提前致谢。

以下是代码:

@ModelAttribute
public void getUserDetail(@RequestParam(value = "udId", required = false)  
    Integer udId,
    Map<String, Object> map) {

    //For update UserDetail form

    if (udId != null) {
        UserDetail userDetail = new UserDetail();
        try {
            userDetail = userDetailService.getUserDetail(udId);
        } catch (Exception e) {
            log.error("failed to get UserDetail with udId: " + udId);
        }
        map.put("userDetail", userDetail);
    }
}


//Add
@RequestMapping(value = "/userDetail", method = RequestMethod.POST)
public String saveUserDetail(@Valid UserDetail userDetail,
    BindingResult bingdingResult) {

    if (bingdingResult.getErrorCount() > 0) {
        log.info("User attempt create user: " + userDetail.toString());
        for (FieldError error : bingdingResult.getFieldErrors()) {
            log.info(error.getField() + ": " + error.getDefaultMessage());
        }
        //redirect to same page to show error return infor (NotEmpty etc...)
        return "/user/inputUserDetail";
    } else {
        try {
            userDetailService.addUserDetail(userDetail);
        } catch (Exception e) {
            log.error("Failed to add new UserDetail: " + 
                     userDetail.toString());
        }
        return "redirect:/user/userDetails";
    }
}

@RequestMapping(value = "/userDetail", method = RequestMethod.PUT)
public String updateUserDetail(@Valid UserDetail userDetail,
    BindingResult bingdingResult, Map<String, Object> map) {

    //Update - need UserDetail model attribute

    if (bingdingResult.getErrorCount() > 0) {
        log.info("User attempt update user: " + userDetail.toString());
        for (FieldError error : bingdingResult.getFieldErrors()) {
            log.info(error.getField() + ": " + error.getDefaultMessage());
        }
        // redirect to same page to show error return infor (NotEmpty etc...)
        // error cannot display error message
        return "/user/inputUserDetail";
    } else {
        try {
            //update user detail
            userDetailService.updateUserDetail(userDetail);
        } catch (Exception e) {
            log.error("Failed to update UserDetail: " +     
                   userDetail.toString());
        }
        return "redirect:/user/userDetails";
    }
}

附加编码:

//Service layer:
@Service("userDetailHistoryService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserDetailHistoryServiceImpl implements UserDetailHistoryService{
    @Override
    public void updateUserDetailHistory(UserDetailHistory userDetailHistory) {
        userDetailHistoryDao.updateUserDetailHistory(userDetailHistory);
    }
    //other coding etc.. etc...
}

@Repository("userDetail")
public class UserDetailDaoImpl implements UserDetailDao {
    @Autowired
    private SessionFactory sessionFactory;

    //Dao layer:
    @Override
    public void updateUserDetail(UserDetail userDetail) {
        sessionFactory.getCurrentSession().update(userDetail);
    }
    //other coding etc.. etc...
}

欢迎任何建议和想法? 再次感谢。

当我尝试向NotEmpty字段提交更新时出现错误,在附加长错误消息:

下面
>Warning:   StandardWrapperValve[dispatcherServlet]: Servlet.service() for servlet dispatcherServlet threw exception
java.lang.StackOverflowError
    at java.util.Collections$UnmodifiableMap.get(Collections.java:1454)
    at com.mysql.jdbc.ConnectionImpl.getEncodingForIndex(ConnectionImpl.java:2780)
    at com.mysql.jdbc.Field.<init>(Field.java:185)
    at com.mysql.jdbc.MysqlIO.unpackField(MysqlIO.java:723)
    at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:418)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:3105)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:2336)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2729)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2503)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1369)
    at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:704)
    at com.mysql.jdbc.SQLError.convertShowWarningsToSQLWarnings(SQLError.java:656)
    at com.mysql.jdbc.StatementImpl.getWarnings(StatementImpl.java:2145)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.getWarnings(NewProxyPreparedStatement.java:1806)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.handleAndClearWarnings(SqlExceptionHelper.java:320)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logAndClearWarnings(SqlExceptionHelper.java:273)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:529)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.release(JdbcCoordinatorImpl.java:421)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:160)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:146)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106)
    at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1012)
    at org.hibernate.proxy.AbstractLazyInitializer.permissiveInitialization(AbstractLazyInitializer.java:212)
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:162)
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:196)
    at com.faddistwardrobe.web.model.UserDetail_$$_jvstb9a_8c.toString(UserDetail_$$_jvstb9a_8c.java)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.BodyMeasure.toString(BodyMeasure.java:220)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at java.util.AbstractCollection.toString(AbstractCollection.java:462)
    at org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:317)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at com.faddistwardrobe.web.model.UserDetail.toString(UserDetail.java:283)
    at sun.reflect.GeneratedMethodAccessor1366.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

等....................................与hibernate相关的长消息...

1 个答案:

答案 0 :(得分:0)

试试这样:

@RequestMapping(value = { "/new" }, method = RequestMethod.GET)
    public String newEmployee(ModelMap model) {
        Employee employee = new Employee();
        model.addAttribute("employee", employee);
        model.addAttribute("edit", false);
        return "registration";
    }

    @RequestMapping(value = { "/new" }, method = RequestMethod.POST)
    public String saveEmployee(@Valid Employee employee, BindingResult result,
            ModelMap model) {

        if (result.hasErrors()) {
            return "registration";
        }                   
        service.saveEmployee(employee);    
        return "success";
    }

@RequestMapping(value = { "/edit-{ssn}-employee" }, method = RequestMethod.GET)
    public String editEmployee(@PathVariable String ssn, ModelMap model) {
        Employee employee = service.findEmployeeBySsn(ssn);
        model.addAttribute("employee", employee);
        model.addAttribute("edit", true);
        return "registration";
    }


    @RequestMapping(value = { "/edit-{ssn}-employee" }, method = RequestMethod.POST)
    public String updateEmployee(@Valid Employee employee, BindingResult result,
            ModelMap model, @PathVariable String ssn) {

        if (result.hasErrors()) {
            return "registration";
        }    

        service.updateEmployee(employee);     
        return "success";
    }