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.
HandlerInterceptor with annotations and tiles?
Quote:
Originally Posted by
Marten Deinum
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?