Spring Mvc + JQuery发布数据问题

时间:2013-12-11 15:30:57

标签: jquery ajax spring hibernate

这是我的代码片段。但没有响应返回到web页面。我在firebug中使用了调试,在jquery代码部分也没有调用成功/异常。 请帮我解决这个问题。[没有发生任何错误。只是没有回复] 感谢

JSP

    function postMedicines() {
        var selectedMedicines = $('#sortable2').sortable('toArray').toString();
        var patientName=$('#patientName').val().toString();
        $.ajax({
            type : "POST",
            url : "setTabletsNames.htm",
            data : "selectedMedicines=" + selectedMedicines+"&patientName="+patientName,
            success : function(response) {
                alert('response : '+response);
                $('#info').html(response);
            },
            error : function(e) {
                alert('data : '+data);
                alert('Error: ' + e);
            }
        });
    }
</script>
</head>
<body>

<form action="" method="post">
Patient Name : <input type="text" name="patientName" id="patientName">

<br/><br/>
    <div style="font-weight: bold;">&nbsp;&nbsp;Available Medicines</div>
    <ul id="sortable1" class="droptrue">
    </ul>

    <div style="font-weight: bold;">Selected Medicines</div>
    <ul id="sortable2" class="dropfalse">
    </ul>

<button id="postButton" onclick="postMedicines()" >Post Data</button>
<div id="info" style="color: green;"></div>

</form>

控制器

@RequestMapping(value = "/setTabletsNames",method = RequestMethod.POST)
public @ResponseBody String setTabletsNames(@RequestParam("selectedMedicines") String []selectedMedicines,@RequestParam("patientName") String patientName) {
    Integer response=0;
    try {
        response= ePrescriberDAO.getTabletbyNameAndSave(patientName,selectedMedicines);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (response==1){
        return "success";
    }else{
        return "failure";
    }
}

控制器也调用了,但没有响应发送回JSP。在输入 patientName 之前,它的工作正常。有关jquery发送数据部分的任何问题。?

DAO

public Integer getTabletbyNameAndSave(String patientName,String []selectedMedicines) {
    int response=0;
    EPrescriber ePrescriber=new EPrescriber();
    List<SelectedTablets> selectedTabletsList=new ArrayList<SelectedTablets>();
    for (String item : selectedMedicines) {

        Tablets tablets=null;   
        String hql="from Tablets t where t.tradeName='"+item.trim()+"' ";
        Query queryList = sessionFactory.getCurrentSession().createQuery(hql);
        tablets=(Tablets)queryList.uniqueResult();

        SelectedTablets selectedTablets=new SelectedTablets();
        selectedTablets.setTablets(tablets);
        selectedTablets.setePrescriberid(ePrescriber);
        selectedTabletsList.add(selectedTablets);
        sessionFactory.getCurrentSession().save(selectedTablets);

    }

    ePrescriber.setPatientName(patientName);
    ePrescriber.setSelectedTablets(selectedTabletsList);
    sessionFactory.getCurrentSession().save(ePrescriber);
    response=1;
    return response;        
}

Bean classes 01

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

    private static final long serialVersionUID = 4854785134773287611L;

    public SelectedTablets() {
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    @SequenceGenerator(name = "selectedTablets_seq", sequenceName = "selectedTablets_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "selectedTablets_seq")
    private int id;

    @Column private Tablets tablets;

    @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name = "ePrescriberid", nullable = true)
    private EPrescriber ePrescriberid;

        ..getters & setters

Bean class 02

@Entity
@Table(name = "E_PRESCRIBER")
public class EPrescriber implements Serializable {
    private static final long serialVersionUID = 440529869955257543L;
    public EPrescriber() {
        super();
    }

    public EPrescriber(int ePrescriberid, List<SelectedTablets> selectedTablets) {
        this.ePrescriberid = ePrescriberid;
        this.selectedTablets = selectedTablets;
    }

    @Id
    @Column(name = "ePrescriberid"  ,unique = true, nullable = false)
    @SequenceGenerator(name = "ePrescriber_seq", sequenceName = "ePrescriber_id_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ePrescriber_seq")
    private int ePrescriberid;

    @Column(name = "patientName" ) 
    private String patientName;

    @OneToMany(fetch = FetchType.LAZY,targetEntity=SelectedTablets.class, mappedBy = "ePrescriberid")
    private List<SelectedTablets> selectedTablets=new ArrayList<SelectedTablets>();
Patient Name : Mr. JAmes Smith

Tablets :                    |            Selected Tablets

[Paracetomol]                |             {Bico}
[Brufen]                     |             {Brufen}
[Bico]                       | 

这些代码需要拖放相关图标[可供选择]

0 个答案:

没有答案