I would like implement JSP useBean taglib as a class annotation… Is possible? How If so, what is the best (easy and efficient) way to do this?
Example:
<jsp:useBean id="p" class="beans.Person" scope="application"></jsp:useBean>
—->
//Custom annotation goes here...
@ManagedBean
public class Bean {
}
There’s no direct way using the standard JSP facilities. The oldschool
<jsp:useBean>approach is also far from MVC as there’s no means of a controller. Just pick an existing MVC framework which supports JSP as View, such as Spring MVC, JSF, etc (although JSF uses since version 2.0 JSP’s successor Facelets as default View technology) and use its managed bean facilities. Modern MVC frameworks supports annotations.In this particular case, with an application scoped bean, a completely different alternative using annotations without the need for a MVC framework is to use a
ServletContextListenerwhich is annotated with@WebListener.(although it’s only pretty strange to have something like a
Personin the application scope…)For the session scope you could implement
HttpSessionListenerand for the request scope theServletRequestListener. It’s only pretty clumsy this way. I’d really consider a decent MVC framework.