struts2框架学习三 ajax

2021-03-27 10:25

阅读:707

标签:jsp   type   int   分代   sea   url   ESS   exce   json   

ajax

jsp部分代码

$(function() {
            $.ajax({
                   type:POST,
                   url:list1Action.action,
                   
               
                   success:function(data){
                       if(data.length>0){
                           var tr=‘‘;
                           $.each(data,function(index){
                             tr+=

+data[index].id+


+data[index].name+


+data[index].zuoze+

+
                           
+data[index].jianjie+


修改&nbsp删除


                           
                           
                           })
                         
                           $(#tb).append(tr)
                           }
                   },
                   error:function(){
                       alert("错误")
                   }
               });
        }); 

action

public String list1() {//json转换
        ActionContext ac = ActionContext.getContext();
        HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
        HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
    
        List list=dao.search();
        
        try {
            
            JSONArray array = new JSONArray();
            
            
            if(list.size()>0) {
            for (int i = 0; i ) {
                JSONObject object = new JSONObject();
                object.put("name", list.get(i).getName());
                object.put("id", list.get(i).getId());
                object.put("zuoze", list.get(i).getZuoze());
                object.put("jianjie", list.get(i).getJianjie());
                array.add(object);
            }
            }
            System.out.println(array);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(array.toString());
            

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

 

struts2框架学习三 ajax

标签:jsp   type   int   分代   sea   url   ESS   exce   json   

原文地址:https://www.cnblogs.com/zlj843767688/p/12631576.html


评论


亲,登录后才可以留言!