正确的工作惠特路径在RESTful api与春天mvc,角度和休眠

时间:2016-08-23 22:10:10

标签: java angularjs spring spring-mvc

我有一个我想要的designprojects.jsp将在LoginController之后运行,我希望我的路径将包含当前月份和年份,它将如下所示:.... / designprojects / 2016/08 / 我想用angularJS CRUD这个路径。怎么做对吗? 我应该在angular_service文件中为REST_SERVICE_URI写什么?可能是REST_SERVICE_URI = '........./designprojects/{year}/{month}/';? 谢谢你们......

我的LoginController:

@Controller
public class LoginController {

@Autowired
private LoginService loginService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
    return "redirect:/login";
}

@RequestMapping(value = {"/login"}, method = RequestMethod.GET)
public ModelAndView displayLogin(User user) {
    ModelAndView model = new ModelAndView("/login");
    model.addObject("user", user);
    return model;
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView executeLogin(HttpServletRequest request, @ModelAttribute("user") User user) {
    ModelAndView modelAndView = null;
    if (user != null && user.getUsername() != null & user.getPassword() != null) {
        Employee employee = loginService.login(user.getUsername(), user.getPassword());
        if (employee != null) {
            request.getSession().setAttribute("employee_id", employee.getId());
            request.getSession().setAttribute("employee_name", employee.getFirstname());
            request.getSession().setAttribute("employee_role", (employee.getRole()).getRole());
            Date date= new Date();
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int month = cal.get(Calendar.MONTH)+1;
            int year = cal.get(Calendar.YEAR);
            modelAndView = new ModelAndView("redirect:/designprojects/" + year + "/" + month);
            return modelAndView;
        }
    }
    modelAndView = new ModelAndView("/login");
    request.setAttribute("errorMessage", "Invalid Username or Password");
    return modelAndView;
}

我的DesignProjectController:

@Controller
public class DesignProjectsRestController {

@Autowired
DesignProjectService designProjectService;

@RequestMapping(value = "/designprojects/{year}/{month}", method = RequestMethod.GET)
public ResponseEntity<List<DesignProject>> listAllDesignProjects(@PathVariable(value="year") int year, @PathVariable(value="month") int month) {
    List<DesignProject> listOfProjects = designProjectService.getAllDesignProjects(year,month);
    return new ResponseEntity<List<DesignProject>>(listOfProjects, HttpStatus.OK);
}

@RequestMapping(value = "/designprojects/{year}/{month}/{id}", method = RequestMethod.GET)
public ResponseEntity<DesignProject> getDesignProject(@PathVariable("id") int id) {
    DesignProject designProject = designProjectService.getDesignProjectsById(id);
    return new ResponseEntity<DesignProject>(designProject, HttpStatus.OK);
}

@RequestMapping(value = "/designprojects/{year}/{month}", method = RequestMethod.POST)
public ResponseEntity<Void> createDesignProject(@RequestBody DesignProject designProject) {
    designProjectService.addDesignProject(designProject);
    return new ResponseEntity<Void>(HttpStatus.CREATED);
}

@RequestMapping(value = "/designprojects/{year}/{month}", method = RequestMethod.PUT)
public ResponseEntity<DesignProject> updateDesignProject(@RequestBody DesignProject designProject) {
    designProjectService.updateDesignProjects(designProject);
    return new ResponseEntity<DesignProject>(designProject, HttpStatus.OK);
}

@RequestMapping(value = "/designprojects/{year}/{month}/{id}", method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteDesignProject(@PathVariable("id") int id) {
    designProjectService.deleteDesignProject(designProjectService.getDesignProjectsById(id));
    return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}


}

1 个答案:

答案 0 :(得分:0)

首先,检查index.html的基本上下文href“/”

*<head>
    <title ng-bind="title">AngularJs</title>
    <meta charset="UTF-8">
    <base href="/" />
</head>*

然后使用服务创建文件以连接到REST api

(function () {
    'use strict';

    angular
        .module('app.core')
        .factory('coursessrevice', coursessrevice);

    coursessrevice.$inject = ['$resource'];

    /* @ngInject */
    function coursessrevice($resource) {
        console.log('resource invoke');
        return $resource('/api/courses/:id', {id: '@id'},
            {create: {method: 'POST'}, save: {method: 'PUT'}});
    }
})();

将服务注入您的控制器,就像这样

(function () {
    'use strict';
    angular
        .module('app.courses')
        .controller('CoursesCtrl', CoursesCtrl);

    CoursesCtrl.$inject = ['$scope', '$location', '$filter', 'coursessrevice'];

    function CoursesCtrl($scope, $location, $filter, coursessrevice) {
        var vm = this;
        vm.courses = [];
        vm.unfiltredCourses = [];
        vm.addCourse = addCourse;
        vm.search = search;
        vm.queryString = '';

        activate();

        function activate() {
            listCourses();
            vm.unfiltredCourses = angular.copy(vm.courses);
        }

        function listCourses() {
            vm.courses = coursessrevice.query();
            vm.courses.$promise.then(function (result) {
                vm.courses = angular.copy(result);
                vm.unfiltredCourses = angular.copy(vm.courses);
            });
        }

        function addCourse() {
            console.log('Add: ');
            $location.path('/courses/new');
        }

        function search() {
            if (!vm.queryString == '') {
                vm.courses = angular.copy(vm.unfiltredCourses);
                vm.courses = $filter('courseSearchFilter')(vm.queryString, vm.courses);
            }
        }
    }
})();

检查Github

上的示例