AngularJS取得後台Jason資料顯示在頁面上

來源:互聯網
上載者:User

標籤:代碼下載   rpo   body   min   data   getc   repeat   ima   expires   

代碼下載:https://files.cnblogs.com/files/xiandedanteng/angularjsAjax.rar

 

前台代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html ng-app="notesApp">  <head>    <base href="<%=basePath%>">        <title>My JSP ‘index.jsp‘ starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->    <script src="angular1.4.6.min.js"></script>  </head>    <body>    <table ng-controller="MainCtrl as ctrl" border="1px">        <tr ng-repeat="member in ctrl.items">            <td><span ng-bind=‘member.id‘/></td>                <td><span ng-bind=‘member.name‘/></td>                       <td><span ng-bind=‘member.age‘/></td>        </tr>    </table>  </body></html><script type="text/javascript"><!--    angular.module(‘notesApp‘,[])     .controller(‘MainCtrl‘,[‘$http‘,function($http){                    var self=this;           self.items=[];        var url="/angularjsAjax/Members";           $http.get(url).then(function(response){               self.items=response.data;            },function(errResponse){               alert(‘error‘+errResponse);                      });            }]);//--></script>

 

後台代碼:

package com.test;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;public class MembersServlet extends HttpServlet {    private static final long serialVersionUID = 56890894234786L;        public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, java.io.IOException {        request.setCharacterEncoding("UTF-8");        PrintWriter out = response.getWriter();                List<Member> ls=new ArrayList<Member>();        ls.add(new Member("001","Andy",20));        ls.add(new Member("201","Bill",40));        ls.add(new Member("501","Cindy",60));        ls.add(new Member("901","Eintein",88));                JSONArray jArray=JSONArray.fromObject(ls);                String json=jArray.toString();                System.out.println("json="+json);        out.print(json);        out.flush();            return;    }            public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, java.io.IOException {        doPost(request, response);    }}

Member類代碼:

package com.test;public class Member{    public Member(String id,String name,int age){        this.id=id;        this.name=name;        this.age=age;                        }        private String id;    private String name;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    private int age;}

效果:

 

AngularJS取得後台Jason資料顯示在頁面上

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.