AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作示例【附demo源碼下載】
本文實(shí)例講述了AngularJS實(shí)現(xiàn)與Java Web服務(wù)器交互操作的方法。分享給大家供大家參考,具體如下:
AngularJS是Google工程師研發(fā)的產(chǎn)品,它的強(qiáng)大之處不是幾句話就能描述的,只有真正使用過的人才能體會到,筆者準(zhǔn)備在這篇文章中,以一個簡單的登錄校驗(yàn)的例子說明如何使用AngularJs和Web服務(wù)器進(jìn)行交互。
準(zhǔn)備工作
1.下載angular js庫。
官網(wǎng)下載地址:https://angularjs.org/
或者點(diǎn)擊此處本站下載。
2.開發(fā)環(huán)境準(zhǔn)備,由于是和Tomcat服務(wù)器進(jìn)行交互,所以JDK什么的都是必不可少的。筆者機(jī)器上使用Eclipse Luna版、JDK7.0和Tomcat8.0。
瀏覽器最好選用Chrome或Firefox調(diào)試起來比較方便。
AngularJs與Java Web服務(wù)器交互案例
這是筆者使用AngularJs和html5、css寫好的一個前端頁面,我們需要實(shí)現(xiàn)的是當(dāng)點(diǎn)擊提交案例后,將文本域中的數(shù)據(jù)提交到服務(wù)器端進(jìn)行校驗(yàn),服務(wù)器端向客戶端返回相應(yīng)的處理結(jié)果。代碼如下:
<!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>登錄</title> <script src="js/angular-1.3.0.14/angular.js"></script> <link rel="stylesheet" type="text/css" href="css/index.css"/> </head> <body ng-app="myApp"> <div> <ul id="loginForm" ng-controller="LoginForm"> <li>用戶名:<input type="text" name="uname" ng-model="uname" /></li> <li>密 碼:<input type="password" name="pword" ng-model="pword" /></li> <li id="loginBtn"><input type="button" value="提交" ng-click="submit()"/> <input type="button" value="重置" ng-click="resetInfo()"/></li> </ul> </div> <script> angular.module("myApp", []) .controller("LoginForm", function($scope,$http) { $scope.resetInfo=function() { $scope.uname=""; $scope.pword=""; } $scope.submit=function() { var postData = "?uname="+$scope.uname+"&"+"pword="+$scope.pword; var url = "loginAction.do" + postData; $http.post(url).success(function(data) { alert(data); }); } }); </script> </body> </html>
AngularJs對服務(wù)器的請求都是通過Ajax來實(shí)現(xiàn)的,所有的操作都封裝在$http中,通過$http.post()方法以uname和pword做為參數(shù)向服務(wù)器端發(fā)送請求,請求資源為loginAction.do,然后調(diào)用alert方法彈出服務(wù)器端返回的內(nèi)容。
在服務(wù)器端,我們需要增加一個Servlet來處理客戶端的請求,并根據(jù)請求內(nèi)容向客戶端返回不同的數(shù)據(jù)。
在web.xml配置servlet,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>AngularJs</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 處理客戶端請求Servlet --> <servlet> <servlet-name>LoginAction</servlet-name> <servlet-class>com.csii.action.login.LoginAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginAction</servlet-name> <url-pattern>/loginAction.do</url-pattern> </servlet-mapping> </web-app>
我們所有的業(yè)務(wù)邏輯都在LoginAction類中處理,LoginAction代碼如下:
package com.csii.action.login; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginAction extends HttpServlet{ private static final long serialVersionUID = 1L; private final String USERNAME = "Rongbo_J"; private final String PASSWORD = "1234567"; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String uname = req.getParameter("uname"); String pword = req.getParameter("pword"); PrintWriter pw = resp.getWriter(); if(USERNAME.equals(uname) && PASSWORD.equals(pword)) { pw.write("username and password is right!"); }else { pw.write("username or password is wrong!"); } } }
這里我們簡單的模擬一下,用戶名和密碼信息并沒有從數(shù)據(jù)庫中取出。
String uname = req.getParameter("uname"); String pword = req.getParameter("pword");
我們從req對象中拿到客戶端傳過來的數(shù)據(jù),和USERNAME 、PASSWORD 對比,如果相同則返回"username and password is right!",否則返回"username or password is wrong!"
然后我們回到登錄界面,用戶名和密碼輸入錯誤可以看到:
正確輸入Rongbo_J和1234567:
完整demo實(shí)例代碼點(diǎn)擊此處本站下載。
希望本文所述對大家AngularJS程序設(shè)計(jì)有所幫助。
相關(guān)文章
AngularJS實(shí)現(xiàn)根據(jù)不同條件顯示不同控件
本篇文章主要介紹了AngularJS實(shí)現(xiàn)根據(jù)不同條件顯示不同控件的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例
這篇文章主要介紹了angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08詳談Angular 2+ 的表單(一)之模板驅(qū)動型表單
這篇文章主要介紹了Angular 2+ 的表單(一)之模板驅(qū)動型表單,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04詳解ng-alain動態(tài)表單SF表單項(xiàng)設(shè)置必填和正則校驗(yàn)
這篇文章主要介紹了詳解ng-alain動態(tài)表單SF表單項(xiàng)設(shè)置必填和正則校驗(yàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06AngularJS基礎(chǔ) ng-non-bindable 指令詳細(xì)介紹
本文主要講解AngularJS ng-non-bindable 指令,這里幫大家整理了ng-non-bindable指令的基本知識資料,有需要的小伙伴可以參考下2016-08-08