Spring 3 MVC 表单未将数据保存在数据库中 [英] Spring 3 MVC Form not saving data in database

查看:42
本文介绍了Spring 3 MVC 表单未将数据保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

控制器

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>

推荐答案

从表面上看,除非你在做内容协商,否则你没有一个控制器方法映射到 URL add.html.

From the looks of it, unless you are doing content negotiation, you don't have a controller method mapped to the 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">

你应该没事.

这篇关于Spring 3 MVC 表单未将数据保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆