Passing $routeParams from angularjs to spring-MVC controller - java

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 (e.g. 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.

Related

Request Forward to another url in spring boot mvc with mustache template

I am trying to forward the request to another url in spring boot.
I know how to forward the request from one endpoint to another endpoint in the same spring boot application. The code for this is like -
#GetMapping("/home")
public ModelAndView home(#PathVariable(name = "env", required = false) String env) {
return new ModelAndView("index");
}
#GetMapping("/forward")
String callForward(){
return "forward:/home";
}
Here If I go to http://localhost:8080/forward then it will be server by "/home" endpoint and we'll get the home page rendered. Interesting thing here is "url" in the browser remains same http://localhost:8080/forward.
My requirement is > I want to forward to the request to any third party url for example when http://localhost:8080/forward is called I want it to be served by https://google.com/forward.
How can I do it.
I am open to any solution which can fulfill the requirement.
There is a difference between Forward vs Redirect.
Forward: happens on the server-side, servlet container forwards the same request to the target URL, hence client/browser will not notice the difference.
Redirect: In this case, the server responds with 302 along with new URL in location header and then client makes another request to the given URL. Hence, visible in browser
Regarding your concern, it can be achieved using multiple ways.
Using RedirectView
#GetMapping("/redirect")
RedirectView callRedirect(){
return new RedirectView("https://www.google.com/redirect");
}
Using ModelAndView with Prefix
#GetMapping("/redirect")
ModelAndView callRedirectWithPrefix(){
return new ModelAndView("redirect://www.google.com/redirect");
}
Usecase for forwarding could be something like this (i.e. where you want to forward all request from /v1/user to let's /v2/user on the same application)
#GetMapping("/v1/user")
ModelAndView callForward(){
return new ModelAndView("forward:/v2/user");
}
The second approach (using ModelAndView) is preferable as in this case, your controller will have no change where it redirects or forwards or just returning some page.
Note: As mentioned in comments, if you are looking for proxying request, i would suggest checking this SO Thread

response.sendredirect not working in external application

Response.sendRedirect(URL) is NOT working when I call from an external application. I am working on two enterprise applications parallelly.
Application A: http://localhost:9080/abc
Application B: http://localhost:8080/xyz
Angular App which is retrieving response from Application B: http://localhost:9000
Currently, I am in App-A and making AJAX call to App-B. When service hit to App-B RestController, then I want to redirect to http://localhost:9000 in browser from RestController of App-B.
I tried the below approach in RestController of App-B, but it doesn't work. Still, browser URL is http://localhost:9080/abc which is point to App-A:
#RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", "http://localhost:9000");
httpServletResponse.setStatus(302);
}
#RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:http://localhost:9000");
}
#RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.sendredirect("http://localhost:9000");
}
I need to pass data from App-A to App-B before I launch angular application. So, I am making AJAX call from App-A. I can not directly call to Angular app from App-A with passing query param. I have to pass data through the header or request body.
Could you help with this?
A redirection response to an ajax request does not automatically cause a page to redirect. You need to manually fire the redirect in your response handler.
See Redirecting after Ajax post.

How to Redirect/Forward data from Spring MVC Controller to other page as POST?

I'm working on a project which is a mix Spring MVC and coldfusion, I want to know how to redirect from the SpringMVC Controller to the cfm using a POST request.
I have this mapping defined
#RequestMapping(value = "/preprocess", method = {RequestMethod.GET})
public ModelAndView doPreprocess(HttpServletRequest request, #RequestParam Map<String, String> params, HttpSession session) {
//retrieve some parameters from db and redirect/forward here to "/postprocess.cfm"
}
After retrieve some parameters, I need to redirect/forward those parameter to other endpoint "/postprocess.cfm", this page reads the params from a form, so the request to this page needs to be a POST
Looking forward to your help. Thanks a lot!

Browser back button getting in to GET method of Spring MVC Controller instead of checking the cache

I am using Spring MVC 2.5. I have get and post methods for all the pages I have.
#RequestMapping(value = "/offer", method = RequestMethod.GET)
public ModelAndView getOffer(ModelMap model, HttpSession session) {
//code
return new ModelAndView(OFFER_SETTING_PAGE, model);
}
#RequestMapping(value = "/offer", method = RequestMethod.POST)
public ModelAndView postOffer(ModelMap model, #ModelAttribute("investorsEligiblitySetting")
//code
return new ModelAndView("redirect:/servlets/ProcessAction/privateplacement/createoffer/additionalinformation");
}
After passing the post method and displaying the next jsp file, when I try to hit the back button , instead of displaying the previous data from the cache (which is what I am looking for) it gets in to the get method of the specified url and causes some problems.
How can I make my application to look for cache first instead of getting in to a get method?
This is an old question, but I found this post that gives some ideas as to how to prevent the browser from resubmitting on the back button. You could also set something in your form when you handle the POST that the GET handler can look at to see if the POST already ran.

Java Error Page in web.xml not for all requests

Is it possible to setup error-page but that should be triggered not for all URI's but for example with /custom-path, and for requests with URI's /main-path do not handle errors?
In web.xml i suppose it is only possible to match error page with certain Exception types.
The problem is we have one single war file inside which we have both rest api and html management UI. So if error comes from rest api, it should be rendered as it is and no error page should be displayed.
I had the same problem and I solved it by calling setStatus() on HttpServletResponse instead of throwing exceptions or calling sendError().
Example of my Spring MVC controller:
#RequestMapping(value = "/{ean}", method = RequestMethod.GET)
#ResponseBody
public User getUser(#PathVariable("id" Long id, HttpServletResponse response) {
// response.sendError(HttpServletResponse.SC_NOT_FOUND)
response.setStatus(HttpServletResponse.SC_NOT_FOUND)
}
This way my RESTful API controllers don't render error pages.

Categories