简体   繁体   中英

Passing $routeParams from angularjs to spring-MVC controller

This is what I have in my AngularJs controller

.when("/M:topicId", {templateUrl: "Mu", controller: "conMFs"})

app.controller('conMFs',function($scope,$routeParams){
$scope.otherId = $routeParams.topicId;
});

This is my Spring Controller

@RequestMapping(value="/controlerM", method = RequestMethod.GET)  
public ModelAndView controlerM(HttpServletRequest request,  HttpServletResponse response) throws Exception {
ModelAndView model = null;
session=request.getSession(true);
user = (User) session.getAttribute("user");

List<ends1> ends=(List<ends1>) ifriendlistservice.getends(user.getId(),5);
    session.setAttribute("MutualFriends", MutualFriends);
model = new ModelAndView("ends");

return model;

I am able to fetch the topicId from my AngularJS page in AngularJS controller (with $scope.otherId ), however I am unable to pass this value to my Spring controller, from where I am redirecting to new page.

How should I do this?

.when("/M:topicId", {templateUrl: "Mu", controller: "conMFs"})
app.controller('conMFs',function($window){
$window.location.reload();
});

Then server app gets topicId from request's url. You also need to have $locationProvider.html5Mode enabled.

How should I do this?

You shouldn't. Angular's off-hour job is to not let server apps to reload the page. The code above is quick-and-dirty, it can be ok only if you're in the middle of Angular integration into existing project, but the time-frame requires you to partially rely on old code base.

If you really need to make a redirect (eg to external page), make an AJAX request, put topicId and whatever there, get redirect url in response, redirect with $window.location.href .

I'm barely familiar with Spring, but the above applies to any server-side application.

Thanks estus,

Issue seems to be resolved...

I had created 2 controller in Spring, One for setting parameter through ajax , and other for redirection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM