Java – can we use @ Autowired in JSP? If so, what?

I am building a web application using spring and hibernate

Solution

If you are using spring MVC, you can pass your service to JSP through modelandview

Suppose you have: controller

@Controller
public void MyController {

    @Autowired
    private MyServiceInterface myService;

    @RequestMapping(value="myUrl")
    public ModelAndView displayPage() {
       //do some stuff
        return new ModelAndView("myView").addObject("myService",myService);
    }
}

JSP:

<html>
.
${myService.myMethodIWantToUse}
.
</html>

But as Slava semushin said, this is a bad practice If you add the results of the methods and print them in the JSP, put them in the model (modelandview)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>