如何在一个jsp中的两个表中插入数据mvc

时间:2017-05-03 12:18:11

标签: java hibernate jsp spring-mvc

我使用Spring mvc并尝试从一个jsp中的两个表中插入数据

我的桌子:

1.Cars
  Id
  Name
  CityId(FK) 

2.City
  Id
  Name

我在下面尝试了但它只在列表中选择,我希望它是输入文本以插入数据

我的豆子:

班级汽车:

@Entity
@Table(name="CARS")
public class Car {
  @Id
  @Column(name="ID", nullable=false, unique=true)
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;

  @ManyToOne
  @JoinColumn(name="CITY_ID")
  private City city;      
  // get and set
}

class City

@Entity
@Table(name="CITY")
public class City {
  @Id
  @Column(name="ID", nullable = false, unique = true)
  @GeneratedValue(strategy=GenerationType.AUTO)

  private int id;
  @Column(name = "NAME")
  private String name;
  // get and set
}

班级管理员:

@RequestMapping(value="/CreateCar", method = RequestMethod.POST)
public ModelAndView setCreateCarPage(HttpServletRequest request, 
                                     HttpServletResponse response,
                                     ModelMap model, @RequestParam("citySelected") Integer citySelected,@ModelAttribute("createCar") Car createCar)    {
  City myCity = cityService.getCityById(citySelected);
  createCar.setCityId(myCity);

  carService.createCar(createCar);
  return new ModelAndView("createCar");
 }

JSP:

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/CreateCar" enctype="multipart/form-data">
    <fieldset style="width:300px">

    <label>Name of Car</label>
        <form:input path="name" id="name"/>

    <label>Name of City</label>
        <select name="citySelected">
            <c:forEach items="${cityList}" var="city">
                <option value="${city.id}">${city.name}</option>
            </c:forEach>
        </select>
  <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" />
    </fieldset>
</form:form>

如何为汽车和城市名称创建/插入数据?

3 个答案:

答案 0 :(得分:0)

您的代码中几乎没有错误。

首先在您的Controller中,您有一个名为setCityId()的方法,但参数为City。此外,您不需要请求/响应。

在您的请求名称中使用大写代替 CreateCar ,使用 createCar

你应该改成这样的东西:

@RequestMapping(value="/createCar", method = RequestMethod.POST)
public ModelAndView setCreateCarPage(@ModelAttribute("createCar") Car createCar)    {
  carService.createCar(createCar);
  return new ModelAndView("createCar");
 }

在您的JSP代码中,您需要将下拉列表的city.id设置为汽车

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/createCar" enctype="multipart/form-data">
    <fieldset style="width:300px">

    <label>Name of Car</label>
        <form:input path="name" id="name"/>

    <label>Name of City</label>
        <select name="citySelected" path="city.id">
            <c:forEach items="${cityList}" var="city">
                <option value="${city.id}">${city.name}</option>
            </c:forEach>
        </select>
  <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" />
    </fieldset>
</form:form>

答案 1 :(得分:0)

您好我理解您使用 name 列的问题,您可以通过添加前缀作为表名来处理此问题,例如:

  

代表车名:name =“Car.name”

     

表示城市名称:name =“Car.City.name”

通过放置此问题,您的问题将得到解决。

答案 2 :(得分:0)

在你的jsp更新你的选择如下:

var input = "Wed%20May%2003%202017%2015:41:49%20GMT%200530%20(IST)";

function formatDate(d) {
    var month = (d.getMonth() + 1)
      , day = '' + d.getDate()
      , year = d.getFullYear()
      ;

    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    
    return [year, month, day].join('-');
}

var decoded = decodeURIComponent(input);
var inputDate = new Date(decoded);

var output = formatDate(inputDate);

console.log(output);

并在您的控制器中更新此方法:

<form:form modelAttribute="createCar" method="POST" commandName="createCar" action="/CreateCar" enctype="multipart/form-data">
    <fieldset style="width:300px">

    <label>Noi dung</label>
        <form:input path="name" id="name"/>   
    <label>Tinh/Thanh pho</label>
      <form:select path="city">
        <form:options items="${cityList}" itemLabel="name" itemValue="id"/>
    </form:select>
  <input class="btn btn-danger" type="submit" value="Create" style="width:100px;" />
     </fieldset>
</form:form>