Transferring $routeParams from AngularJS to Spring MVC controller

Here is the code snippet from my AngularJs controller:

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

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

And this is my implementation in the 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;

Although I am successfully extracting the `topicId` value from my AngularJS page into the AngularJS controller using `$scope.otherId`, I am facing challenges passing this value to my Spring controller for redirecting to a new page.

What would be the best approach to resolve this issue?

Answer №1

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

Next, the server app retrieves the topicId from the request's URL. It is important to have $locationProvider.html5Mode enabled.

What is the recommended approach for this?

Avoid doing this. Angular should prevent server apps from reloading the page. The code snippet provided is a temporary solution and should only be used during the transition of integrating Angular into an existing project with time constraints that require reliance on old code.

If you must redirect (e.g. to an external page), consider making an AJAX request, include the topicId in the data, retrieve the redirect URL in the response, and perform the redirection using $window.location.href.

While my knowledge of Spring is limited, the principle mentioned above applies to any server-side application.

Answer №2

Appreciate the help, estus.

Looks like we were able to fix the problem...

I ended up creating 2 controllers in Spring - one for handling parameters through ajax and the other for redirection.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript code that opens a URL in a new tab with proper formatting

I'm having trouble figuring out how to make my JavaScript open a URL in a new tab. Here is the script: function viewSource(){ var webcache = "http://webcache.googleusercontent.com/search?q=cache:"; var request = "&prmd=ivn&strip=0& ...

Using TinyMCE editor to handle postbacks on an ASP.NET page

I came up with this code snippet to integrate TinyMCE (a JavaScript "richtext" editor) into an ASP page. The ASP page features a textbox named "art_content", which generates a ClientID like "ctl00_hold_selectionblock_art_content". One issue I encountered ...

Exploring the concept of looping through an array of JSON objects using AngularJS

I am working with a JSON object and my goal is to extract specific information to be displayed using console.log in the browser. FROM: Frontier WHSE, TO: ENTEC POLYMERS The JSON object structure is as follows: { "loadStops": [{ "id": 1, ...

HTML - implementing a login system without the use of PHP

While I am aware that the answer may lean towards being negative, I am currently in the process of developing a series of web pages for an IST assignment in Year 9. Unfortunately, the web page cannot be hosted and our assessor lacks the expertise to utiliz ...

What is the best method for starting a string using the symbol '~'?

Hello everyone! I have a task that requires me to add a special feature. The user needs to enter something in a field, and if it starts with the tilde (~), all subsequent entries should be enclosed in a frame within the same field or displayed in a list ...

Verify whether the element retains the mouseenter event after a specified delay

I recently implemented some blocks with a mouseenter and mouseleave event. <button onMouseEnter={this.MouseEnter}>hover</button> MouseEnter(e) { setTimeout(() => { // Checking if the mouse is still on this element // Pe ...

I'm curious whether there exists a polyfill available for the window.DOMParser() method that can

Seeking to modify the XML to JSON converter using DOMParser. Wondering if there is a method to adapt it for NodeJS. ...

Choose particular ng-repeat elements to emphasize them in a different location

I am currently working with a list that is being parsed by an ng-repeat directive. The user has the ability to filter this list, which is functioning correctly. One particular feature I am trying to implement is highlighting the first item in the filtered ...

Using HTML5 Canvas: Implementing an Image.onload() function through Image.prototype

I am trying to create a simple function that will refresh the canvas automatically when an image is loaded. The idea is to be able to use code like this: var map = new Image(); map.onloadDraw(); map.src = "images/" + worldmapCanvasShapeImage; Currently, ...

Resolving a duplicate class exception with Javassist

Currently, I am using Javassist to insert a line of code into a method's body. This involves a simple modification for adjusting the text color of a label inside the intellij-IDE. However, when attempting to make this modification, I encounter the fol ...

The datepicker in Angular UI is displaying incorrect dates

Currently, I am developing an application using Angular and incorporating Angular UI. One of the features I have implemented is a datepicker that is coded like this: <input type="text" ng-required="true" name="targetDate" uib-date ...

Facing difficulties with the XPATH due to an inability to access specific parts of the HTML code

Currently, I am facing an issue where I need to click on a link using Selenium and Java. When searching for the link using XPath, I am encountering a problem where only white spaces are displayed instead of most of the webpage content. The highlighted link ...

Calculate the total rows within a specified date range

Within my database, there is a column named IsStaff, which returns a value as a bit. This signifies whether a staff member in a company has an illness (1) or not (0). How can I construct an SQL query to count the occurrences of both 1's and 0's w ...

Are the libraries imported within lazy loaded modules found in vendor.bundle.js?

Does the vendor.bundle.js file only consist of libraries that are imported within AppModule? I have removed all content from app.module.ts and noticed that the size of vendor.bundle.js is 9.5MB when using ng serve, but reduces to 3.22MB when using ng bui ...

Transmit information to an AngularJS application instantly

Looking for a solution to keep my AngularJS app displaying real-time data without constantly querying my api. Is there a way to establish a connection between my server and the AngularJS app so that new data can be pushed from the server to the app for dis ...

Creating dynamic transformations and animations for characters and words within a paragraph in 3D

Looking to add animation effects to specific parts of a paragraph, but transforming the entire box instead. Remembering seeing a solution on StackOverflow before, now regretting not saving it. Spent over an hour searching for a similar answer without succ ...

Conceal interactive JavaScript buttons depending on a variable's value

I encountered a similar issue like this one: Hiding dynamically added buttons based on an if statement. I am currently working on a JavaScript modification for a Dark Room to enhance my JS skills. Both of the code snippets mentioned in the link are not min ...

Adding JavaScript files to a project in Ionic2 with Angular2 integration

I'm looking to incorporate jQuery into my Ionic2 app, which requires loading several JavaScript files: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/j ...

Uploading to npm unsuccessful as the specified file in the `main` attribute was not successfully published

I am currently facing an issue while trying to publish a package on npm. The .js file I specified in the package.json is not being included with the package. My project is built using typescript, and I transpile it using the following npm script... "prepu ...

Error in Angular multiselect dropdown: Unable to retrieve the length of undefined property

counter: number = 0; getDatatypes(){ if(this.counter == 0) { if(this.appId != 0) { if(undefined != this.datatypes && this.datatypes.length) for (let i = 0; i < this.datatypes.length; i++) { this.ap ...