Spring 3 MVC Form不保存数据库中的数据

时间:2013-05-31 17:16:21

标签: forms spring model-view-controller

我正在尝试运行一个简单的Spring 3 MVC项目来保存表单数据,但是当我提交数据时,页面会转到带有空表单的add.html,并且没有数据保存在Mysql中,也不会显示在页面上。

控制器

package com.app.a;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping(value="/")
public class HomeController {

    @Autowired
    private Personrepo personRepo;

    @RequestMapping(method=RequestMethod.GET)
    public String showForm(ModelMap model){
        List<Person> persons = personRepo.getAll();
        model.addAttribute("persons", persons);
        Person person = new Person();
        person.setId(UUID.randomUUID().toString());

        model.addAttribute("person", person);
        return "home";
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    public ModelAndView add(@ModelAttribute(value="person") Person person,BindingResult result){

        ModelAndView mv = new ModelAndView("home");
        if(!result.hasErrors()) {
            personRepo.add(person);
            person = new Person();
            person.setId(UUID.randomUUID().toString());
            mv.addObject("person", person);
        }
        mv.addObject("persons", personRepo.getAll());
        return mv;
    }

    @RequestMapping(value="/edit", method=RequestMethod.POST)
    public ModelAndView edit(@ModelAttribute(value="person") Person person,BindingResult result) {

        ModelAndView mv = new ModelAndView("home");
        if(!result.hasErrors()) {
            personRepo.edit(person);
            person = new Person();

            mv.addObject("person", person);
        }
        mv.addObject("persons", personRepo.getAll());
        return mv;
    }

    @RequestMapping(value="/delete", method=RequestMethod.POST)
    public ModelAndView update(@ModelAttribute(value="person") Person person,BindingResult result){
        ModelAndView mv = new ModelAndView("home");
        if(!result.hasErrors()) {
            personRepo.delete(person.getId());

            //personRepo.delete(person);
            person = new Person();

            mv.addObject("person", person);
        }
        mv.addObject("persons", personRepo.getAll());
        return mv;
    }
}

主页.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <table align="center">
        <c:forEach items="${persons}" var="person">
            <tr>
                <td>Welcome:</td>
                <td><c:out value="${person.firstName}" /></td>
            </tr>
            <tr>
                <td>Your lastname:</td>
                <td><c:out value="${person.lastName}" /></td>
            </tr>
        </c:forEach>
    </table>

    <form:form modelAttribute="person" method="post" action="add.html">
        <form:hidden path="id" />

        <table>
            <tr>
                <td><form:label path="firstName">First Name:</form:label></td>
                <td><form:input path="firstName" /></td>
            </tr>
            <tr>
                <td><form:label path="lastName">Last Name</form:label></td>
                <td><form:input path="lastName" /></td>
            </tr>
            <tr>
                <td><form:label path="money">Money</form:label></td>
                <td><form:input path="money" /></td>
            </tr>
        </table>
        <input type="submit" value="Save" />
    </form:form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

从它的外观来看,除非您正在进行内容协商,否则您没有将控制器方法映射到URL add.html

比较

<form:form modelAttribute="person" method="post" action="add.html">

@RequestMapping(value="/add", method=RequestMethod.POST)

成功

<form:form modelAttribute="person" method="post" action="add">

你应该没事。