Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

easiest way to confirm password with Spring MVC

I wanna register a user using Spring MVC 3, Hibernate, and PostgresQL. Here Is the form I wanna submit:

<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>

here is the POJO I want to save:

@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

and here is the relevant part of the Controller:

@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;
}

Because I feel it wasteful to store the value of retypePassword in the DB I did not create a column for that. That caused the exception below when I submitted the form:

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

I could easily fix this exception by adding the column "retypePassword" to the table but I still feel that would be wasteful. Is there any clever way to fix the problem without adding duplicate data in the DB?

like image 464
Sanyifejű Avatar asked Sep 19 '25 23:09

Sanyifejű


1 Answers

You can mark retypePassword in entity with @Transient, this will instruct hibernate not to create column in table but still you can use this pojo to bind with spring mvc for validation purpose.

@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
like image 154
Jigar Parekh Avatar answered Sep 21 '25 13:09

Jigar Parekh