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>
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) ; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。