Spring 3 MVC Form not saving data in database

允我心安 提交于 2020-12-13 18:23:52

问题


I am trying to run a simple Spring 3 MVC project to save form data but when I submit data the page goes to add.html with empty forms and no data is save in Mysql neither it is shown on the page.

Controller

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

Home .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:


From the looks of it, unless you are doing content negotiation, you don't have a controller method mapped to the URL add.html.

Compare:

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

and

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

Make it

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

and you should be fine.



来源:https://stackoverflow.com/questions/16862551/spring-3-mvc-form-not-saving-data-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!