我想在CDI/JSF2应用程序中使用RESTful服务.我对JAX-RS不是很熟悉,但是我已经读过它的生命周期与CDI/JSF2不兼容.是否可以将JAX-RS与CDI/JSF2合并到JEE6堆栈中?如果没有替代品?
谢谢
为了得到RequestFactory以持续连接的实体,我需要确保我用同样的EntityManager每个请求.
我根据ThreadLocal实现拼凑了我自己的Factory类,但我不确定如何正确释放资源(例如,如何知道请求已完成并调用close()).
是否有一种简单的方法可以确保在给定的ServletRequest中使用单个EntityManager而无需使用完整的J2EE/CDI?如果必须,我会采取这种方式,但我希望保持简单,特别是因为我想继续使用GWT附带的轻量级开发服务器.
我想清楚地了解开发JSF应用程序的正确方法.在我们的项目中,模型和控制器是同一个类,由CDI bean表示.我有点困惑这是不是一个好习惯.据我了解MVC模式,控制器应该处理用户操作(例如提交表单),模型应该包含业务逻辑和数据.或者可以,他们在同一个班级?
此外,每个页面都应该有自己独立的控制器/模型(即它被认为是一种好的做法)吗?
在我的JSF网络应用程序中,我想为用户创建一个用于书签或复制的URL.那部分很容易,像这样:
<h:link value="Permanent Link"
outcome="/showStuff">
<f:param name="recID" value="#{bean.recordID}" />
</h:link>
Run Code Online (Sandbox Code Playgroud)
虽然该链接中包含所需的参数(recID),但它也具有JSF Servlet生成的windowid参数.有没有方便的方法来生成没有windowid参数的URL ?或者这有什么不同?
(这是Mojarra)
我正在尝试将我的UserDAO注入Apache Shiro正在使用的自定义AuthorizingRealm中,但是......我得到了null.
我究竟做错了什么?
shiro.ini
[main]
user = demo.shiro.security.FacesAjaxAwareUserFilter
realmA = demo.shiro.security.JpaRealm
credentialsMatcher = org.apache.shiro.authc.credential.SimpleCredentialsMatcher
realmA.credentialsMatcher = $credentialsMatcher
securityManager.realms = $realmA
user.loginUrl = /pages/public/login.xhtml
[users]
admin = admin
user = user
[urls]
# public files and folders
/index.html = anon
/resources/** = anon
/pages/public/** = anon
# restricted files and folders
/pages/admin/** = user
/pages/user/** = user
Run Code Online (Sandbox Code Playgroud)
JpaRealm.java
public class JpaRealm extends AuthorizingRealm {
@Inject
private UserDao userDao;
public JpaRealm() {
setCredentialsMatcher(new Sha256CredentialsMatcher());
}
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authToken) throws AuthenticationException { …Run Code Online (Sandbox Code Playgroud) 我的印象是CDI没有使用具有@javax.faces.component.FacesComponent.的类.这是真的?
这是我的例子,这不起作用.将MyInjectableClass用于在在注射是没有问题的代码等穴位,所以它必须是有关@FacesComponent注释,我认为.
我要注入的课程:
@Named
@Stateful
public class MyInjectableClass implements Serializable {
private static final long serialVersionUID = 4556482219775071397L;
}
Run Code Online (Sandbox Code Playgroud)
使用该类的组件;
@FacesComponent(value = "mycomponents.mytag")
public class MyComponent extends UIComponentBase implements Serializable {
private static final long serialVersionUID = -5656806814384095309L;
@Inject
protected MyInjectableClass injectedInstance;
@Override
public void encodeBegin(FacesContext context) throws IOException {
/* injectedInstance is null here */
}
}
Run Code Online (Sandbox Code Playgroud) 我确实遇到了与此处所述相同的问题:JBoss7.1.3:@EJB在@Inject失败的情况下工作 可能是相同的设置(Wildfly 8.0).
尽管有一个带有生产者的Resource类,但我无法注入一个Logger实例.在Controller中Neiter,也不在EJB中.[问题1]如果我错了,请纠正我,我应该能够将它们注入@Model bean以及使用@Stateless(EJB)注释的bean,不应该吗?
package com.doe.webapp.service.auxilliary.cdiproducer;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
@Dependent
public class Resources {
@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
@Produces
@RequestScoped
public FacesContext produceFacesContext() {
return FacesContext.getCurrentInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器的开始:
package com.doe.webapp.controller.security;
import java.io.IOException;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;
import org.slf4j.Logger;
import …Run Code Online (Sandbox Code Playgroud) 我正在从JBoss Seam到CDI的迁移项目.以下是技术堆栈:
1)WildFly 8.2.0(CDI 1.2,Weld作为CDI提供商)
2)JSF 2.2
3)JPA 2
我们正在使用容器管理的JTA事务:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="surveillenace" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/surveillenaceDS</jta-data-source>
<!-- other configurations not shown here -->
</persistence>
Run Code Online (Sandbox Code Playgroud)
并使用@PersistenceContext注释将EntityManager注入DAO对象.
我们正在@Transaction为容器管理的事务使用注释.
我的问题/理解如下.
任何人都可以证实这一点,因为这对我来说是一个相对较新的领域.
1)据我所知,CDI通过@Transaction拦截器为CMT提供支持.哪个类/依赖实际上实现了这个拦截器?我们需要在pom.xml中为此导入哪些工件?
2)由于使用了CMT,我们不需要划分任何事务,容器将管理它.我们只需要使用EntityManager API来持久保存Db中的更改.这种理解是否正确?
@Transactional
public String finishOperation() {
log.debug("in finishOperation() ") ;
try {
//operations done on managed entities
//no transaction demarcation code is required here
dao.getEntityManager.commit();
}catch(Throwable xx){
}
}
Run Code Online (Sandbox Code Playgroud)
3)考虑使用上述配置执行的以下简单场景:
Component1.somemethod() - 在事务内部运行并持久保存实体(例如:用户)并提交事务.在此之后,如下调用Component2:
Component2.somemethod()- 在转换内运行,但实体User似乎不处于托管状态, em.contains(user) …
这是单元测试代码.当我们运行单元测试代码(SampleServiceTest2)时; 在AbstractDao中注入的EntityManager始终为null!我们如何在单元测试期间注入em.
***SampleServiceTest2.java
import javax.inject.Inject;
import org.jglue.cdiunit.CdiRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(CdiRunner.class)
public class SampleServiceTest2 {
@Inject SampleService greeter;
@Test
public void testGreeter() throws Exception {
System.out.println("before2");
greeter.addSampleData(new SampleDataDto(), new KullaniciDto());
System.out.println("after2");
}
}
Run Code Online (Sandbox Code Playgroud)
***SampleService.java
import javax.ejb.Stateless;
import javax.inject.Inject;
....
@Stateless
@SecuredBean
public class SampleService {
@Inject
SampleLogic sampleLogic;
@Yetki(tag="perm_add_sample_data")
public void addSampleData(SampleDataDto data, KullaniciDto aktifKullaniciDto){
SampleDataHelper sampleDataHelper = new SampleDataHelper();
SampleData sampleData = sampleDataHelper.getEntity(data);
KullaniciHelper kullaniciHelper = new KullaniciHelper();
Kullanici kullanici = kullaniciHelper.getEntity(aktifKullaniciDto);
sampleLogic.addData(sampleData, kullanici);
}
}
Run Code Online (Sandbox Code Playgroud)
****SampleLogic.java
import …Run Code Online (Sandbox Code Playgroud) 我建立的一个非常简单的项目的Hello World在
Payara 5(5.181)
JSF 2.3
JDK 1.8
CDI 2.0
Maven
遇到了问题
由于以下问题,无法启动服务器:启动过程失败,退出代码为1
在控制台中,它抛出错误:
错误:找不到或加载主类server \ payara5 \ glassfish.lib.grizzly-npn-bootstrap.jar