使用Spring MVC确认密码的最简单方法

我想使用Spring MVC 3,Hibernate和Postgresql注册用户.
这是我想提交的表格:

<form:form name="registerForm" method="post"
        action="registerNewUser.html" commandName="user">

        <table>

            <tr>
                <td><spring:message code="label.userName" /></td>
                <td><form:input path="userName" /></td>
                <td><form:errors path="userName" cssClass="error" /></td>
            </tr>

            <tr>
                <td><spring:message code="label.password" /></td>
                <td><form:password path="password" /></td>
                <td><form:errors path="password" cssClass="error" /></td>
            </tr>


            <tr>
                <td><spring:message code="label.password" /></td>
                <td><form:password path="retypePassword" /></td>
                <td><form:errors path="retypePassword" cssClass="error" /></td>
            </tr>

            <tr>
                <td colspan="2"><input type="submit"
                    value="<spring:message code="register.label" />" /></td>
            </tr>
        </table>

    </form:form>

这是我要保存的POJO:

@Entity
@Table(name = "user_table")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "user_name", nullable = false, length = 50)
    private String userName;

    @Column(name = "password", nullable = false, length = 50)
    private String password;

    private String retypePassword;
        // getters setters

以下是财务主任的相关部分:

@RequestMapping(value = "/registerNewUser", method = RequestMethod.POST)
public String saveNewUser(@modelattribute User user, BindingResult result, Model model, HttpSession session) {

    UserValidator validator = new UserValidator();
    validator.validateUser(user, result, userService.existingUser(user));
    String ret = REGISTER_USER;
    if (!result.hasErrors()) {
        user.setPassword(PasswordEncripter.md5(user.getpassword()));
        userService.save(user);
        ret = goToPractice(user, model, session);
    }
    return ret;
}

因为我觉得将retypePassword的值存储在DB中是浪费的,所以我没有为它创建一个列.
当我提交表单时,这导致了以下异常:

org.postgresql.util.PsqlException: ERROR: column user0_.retypepassword does not exist

我可以通过在表中添加“retypePassword”列来轻松修复此异常,但我仍然认为这会浪费.有没有聪明的方法来修复问题而不在数据库添加重复数据?

解决方法:

您可以使用@Transient在实体中标记retypePassword,这将指示hibernate不在表中创建列,但仍然可以使用此pojo与spring mvc绑定以进行验证.

@Entity
@Table(name = "user_table")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "user_name", nullable = false, length = 50)
    private String userName;

    @Column(name = "password", nullable = false, length = 50)
    private String password;

    @Transient
    private String retypePassword;
        // getters setters

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...