3

I have a Spring MVC web form built using JAVA Spring MVC (Backend) and AngularJS (frontend), JSP(for view) and MySQL DB. A user is able to submit the form after filling it out and search an existing entry. An email is also sent out to the user once the entry has been submitted. The entry also gets saved on a database. In the email body, I am trying to have a link which redirects the user to the form populated with the entries corresponding to the given id..the same way as when the search button is clicked..the form gets populated with the existing form fields..

Currently I wrote a controller which returns the JSP page with the data (searchData)..using ModelAndView however.. it is not populating the form with the details of the entry. How can I achieve this functionality?

I wrote the below code for the controller

@RequestMapping(value = "/searchById", method = RequestMethod.GET)
public ModelAndView populateEntrydata(@RequestParam(value = "id") String id) {
    String url= "http://localhost:8080/myform/searchById?id=" + id;
    ModelAndView model = new ModelAndView("index"); //jsp page
    ResponseEntity<Data> searchData = search(id);

    model.addObject("searchById", searchData);
    // String, String, object
    return model;
}  

How can I access the model object in the angularJS controller so I can populate the jsp with the contents i receive from the ModelAndView?

3
  • That depends on the form and why you are using AngularJS Commented Sep 28, 2017 at 0:13
  • I am using AngularJS for modularity. Do you know how to access model object in AngularJS Controller? Commented Sep 28, 2017 at 0:53
  • You can't AFAIK. Use REST as back-end and AngularJS for MVC. Node.js or Spring will do. Avoid Servlets here. Commented Sep 28, 2017 at 7:54

2 Answers 2

4

The mistake that you are doing here is mixing serverside presentation (JSP) and clientside presentation (Angular) layers. When you are dealing with clientside presentation technologies like Angular (react or vue or any yet-another-javascript framework) all your server side needs to worry about is the data (usually json/xml) not the view (html).

Solution

If you made up your mind about Angular or any other SPA, then forget about JSP, create a spring boot rest webservice.

Sign up to request clarification or add additional context in comments.

Comments

2

I would like to suggest a slightly different approach. When user clicks on the link in the email, it gets redirected to the form. In this proccess, dont fetch the data. Write a method in controller, which gets loaded with the page. In this method you will make call to the method in your spring controller with something like this:

$http.get(<url>).success(funtion(data){
    console.log(data);
});

The url is the url to call the method in java controller(along with the id as url parameter). You will receive the response data from java app in the data variable. From there you can use it to populate the jsp page. Hope that helps.

6 Comments

Hi Sddhesh, Thanks a lot for replying. The ModelAndView function I have written in my Java controller returns a JSP Page..how do I access the data in controller?
You can write methods in your spring controller. One method will just return a JSP page. Second will return the search data that you want to populate in front end. The $http.get method will have URL for second URL. This way, you will get the data as shown in the answer. Assign that data to $scope.<variable_name> and use it in angular controller.
The problem is i need to know the ID that is on the email and on the page that loads up once clicked...how do i get that id so that I can send it in to the java controller and get the data that corresponds to that id? URL: "localhost:8080/myform/searchById?id=1234;
Use $location.search().id in angular controller. This should give you the ID
It worked! Thank you so much for your help! God bless you :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.