AngularJS for login
2016-08-12 14:23
375 查看
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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://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>
<?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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://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") @ResponseBody public 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 入门系列-1 使用 AngularJs 搭建页面基本框架
- angularjs --- ngResource 类似于 ajax发送请求。
- angularjs记账
- 表单验证<AngularJs>
- angularjs学习笔记—指令input
- AngularJS ng-class用法
- 图表控件Highcharts在AngularJS中的使用
- AngularJS入门笔记(1)
- Angular CLI is here for AngularJS 2
- AngularJS RootScope 源码分析
- jquery 对身份证号码验证以及AngularJS表单验证
- AngularJS 表单验证
- angularjs输出html
- Angular2 组件
- Angular之作用域与事件(转)
- 【AngularJs】Angular双向数据绑定
- Angular中的Error: [$resource:badcfg]错误如何解决之一种
- angularjs学习笔记—工具方法
- angular controller之间通信方式
- Angular之filter学习