Announcement Announcement Module
Collapse
No announcement yet.
getting EntityManager inside custom tag Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting EntityManager inside custom tag

    Hi Friends,

    I am writing a custom tag as shown below. I am using JPA. I am trying to get a reference to my entityManager but it keeps coming up null!

    I am referencing the EntityManager in my controllers the same way and it is working fine. This is how I tried to get a reference to the entityManager in my controllers:

    Code:
    	@PersistenceContext
    	public void setEntityManager(EntityManager entityManager) {
    	    this. entityManager = entityManager;
    	}
    I did some more digging and I found that the EntityManager can be injected in my custom tag according to this definition for WebSphere, although I am using Tomcat:
    Code:
     Attention: Injection of the EntityManager is only supported for the following artifacts:
    
        * EJB 3.0 session beans
        * EJB 3.0 message-driven beans
        * Servlets, Servlet Filters, and Listeners
        * JSP tag handlers which implement interfaces javax.servlet. jsp.tagext.Tag and javax.servlet.jsp.tagext.SimpleTag
        * Java Server Faces (JSF) managed beans
        * the main class of the application client.
    The following is the code for my custom tag (note I did try to use @PersistenceContext but it did not work):

    Code:
    public class CommentsTag implements Tag  {
    
    	private static final long serialVersionUID = -1395963113880536744L;
    	private EntityManager entityManager;
    	private JspWriter out;
    	private  PageContext pageContext; 
    
    	
    	//@PersistenceContext(unitName = "dataAccessPU")
    	@PersistenceContext
    	public void setEntityManager(EntityManager entityManager) {
    	    this. entityManager = entityManager;
    	}
    	
    	public int doStartTag() throws JspException {
    		// TODO Auto-generated method stub
    		
    		//	Get topic!!
    	
    		String sql_query_tod = "SELECT * FROM topic t WHERE t.type='tod' AND t.status='active';";
    		Query queryTOD = (Query) entityManager.createNativeQuery(sql_query_tod,Topic.class);
    How would I get a reference to entityManager in this case??
    Please advise. Thank you.

  • #2
    The entitymanager is (with spring at least) never going to be injected into a class/instance that isn't under the control of spring. Your controller is instantiated and controlled by spring your tag isn't because that is created by the web container.

    You will have to get the ApplicationContext and retrieve it from there, so no injection possible.

    Next to that IMHO you shouldn't use a tag to execute a query, that (reference) data should be setup in your controller not from a tag.
    Last edited by Marten Deinum; Oct 7th, 2009, 02:20 AM.

    Comment


    • #3
      solutions....?

      Here is the scenario:

      What I need is the latest 2 records from a table to be displayed in the homepage. It doesn't matter how the user gets to the homepage. e.g. thru the base url, or by being re-directed internally from any other controller, I would just like the latest 2 records to be shown.

      My Idea was to use a custom tag to show this information, so that each time the page refreshes those records would be there.

      Now I am thinking maybe I can either

      a) make a call to a DAO from my custom tag and get the data. Custom tag will always fire because it is part of the homepage markup.
      b) just use ajax and forget the custom tag. I am not too familiar with AJAX. Is this the best way to go? Basically, the event will be "onLoad", each time the page loads, fetch data. I could write a basic Javascript AJAX function or I can use something like DWR or JQuery?
      c) some other solution I am not aware of at this point

      Please advise.
      Thank you.

      Comment


      • #4
        Create a HandlerInterceptor (I assume here that your homepage is backed by a Controller) which simply adds that information to the model. No need to create a tag or to use ajax.

        Comment


        • #5
          HandlerInterceptor with annotations and tiles?

          Originally posted by Marten Deinum View Post
          Create a HandlerInterceptor (I assume here that your homepage is backed by a Controller) which simply adds that information to the model. No need to create a tag or to use ajax.
          Hi, thanks for the reply.
          My issue is this, I am using Tiles. So Whether I am in the LoginController, LogoutController, or any other controller that redirects to ModelAndView view= new ModelAndView("homepage"); where "homepage" is defined as a tiles definition, it does not actually go through my homePageController.

          The HomepageController is where HomePage logic is stored, like this query to the database.

          Tiles simply takes the "homepage" or in actuality, the homepage.JSP which is the body and whips it together with the header and footer.

          This is why I came up with the idea of a custom tag which would be independent of any controller. It would simply run on the homepage.jsp regardless of how it was redirected to.

          How can I use HandlerInterceptor in this scenario where tiles creates its own view. In other words, I do not want to duplicate the homepage logic in every other controller that redirects to the homepage!

          Thanks.

          p.s. is it possible to use controllers and bean mappings inside dispatcher-servlet.xml? I tried using SimpleURLMapping but then my @Controllers stopped working.
          this is the only way I know of using HandlerInterceptor (through dispatcher.xml)
          Can HandlerInterceptor be used with @Controller?

          Comment


          • #6
            p.s. is it possible to use controllers and bean mappings inside dispatcher-servlet.xml? I tried using SimpleURLMapping but then my @Controllers stopped working.
            this is the only way I know of using HandlerInterceptor (through dispatcher.xml)
            Can HandlerInterceptor be used with @Controller?
            I suggest the reference guide...

            Register a DefaultAnnotationHandlerMapping to register the interceptor. That way the interceptor will run for every controller not needing to code a tag, ajax etc.

            You don't redirect you FORWARD to your view, if you where to make it a redirect then it indeed would hit your HomePageController and you could do everything in there quite easy.

            Code:
            new ModelAndView("redirect:homepage.htm");
            Would issue a redirect to the homepage.htm url, which in turn would hit your controller which could then setup the data and after that select the homepage to render.

            Comment

            Working...
            X