2013-07-24 31 views
0

我使用Hibernate(多对一)关系:如何显示在春天MVC3下拉:与resonsebody JSON数据

@Entity 
    public class City implements Serializable { 

     private static final long serialVersionUID = 1L; 
     @Id 
     @GeneratedValue 
     private long id; 
     private String description; 
     @ManyToOne 
     @JoinColumn(name="state_id") 
     private State state;... 
    } 

模型的城市:

@Entity 
    public class State {  
     @Id 
     @GeneratedValue 
     private long id; 
     private String description; 
     @OneToMany(mappedBy="state") 

     private List<City> city=new ArrayList<>();... 
    } 
(形式选择路径)为客户

型号:

@Entity 
    public class Customer implements Serializable { 
     /** 
     * 
     */ 
     private static final long serialVersionUID = -5442590361339242648L; 
     @Id 
     @GeneratedValue 
     private long id; 
     @NotEmpty 
     private String firstname; 
     @NotEmpty 
     private String lastname; 
     @NotEmpty 
     private String phone; 
     @NotEmpty 
     private String address; 
     @ManyToOne 
     @JoinColumn(name="city_id") 
     private City city; 
     @ManyToOne 
     @JoinColumn (name="state_id") 
     private State state; 
     @NotEmpty 
     private String zipcode; 
     @NotEmpty 
     @Email 
     private String email; 
     private int status; 
     private Date createdate; 

     @OneToMany(mappedBy = "customer") 
     private List<Account> account = new ArrayList<Account>();... 
    } 

    in my controller i've a method: 

    @RequestMapping(value = "/city", method = RequestMethod.GET) 
     public @ResponseBody 
     List<City> findCities(
       @RequestParam(value = "stateId", required = true) long city, 
       Model model) { 

      List<City> myCity = cityDao.getCityState(city); 

      return myCity; 
     } 

我一个jQuery功能:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#state').change(function() { 
      $.getJSON('${findCitiesURL}',{stateId : $(this).val(),ajax : 'true'}, 
       function(data) { 
        var html = '<option value="">City</option>'; 
         var len = data.length; 
          for (var i = 0; i < len; i++) { 
           html += '<option value="' + data[i].id + '">'+ data[i].description+ '</option>'; 
          } 
          html += '</option>'; 
          $('#city').html(html); 
          }); 
          }); 
        }); 
</script> 

我的问题是如何能在jQuery的替代显示城市状其中的代码的代码:选择路径...

我不能够将数据保存在我的数据库

+0

你的问题还不是很清楚,你的问题是什么:“我无法将数据保存到我的数据库中”或“我怎样才能在jquery中替换显示代码的代码,如表单所示:select path” – Ralph

回答

0

首先要了解的是显示HTML元素应该与Hibernate无关。您不应该将表示(HTML)与持久性(Hibernate)混淆。

要显示的城市下拉列表中,春<form:select>标签可以像这样使用:

<form:select path="city" items="${cities}" itemLabel="description" itemValue="id" /> 

${cities}是指城市对象的集合,你应该通过你的控制器模型属性。 itemLabel和itemValue设置应使用什么标签/值来填充HTML <option>标签

当控制器必须从数据库中获取城市集合时,Hibernate(持久性)才会起作用。

+0

你Raph和gerrytan为你的输入,显示使用jquery工作正常。当我想使用休眠来保存数据时,我遇到了问题。休眠期望一个对象,在我的下拉列表中(选择选项)使用jQuery,当我尝试保存它采取选项值,它不能坚持到数据库。我想要一种方法来保存一个对象,而不是选项值 – sylva

+0

但是你的问题说“如何在spring MVC3中显示一个下拉列表(形式:选择路径)with resonsebody json data” – gerrytan