1

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 {

  }
Lucas Batistussi
  • 2,283
  • 3
  • 27
  • 35

1 Answers1

1

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 ServletContextListener which is annotated with @WebListener.

@WebListener
public void Config implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        event.getServletContext().setAttribute("p", new Person());
    }

    // ...
}

(although it's only pretty strange to have something like a Person in the application scope...)

For the session scope you could implement HttpSessionListener and for the request scope the ServletRequestListener. It's only pretty clumsy this way. I'd really consider a decent MVC framework.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How I can get all classes with a specific annotation at runtime? – Lucas Batistussi Aug 05 '12 at 01:50
  • You'd basically need to scan and load all classes in the classpath (or only some paths under it) one by one and test if [`Class#getAnnotation()`](http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getAnnotation(java.lang.Class)) doesn't return `null` for that annotation. There exist tools for this. In the future please press `Ask Question` button on the right top if you have a new question instead of (ab)using comments of a completely different question for this :) – BalusC Aug 05 '12 at 01:54
  • I want to implement my solution to ManagedBean... I don't want use other tools... This last question is part of my idea to solve my first problem :) Isn't totally "different" question – Lucas Batistussi Aug 05 '12 at 01:57
  • That's completely fine if it's just a private hobby project for pure learning/exercising purposes, been there done that. But other than that, I strongly recommend to not reinvent the MVC framework wheel or you (and your successors) will regret this. – BalusC Aug 05 '12 at 01:58
  • Yep ! I love to know how the things are made to use later :) Thanks for the responses – Lucas Batistussi Aug 05 '12 at 02:00
  • @BalusC I am sorry for offtopic. But I do not know how else to contact to you:) Help please figure out who is more right. What variant is better? Or are they all equally right? I interested this issue and your opinion. I know that you are very strong specialist. http://stackoverflow.com/questions/11892284/converting-jsp-to-jstl. Please answer me if you is not hard to my mail `misterZesler@yahoo.nl` or any convenient way for you. Thank you for your very helpful actions here! – Michael Aug 10 '12 at 01:36