本文主要是介绍AngularJS for login,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><mvc:annotation-driven/><mvc:default-servlet-handler/><context:component-scan base-package="com.liyang"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><property name="prefix" value="/WEB-INF/"></property><property name="suffix" value=".html"></property></bean> </beans>
index.html
<head><meta charset="utf-8"><title>AngularJS 依赖注入</title><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body><div ng-app="myApp" ng-controller = "loginCtrl"> <form><input ng-model = "user.name" /> <br><input ng-model = "user.pass" /> <br><button ng-click = "login(user)" > LogIn</button></form> <p> {{res}}</p> </div><script>var app = angular.module('myApp', []);app.controller('loginCtrl', function($scope , $http) {$scope.res = "登陆测试" ;$scope.login = function(user) {$http.get("admin/login?username=" + user.name + "&password=" + user.pass) .success(function(res){$scope.res = res.data ;}) }});</script></body>
</html>
package com.liyang.view;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
public class Web {@RequestMapping(value = "index")public String index(){return "index" ;}
}
package com.liyang.controller;import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.gson.Gson;@Controller
@RequestMapping(value = "/admin")
public class AdminController { @RequestMapping(value = "login")@ResponseBodypublic String login(@RequestParam(value = "username") String username ,@RequestParam(value = "password") String password ){Map<String , String> res = new HashMap<String, String>() ;if(username.equals("a") && password.equals("a")){res.put("data", "yes") ;}else res.put("data" , "wrong") ;Gson gson = new Gson() ;return gson.toJson(res) ; }}
这篇关于AngularJS for login的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!