在War-Packaged App的@ FaceSconverter / Validator内获取EntityManager / EJB
-
17-09-2020 - |
题
我在Glassfish V3.0.1上运行了一个javaee6应用程序,使用EJB 3.1(应用程序包装为War),所有的EJB都是@LocalBeans。 目前,我正在编写一个在JSF2中使用的Facesconverter,其中我需要EntityManager从ID获取一个实体。
现在我想知道,这是最好的和最干净的方式来获得encesconverter内的entitymanager,现在我们在jee6?或者我甚至可以通过表达语言访问EJB?焊接/ CDI在弹簧转换器内部不起作用,或者是呢?
@FacesConverter(value="subscriptionListConverter")
class SubscriptionListConverter extends Converter {
public Object getAsObject(FacesContext ctx, UIComponent comp, String value) {
var id:Long = Long.parseLong(value);
// How to get the entitymanager?
return em.find(User.getClass, id);
}
public String getAsString(ctx:FacesContext, comp:UIComponent, value:Object) {...}
}
.
抱歉,我希望这是没有重复的,但大多数情况下我看到哪里有略有不同,并没有帮助我。 解决方案 2
好后再尝试我成功地获得了一个手动查找的EJB:
Context ctx = new InitialContext();
UserEJB userEJB = (UserEJB) ctx.lookup("java:global/teachernews/" + UserEJB.class.getName())
.
看起来还好,但无论如何,如果还有其他有趣的方法,请随时发布它们。
其他提示
我也使用手动查找,但我创建了一个抽象类实现转换器接口,延伸到不同的转换器并有一种方法来获得EJB。要在不同的应用程序中重用它我完成了以下内容以获取AppName和Web模块名称:
abstract public class Converter implements javax.faces.convert.Converter
{
public Object getManager(String jndiName) throws NamingException
{
ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
Context env = new InitialContext();
String aname = (String)env.lookup("java:app/AppName");
if (aname == null || aname.length() == 0 || servletContext.getContextPath().equals(aname)) //FIXME If deploying war inside an ear, appname equals module name, won't work.
return env.lookup("java:global/" + aname + "/" + jndiName);
else
return env.lookup("java:global/" + aname + servletContext.getContextPath() + "/" + jndiName);
}
}
. 我刚刚使用@ejb将它们注入我的转换器。
不隶属于 StackOverflow