1

Hi, I don't know this right question or not but i got doubt thats why i posted this question.

if i use @RestController then i can write my response directly to HTTP Response object. But, i want to serve that response to JSP. how to serve this response to JSP? please , help me on this, i searched and read so many blogs and websites still i didn't get any clarification. #inSTACKOVERFLOW How to use Spring 4 Rest Controller to serve for Jsp view? I got this one, but here Same question asked by some one, here they are suggested like instead of @RestController use @Controller for JSP View.

@Controller: This Annotation introduced in Spring2.5 Version. For Supporting WEB-Services, the spring people used @ResponseBody with @Controller , if You Use @ResponseBody Then you can write response directly into Http Response. Later

@RestController: This Annotation introduced in Spring4.0 Version, Instead of using @Controller+@ResponseBody simple you can use @RestController.

so, finally with help of @RestController we can write response directly into HTTP Response Object. This Response, i want to send to JSP View.

i know, by using ajax we can do it. but if i use ajax i can get every response into same page only.

Here , if i want to use JSP's for View with the combination of Spring-Rest, how can i pass my response to jsp view.

Please help me.

  • ResponseBody is not for jsp pages. it is used for json as example . So thats why RestController is not used for jsp pages, too. Use Controller and insert the data you need into you model – YingYang May 30 '18 at 13:27
  • Thanks for Reply, After this discussion i came to know . Yes , you are right `Http Response Object is not for jsp pages`. to get response from Controller to jsp we need to use model object with @Controller annotation. for like JSON Response or XML Response we can use RestController( spring-rest). – K Kishore Kumar Reddy May 30 '18 at 13:50

3 Answers3

2

You shouldn't use a RestController to return a jsp page, change your annotation into @Controller and make your functions return a jsp page.

If you need to offer a Rest service and return a jsp as well then refer to this answer

Ouissal
  • 1,519
  • 2
  • 18
  • 36
0

Make it the way as Quissal said.

With a rest controller, your return type is normaly some kind of object for serialisation (like into json) and NOT a jsp page

To do a web application you have to use @controller and a jsp mapping.

There a lot of examples on the web, like: http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example-annotation/

YingYang
  • 378
  • 1
  • 5
  • 15
  • Thanks for Reply, after your reply i got to know @Controller for developing web-applications. RestController for developing web-services concept for communicating different platfrom applications. – K Kishore Kumar Reddy May 30 '18 at 13:39
0

Use @Controller annotation, and set data in ModelMap and return String (should be jsp name)

Controller:-

@Controller
@RequestMapping("/product")
public class ProductController {

   @RequestMapping(method == RequestMethod.GET)
   public String getProduct(ModelMap model) {

       //get details from db and set to modelmap, testing hardcoded product name
      model.addAttribute("productName", "toys");
      return "productDetails";  
   }

Jsp page to access model data as,

productDetails.jsp

<html>
<head>
  <body>
     <h2>${productName}</h2>
  </body>
</head>
</html>
Anil Nivargi
  • 1,473
  • 3
  • 27
  • 34