SSH中初次使用Ajax

前端之家收集整理的这篇文章主要介绍了SSH中初次使用Ajax前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 前台JS

java
    $(document).ready(function(){ 
                var value = $('#addPatient_room_room_number').val(); 
                changeBedNumber(value);
        });

    function changeBedNumber(value){
                var obj = document.getElementById("addPatient_patient_bed_num");
                $.ajax({
                        url:"getBedNumber.action",type:"POST",data:"room_number=" + value,success:function(data){
                                var opt = document.createElement ("option");
                                for(var i = 0; i < data.length; i++){
                                        var ch = data.substring(i,i+1);
                                        obj.options.add(new Option(ch,ch));
                                }
                        }
                });
        }

2. Action后台

- xml配置

java
    <action name="getBedNumber" class="com.action.PatientInfo" method="getBedNumber"></action>

- 处理代码

java
    public void getBedNumber() throws Exception{
        //获取response对象
        ActionContext cxt = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse)cxt.get(ServletActionContext.HTTP_RESPONSE);

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        List<?> Roomlist = hibernateTemplate.find("from Room room where room.room_number=?",room_number);
        Room room = (Room)Roomlist.get(0);
        //数据返回给Ajax
        response.getWriter().print(room.getBed_number());
  }
原文链接:https://www.f2er.com/ajax/163279.html

猜你在找的Ajax相关文章