0

I am trying to add ModelAttribute value to different pages within the same application with the help of ControllerAdvice but output message is not getting displayed on the jsp pages.

ServicesController.java

package com.test;     
@Controller
public class ServicesController {
@RequestMapping("/serviceUser") 
public String dis() {
return "input";
}
}

ProductsController.java

package com.test;     
@Controller
public class ProductsController {
@RequestMapping("/products")
public ModelAndView display()
{
ModelAndView m=new ModelAndView("output");
return m;
}
}

GlobalAdviceHandler.java

package com.test;     
@ControllerAdvice 
public class GlobalAdviceHandler {
@ModelAttribute 
public void show(Model model) {
model.addAttribute("msg", "hello world");
}
}

input.jsp

<body>
<p>Services </p>
${msg}
</body>

output.jsp

   <body>
   <p>Products </p>
    ${msg}
    </body>

Thank you in advance!!

Harry
  • 65
  • 1
  • 2
  • 9
  • You need to add `@ControllerAdvice` in your controller after `@Controller`, as well. – Amit kumar Jul 18 '20 at 16:25
  • Added @ControllerAdvice in both ServicesController and ProductsController classes after Controller but still output is not coming – Harry Jul 18 '20 at 16:42
  • @Harry You Can checkout this [Link](https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/controller-advice-with-model-attribute.html) – Vakul Gupta Jul 18 '20 at 18:35
  • It's working now message is getting displayed for jsp pages. Thank you – Harry Jul 19 '20 at 04:21
  • If I run the program with ControllerAdvice written after Controller in Service and Products classes(as advised by @Amitkumar) it run fine and if I only write Controller then also it run fine. What is the use of writing ControllerAdvice and Controller together in one class? – Harry Jul 19 '20 at 04:50

0 Answers0