码迷,mamicode.com
首页 > Web开发 > 详细

struts2框架学习三 ajax

时间:2020-04-04 15:04:39      阅读:82      评论:0      收藏:0      [点我收藏+]

标签: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+=<tr><td>+data[index].id+</td><td>+data[index].name+</td><td>+data[index].zuoze+</td>+
                           <td>+data[index].jianjie+</td><td><a href="updateAction.action?book.id=+data[index].id+">修改</a>&nbsp<a href="delAction.action?book.id=+data[index].id+">删除</a></td></tr>
                           
                           
                           })
                         
                           $(#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<Book> list=dao.search();
        
        try {
            
            JSONArray array = new JSONArray();
            
            
            if(list.size()>0) {
            for (int i = 0; i < list.size(); 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!