<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 添加对springmvc的支持 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <!-- <async-supported>true</async-supported> --> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第三步:编写各个Java类,以下是用户控制器(实现db_news数据库中t_user表的用户添加与用户删除)
package com.hin.controller; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.hin.entity.User; import com.hin.service.UserService; @Controller @RequestMapping("/users") public class UserController { @Resource private UserService userService; @RequestMapping("/userlist.json") public @ResponseBody List<User> getUserList() { return userService.getAllUsers(); } @RequestMapping(value = "/addUser/{userName}", method = RequestMethod.POST) public @ResponseBody void addUser(@PathVariable("userName") String userName) { userService.addUser(userName); } @RequestMapping(value = "/removeUser/{userName}", method = RequestMethod.DELETE) public @ResponseBody void removeUser(@PathVariable("userName") String userName) { userService.deleteUser(userName); } @RequestMapping(value = "/removeAllUsers", method = RequestMethod.DELETE) public @ResponseBody void removeAllUsers() { userService.deleteAll(); } @RequestMapping("/layout") public String getUserPartialPage() { return "users/layout"; } }
第四步:引入angular的js文件,如下:
这里使用Angular来实现添加用户与删除用户功能主要是UserController.js,如下:
'use strict'; /** * UserController */ var UserController = function($scope, $http) { $scope.fetchUsersList = function() { $http.get('users/userlist.json').success(function(userList){ $scope.users = userList; }); }; $scope.addNewUser = function(newUser) { $http.post('users/addUser/' + newUser).success(function() { $scope.fetchUsersList(); }); $scope.userName = ''; }; $scope.removeUser = function(user) { $http.delete('users/removeUser/' + user).success(function() { $scope.fetchUsersList(); }); }; $scope.removeAllUsers = function() { $http.delete('users/removeAllUsers').success(function() { $scope.fetchUsersList(); }); }; $scope.fetchUsersList(); };
关于Angular的其他文件具体可看源码,最后再右键项目,Run as,Maven install,再发布到Tomcat下就可以看到效果了,如下:
AngularJS整合Springmvc、Spring、Mybatis搭建开发环境就完成了,希望对大家的学习有所帮助。
您可能感兴趣的文章: