无法使用Spring MVC,Hibernate和MySQL将新记录添加到数据库中

时间:2015-11-16 06:21:31

标签: java mysql spring hibernate jsp

我目前正在使用Spring MVC和Hibernate将用户添加到我的数据库MySQL中。当我点击注册按钮时,我会得到一个 HTTP状态400 - 客户端发送的请求在语法上不正确。

registerform.jsp

var data = (from pro in context.Products
                    where pro.ProductTypeId == productTypeId
                    select new
                    {
                        Pic = pro.Photo,
                        Name = pro.Name,
                        Price = pro.Price

                    }).OrderBy(x => x.Price).ToList();

UserController.java

    <form:form commandName="form" action="${pageContext.request.contextPath}/register.jsp" method="POST">
                <div class="ribbon">
                    <h3>Login Information</h3>
                    <br />
                    <table align="center">
                        <tr>
                            <td><label id="userEmail">Email Address:</label></td>
                            <td><form:input path="userEmail" /></td>
                        </tr>
                        <tr>
                            <td><label id="userPassword">Password:</label></td>
                            <td><form:input path="userPassword" /></td>
                        </tr>
                    </table>
                </div>

                <div class="ribbon">
                    <h3>Personal Information</h3>
                    <br />
                    <table align="center">
                        <tr>
                            <td><label id="userName">Full Name:</label></td>
                            <td><form:input path="userName" /></td>
                        </tr>
                        <tr>
                            <td><label id="userGender">Gender:</label></td>
                            <td><form:input path="userGender" /></td>
                        </tr>
                        <tr>
                            <td><label id="userDOB">Date of Birth:</label></td>
                            <td><form:input path="userDOB" /></td>
                        </tr>
                        <tr>
                            <td><label id="userContact">Contact:</label></td>
                            <td><form:input path="userContact" /></td>
                        </tr>
                    </table>
                </div>

                <div class="ribbon">
                    <h3>Address Information</h3>
                    <br />
                    <table align="center">
                        <tr>
                            <td><label id="userAddress">Address:</label></td>
                            <td><form:input path="userAddress" /></td>
                        </tr>
                        <tr>
                            <td colspan="2"><input type="submit" value="Register" />
                                <button type="button" name="back"
                                    onclick="window.location='index.jsp'">back</button></td>
                        </tr>
                    </table>
                </div>
</form:form>

UserTO.java

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/register")
    public ModelAndView addUser() {

        // direct to registerform page
        ModelAndView model = new ModelAndView("registerform");

        // add form as a new userTO
        model.addObject("form", new UserTO());
        return model;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ModelAndView addUser(@ModelAttribute UserTO userTO) {

        ModelAndView model = new ModelAndView("registerform");
        userService.registerUser(userTO);

        String message = "User registration successful!";
        model.addObject("message", message);
        return model;

    }

}

0 个答案:

没有答案