使用Spring MVC中的数据库中的下拉列表从jsp中保存表单

时间:2017-08-13 17:55:55

标签: spring hibernate jsp

我正在尝试在hibernate中保存Account对象。请找到以下代码。

@RequestMapping(value = "/saveAccount", method = RequestMethod.POST)
    public ModelAndView saveAccount(@ModelAttribute("account") Account account,
            BindingResult result) {
        Session session = sessionFactory.openSession();
        System.out.println(account.getFirstName());
        System.out.println(account.getLastName());
        System.out.println(account.getSubject());
        System.out.println(account.getCity().getCityName());
        session.save(account);
        return new ModelAndView("redirect:/form.html");
    }

我的jsp页面有一个包含名字,姓氏,城市和主题字段的表单。 我正在通过基础设施进行城市下降。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib
    prefix="c"
    uri="http://java.sun.com/jsp/jstl/core" 
%>
<!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">
<title>Insert title here</title>
</head>
<body>
<h3>Contact Form</h3>
<div class="container">
  <form  method="post" action="/sdnext/saveAccount.html">
    <label for="fname">First Name</label>
    <input type="text" id="fname" name="firstName"   placeholder="Your name..">
    <label for="lname">Last Name</label>
    <input type="text" id="lname" name="lastName"  placeholder="Your last name..">
    <label for="country">City</label>

<select name="city" id="cityName" >
    <c:forEach var="cityname" items="${lists}">
        <option value="${cityname.cityName}">${cityname.cityName}</option>
    </c:forEach>
</select>

        <label for="subject">Subject</label>
    <textarea id="subject" name="subject"  placeholder="Write something.."  style="height:200px"></textarea>
    <input type="submit" value="Submit">
  </form>       

</div>  
</body>
</html>

DataBase bean类在这里

package test.*;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name= "Account")
public class Account implements Serializable {

    /**
         * 
         */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    private String firstName;

    private String LastName;

    @OneToOne
    private City city;

    private String subject;
}





package test.*;


@Entity
@Table(name= "City")
public class City implements Serializable {

  /**
         * 
         */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private String cityName;

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }
}

我已经使用了一对一映射到city对象。但是我无法在数据库中保存城市对象,所有firstNme,LastName和主题都被保存。在网络控制台中显示,但是数据没有被保存在数据库中。请帮我解决这个问题。谢谢你..

2 个答案:

答案 0 :(得分:1)

通过创建dto,我们可以保留从下拉列表中选择的值,并为我工作。

答案 1 :(得分:0)

<option value="${cityname}">${cityname.cityName}</option>

问题是你发送的cityname.cityName(String)无法映射到City对象,所以, 将完整的城市对象作为值发送。 Hibernate将负责映射。