Angularjs Learning Access Server (5)

Source: Internet
Author: User

(1) Backstage Angularcontroller.java code

 PackageCom.amy.controller;Importjava.util.ArrayList;Importjava.util.List;ImportNet.sf.json.JSONArray;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;Importorg.springframework.web.bind.annotation.ResponseBody; @Controller Public classAngularcontroller {/*** Get all customer information * *@return     */@RequestMapping (Value= "/angular/postcustomerslist", method = Requestmethod.post, produces = "Text/html;charset=utf-8") @ResponseBody Publicstring Postcustomerslist (String userName) {System.out.println (userName); List<String> customers =NewArraylist<>(); Customers.add ("POST"); Customers.add ("Zhang San"); Customers.add ("John Doe"); Customers.add ("Lao Wang"); Customers.add ("Lao Zhang"); Customers.add ("Amy"); Jsonarray Array=Jsonarray.fromobject (Customers);        System.out.println (Array.tostring ()); returnarray.tostring (); } @RequestMapping (Value= "/angular/getcustomerslist", method = Requestmethod.get, produces = "Text/html;charset=utf-8") @ResponseBody Publicstring GetCustomersList (String userName) {System.out.println (userName); List<String> customers =NewArraylist<>(); Customers.add ("Get"); Customers.add ("Zhang San"); Customers.add ("John Doe"); Customers.add ("Lao Wang"); Customers.add ("Lao Zhang"); Customers.add ("Amy"); Jsonarray Array=Jsonarray.fromobject (Customers);        System.out.println (Array.tostring ()); returnarray.tostring (); } @RequestMapping (Value= "/angular/customerslist", method =requestmethod.get) PublicString customerslist () {return"Angular/customerlist"; }}

(2) customerlist.jsp page

<%@ taglib Prefix="C"URI="Http://java.sun.com/jstl/core"%><%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%    StringPath=Request.getcontextpath (); StringBasePath=Request.getscheme ()+ "://"            +Request.getservername ()+ ":" +Request.getserverport ()+Path+ "/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML><Head><Basehref= "<%=basePath%>"><title>My JSP ' customerlist.jsp ' starting page</title><Metahttp-equiv= "Pragma"content= "No-cache"><Metahttp-equiv= "Cache-control"content= "No-cache"><Metahttp-equiv= "Expires"content= "0"><Metahttp-equiv= "keywords"content= "Keyword1,keyword2,keyword3"><Metahttp-equiv= "description"content= "This is my page"><Scripttype= "Text/javascript"src= "<c:url value= '/jslib/jquery-1.7.2.js '/>"></Script><Scripttype= "Text/javascript"src= "<c:url value= '/jslib/json2.js '/>"></Script><Scripttype= "Text/javascript"src= "<c:url value= '/jslib/angular-1.2.5.min.js '/>"></Script><Scripttype= "Text/javascript"src= "<c:url value= '/js/customerlist.js '/>"></Script><!--<link rel= "stylesheet" type= "Text/css " href= "Styles.css" > -</Head><Body>    <DivNg-app= "MYAPP"Ng-controller= "Postcustomerscontroller">    <H3>Post Query</H3>        <ul>            <binng-repeat= "x in Names">{{x}}</Li>        </ul>    </Div>        <DivNg-app= "MYAPP"Ng-controller= "Getcustomerscontroller">    <H3>Get query</H3>    <ul>    <Ling-repeat= "x in Names">{{x}}</Li>    </ul>    </Div></Body></HTML>

(3) Customerlist.js code

/** * Customer Information list*/varApp = Angular.module ("myApp", []);//Post MethodApp.controller ("Postcustomerscontroller",function($scope, $http) {//forgive me, I don't know what this sentence is for. Instead of replacing the text with ' ', it doesn't seem to have any effect    //But there must be    varPostData ={text:' Long blob of text '    }; varMyData ={userName:"Zhang San"    }; $http. Post ("Angular/postcustomerslist", PostData,{Params:mydata}). Success (function(data, status, headers, config) {$scope. Names=data; }). Error (function(data, status, headers, config) {console.log (data);        Console.log (status);        Console.log (headers);    Console.log (config); });});//Get data from the get methodApp.controller ("Getcustomerscontroller",function($scope, $http) {$http. Get ("Angular/getcustomerslist", {params: {userName:"Zhang San"}}). Success (function(data, status, headers, config) {$scope. Names=data; }). Error (function(data, status, headers, config) {console.log (data);        Console.log (status);        Console.log (headers);    Console.log (config); });});

(4) test

Input Address: Http://localhost:8080/SpringMVC01/angular/customersList

The page is displayed as:

  

Note: Only the Post method goes backstage, presumably the controller has only one page.

The following code is printed in the background:

Zhang San ["Post", "Zhang San", "John Doe", "Lao Wang", "Lao Zhang", "Amy"] Zhang San ["Post", "Zhang San", "John Doe", "Lao Wang", "Lao Zhang", "Amy"] Zhang San ["Post", "Zhang San", "John Doe", "Lao Wang", "Lao Zhang", "Amy"] Zhang San ["Post", "Zhang San", "John Doe", "Lao Wang", "Lao Zhang", "Amy"] Zhang San ["Post", "Zhang San", "John Doe", "Lao Wang", "Lao Zhang", "Amy"]

Angularjs Learning Access Server (5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.