Results 1 to 6 of 6

Thread: getting EntityManager inside custom tag

  1. #1
    Join Date
    Jun 2009
    Posts
    106

    Question 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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 at 02:20 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2009
    Posts
    106

    Question 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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jun 2009
    Posts
    106

    Default HandlerInterceptor with annotations and tiles?

    Quote 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?

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •