Angularjs and back-end connection is essentially the connection between JavaScript and the back end, the transmission is the JSON object, just angularjs two-way data binding is very practical, it can help us write a lot less JavaScript code, its powerful no more introduction.
Start by importing the jar packages required for the project, which have the DWR jar package and the JSON jar package:
There is also a need to configure the DWR framework, omitting ....
JSP page code:
<%@ 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" >
JS Code:
Var usersignup = angular.module ("Usersignupmodule", []);//write a angular module usersignup.controller (" Usersignupctrl ", [" $scope ", Function ($scope) { $scope. User={email:" ", Password:" ", Telphone: ""};//json model $scope. Signup=function () { /** * Call the background Java class, add user * json.stringify ($ Scope.user) method is to convert the JSON object $scope.user into a JSON string * in addition, JSON string into JSON object: $scope. User = json.parse (JSONSTR); */ //here calls the background Java class and passes the JSON string as a parameter userservice.signup (Json.stringify ($scope. User), function () { }); }; $scope. clear= function () { $scope. User={email: "", Password: "", Telphone: ""}; };}]);
Background Java code:
Entity class, where the fields need to correspond to the field one by one of the ANGULARJS model:
package com.lin.angular.entity;public class user { private string email; private string password; private String telPhone; public user () { super (); } public user (string email, string password, String telphone) { super (); this.email = email; this.password = password; this.telphone = Telphone; } public string getemail () { Return email; } public void setemail (String email) { this.email = email; } public string getpassword () { return password; } public void SetPassword (String password) { this.password = password; } public string gettelphone () { return telPhone; } public void settelphone (String telphone) { this.telPhone = telPhone; } @Override public string tostring () { return "user [ Email= " + email + ", password= " + password + ", telphone=" + telphone + "]"; }}
Business class:
package com.lin.angular.service;import com.lin.angular.entity.user;import com.lin.angular.util.jsonservice;public class userservice { public void signup (STRING&NBSP;JSONSTR) { User user = (User) jsonservice.getinstance (). Jsonstring2object ( Jsonstr, user.class); signup (user); } public Void signup (User user) { /** * here call jdbc 's api to add the user to the database */ system.out.println ("-------------------------User registration------------------------"); system.out.println (" The user registered: "+user.tostring () ); }}
Helper classes that JSON and Java classes convert to each other:
package com.lin.angular.util;import net.sf.json.jsonobject;public class jsonservice { private volatile static JSONService Service; private jsonservice () {} /** * here is a single piece * @ return */ public static jsonservice GetInstance () { if (service == NULL) { synchronized (Jsonservice.class) { if (service == null) { service = new jsonservice (); } } } return service; } public static object jsonstring2object (String jsonstr,class type) { if (JsonStr.indexOf ("[")!=-1) { jsonstr.replace ("[", ""); } if(Jsonstr.indexof ("]")!=-1) { jsonstr.replace ("]", ""); } Jsonobject jsonobject = (New jsonobject ()). Fromobject (jsonstr);//json string into JSON object return Jsonobject.tobean (Jsonobject, type);//json object into Java class } public static string object2jsonstring (Object object) { JSONObject json = Jsonobject.fromobject (object);//Convert Java object to JSON object string&nbsP;str = json.tostring ();//Convert a JSON object to a string return str; }}
Angular JS How to send data to the background Java class