Requestmapping from angular-ui-router and spring m

2019-09-04 05:50发布

问题:

I am using AngulaJS as a javascript client side and spring mvc as a rest backend. In AngulaJS i am using ui-router.

Here is config.js file

function config($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/index");
    $stateProvider
        .state('trains', {
            url: "/trains",
            templateUrl: "views/pages/trains.html",
            data: {
                pageTitle: 'Trains'
            }
        })

Below is html file (left-sliderbar.html

<li ui-sref-active="active">
    <a href="javascript:;" ui-sref="trains" title="the trains">Trains </a>
 </li> 

The problem is when I clicked on "Trains" menu in left left-sliderbar, I cannot get request mapping with the method in Rest Backend of Spring MVC. Below is code from Controller of Spring MVC

@RequestMapping("/trains")
    public String getTrainPartialPage(ModelMap modelMap) {
        System.out.println("---------Request Mapping: /trains: " + this.getClass());
        return "pages/trains";
    }

Please help me to fix it out, I'd like to use ui-router than ngRoute, thanks you

回答1:

function config($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/index");
$stateProvider
    .state('trains', {
        url: "/trains",
        data: {
            pageTitle: 'Trains'
        },views: {
                'content@': {
                    templateUrl: "views/pages/trains.html",
                    controller: 'TrainsController'
                }
            }
    })

and you need to implement the rest calls in your service or in your TrainsController.



回答2:

I had the same problem in a same context.

I think you shoud try to use templateUrl: "views/pages/trains" instead of templateUrl: "views/pages/trains.html" in your $stateProvider provider.

The back-end controller should expose this request mapping:

@RequestMapping(value = "/train")
public ModelAndView getMain() {
    return new ModelAndView("pages/train");
}

NB: Using ModelAndView as a return object instead of String (didn't worked with String and actually I con't figure why).

Angular UI Router will write /train in the url and Spring will serve the html file mapped on the /train route.

Hope will help you.