SpingMVC中@Controller和@RestController的区别

官方文档:
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.

简单点就是说@RestController就是一直返回json数据如果你配置了configureMessageConverters的话。

1)如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容,如果是对象那么则返回json。

例如:本来应该到success.jsp页面的,则其显示success.

2)如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
3)如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。

angularJS学习笔记

QQ截图20160929161813

<script>
    function c(str) {
        console.log(str)
    }

    var app = angular.module("Myapp", ['ngRoute'])
            .config(['$locationProvider',function($locationProvider){
                $locationProvider.html5Mode(false);
                $locationProvider.hashPrefix("!")
            }])
            .config(function ($routeProvider) {

                $routeProvider
                        .when('/', {
                            templateUrl: 'views/main.html',
                            controller: 'MainCtrl'
                        })
                        .when('/about', {
                            templateUrl: 'views/about.html',
                            controller: 'AboutCtrl',

                        })
                        .when('/action', {
                            templateUrl: 'views/action.html',
                            controller: 'ActionCtrl'
                        })
                        .when('/contact:q', {
                            templateUrl: 'views/contact.html',
                            controller: 'ContactCtrl'
                        }).when("/register", {
                            templateUrl: 'views/register.html',
                            controller: 'registerCtrl'
                        })
                        .otherwise({
                            redirectTo: '/'
                        })
            })
            .run(['$rootScope','$location',function ($rootScope,$location) {
                $rootScope.title = "网站的标题";
                $rootScope.keywords = "网站的关键词";
                $rootScope.description = "网站的描述";

                //路由开始
                $rootScope.$on('$routeChangeStart',function(evt, next, current){

                });

                //路由成功
                $rootScope.$on('$routeChangeSuccess', function(evt, next, previous) {

                });

                //路由错误
                $rootScope.$on('$routeChangeError', function(current, previous, rejection) {
                    c('$routeChangeError')
                    c(current)
                    c(previous)
                    c(rejection)
                });

            }])
            .factory('myFactory', function(){
                var service = {1:"a",2:"b",3:"c"};
                return service;
            })
            .directive('myDirective', function(){
                return {
                    template: '<button>Click me</button>'
                }
            })
            .directive('ensureUnique', function ($http) {
                return {
                    require: 'ngModel',
                    link: function (scope, ele, attrs, cfg) {
                        scope.$watch(attrs.ngModel, function () {
                            $http({
                                method: 'POST',
                                url: '/1.php',
                                params: {filed: attrs.name, value: attrs.ngModel, value2: ele[0].value}

                            }).success(function (data, status, headers, fg) {
                                c(data);

                            }).error(function (data, status, headers, fg) {
                                alert('网络繁忙!');
                            })
                        });
                    }
                };
            })

            .controller('MainCtrl', function ($scope, $routeParams) {
                $scope.context = "Main页面标题";
                $scope.add = function (num) {
                    ++num;
                    alert("num:" + num);
                };
                $scope.name = {name: "a001", pwd: "a123456"};

            })
            .controller('AboutCtrl', function ($scope, $routeParams, $filter) {
                $scope.context = "关于我们";
                $scope.other = "谢谢支持!";

//

                //$scope.name = $filter('lowercase');
                $scope.unClock = function () {
                    $scope.isDisabled = true;
                    // alert('ok');
                }
                $scope.myChange = function (d) {
                    c(d);
                }
                $scope.submitForm = function (d) {
                    c(d);
                }

                $scope.people = [
                    {name: "小A", city: "重庆"},
                    {name: "小b", city: "成都"},
                    {name: "小c", city: "武汉"},
                    {name: "小d", city: "广州"},
                    {name: "小e", city: "上海"}
                ];

                $scope.fields = [
                    {isRequired: true, name: "user_name", placeholder: "请输入用户名", model1: "", msg: "请输入用户名"},
                    {isRequired: true, name: "user_pwd", placeholder: "请输入用户密码", model1: "", msg: "请输入用户密码"},
                    {isRequired: true, name: "email", placeholder: "请输入用户邮箱", model1: "", msg: "请输入用户邮箱"},
                ]
                $scope.cx = "testcx"


            }).controller('ActionCtrl', function ($scope, $routeParams) {
                $scope.context = "Action测试页面";


            }).controller('ContactCtrl', function ($scope, $routeParams) {
                $scope.context = "联系人";


            }).controller("nowDae", function ($scope) {

                $scope.now = new Date();
                $scope.do = function () {

                    $.ajax({
                        url: '/1.php',
                        type: 'post',
                        dataType: 'json',
                        async: false,
                        data: {id: 10, user_name: 'a001', user_pwd: '123456'},
                        success: function (data) {
                            c("data>>>>>");
                            c(data);
                        }
                    })
                }

            }).controller("registerCtrl", function ($scope, $routeParams) {
                $scope.context = "用户注册";
                $scope.showAgreement = function () {
                    alert("用户协议...");
                };

                $scope.doreg = function () {

                    alert('sdaf');
                    return false;
                }


            })
            .filter('number2', function () {
                return function (input) {

                    return "过滤过的:" + input;


                };
            }).filter('email2', function () {
                return function (input) {
                    if (input) {
                        return "filter:" + input;
                    }

                };
            })



</script>

代码包下载:百度网盘

http://pan.baidu.com/s/1kUH1H0F

 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |