任何人都可以通过angularJs ajax方法 ctrlClickHandler 来帮助调用spring方法 listEmployee ,并且应该返回结果作为响应任何字符串msg。
有3个文件:
1。的login.html
<html>
<head>
<title>Javascript Login Form Validation</title>
<script>
angular.module("app",[])
.controller("ctrl", MyCtrl)
function MyCtrl($scope,$http){
$scope.attempt = 3;
$scope.ctrlClickHandler = function(){
alert("Inside controller: value of myval is " + $scope.attempt--);
$scope.username = document.getElementById("username").value;
$scope.password = document.getElementById("password").value;
alert($scope.username);
$http.get("/leavemgmt/getlistEmployee").success(function(data){
alert(data+"...");
$scope.Employees=data;
});
}
};
var globalClickHandler=function globalClickHandler(){
alert("Inside global click handler ");
}
</script>
</head>
<body ng-app ="app" ng-controller="MyCtrl">
<div class="container">
<div class="main">
<form id="form_id" method="get" name="myform" action="/leavemgmt/getlistEmployee">
<label>User Name :</label>
<input type="text" name="username" id="username"/>
<label>Password :</label>
<input type="password" name="password" id="password"/>
<button type="button" ng-click="ctrlClickHandler()">Login</button>
</form>
</div>
</div>
</body>
</html>
2。 ValidateLogin.java
import com.bean.Employee;
@Controller
public class ValidateLogin{
private JdbcTemplate jdbcTemplateObject;
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
@RequestMapping(value = "/getlistEmployee", method = RequestMethod.GET)
public @ResponseBody String listEmployee(HttpServletRequest request) {
String SQL = "select * from employee";
List <Employee> employees = jdbcTemplateObject.queryForList(SQL);
return employees.toString();
}
}
第3。的applicationContext.xml
<bean id="dataSource"
class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
<property name = "url" value = "jdbc:mysql://localhost:3306/lMS"/>
<property name = "username" value = "root"/>
<property name = "password" value = "password"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id = "employeeJDBCTemplate"
class = "controller.ValidateLogin">
<property name = "dataSource" ref = "dataSource" />
</bean>