我有一个(请求范围的)列表,用户可以从中选择"PQ"(链接列表).单击或以其他方式输入浏览器时,将显示每个PQ的主页面.每个PQ的页面都是表格
http://localhost:8080/projectname/main.jsf?id=2
这是PQ bean的第一个:
@Named
@ViewScoped
public class PqHome implements Serializable
{
@PersistenceContext(unitName="...")
private EntityManager em;
private Integer id;
private PQ instance;
@PostConstruct
public void init()
{
System.out.println("ID is " + id); // ID from URL param
instance = em.find(PQ.class, id);
}
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public PQ getInstance()
{
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
这是main.xhtml:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
...>
<ui:define name="metadata">
<f:metadata>
<f:viewParam name="id" value="#{pqHome.id}">
<f:convertNumber integerOnly="#{true}" />
</f:viewParam> …Run Code Online (Sandbox Code Playgroud) 我有一个CDI bean,我正在使用@ConversationScoped.当我尝试为Conversation对象执行@Inject时,我得到一个NPE.
@ConversationScoped
@Named("customerbean")
public class CustomerBean implements Serializable {
@Inject
private Conversation conversation;
private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("ba");
private EntityManager em;
private Customer customer;
boolean disabled;
public CustomerBean() {
beginConversation();
customer = new Customer();
em = emf.createEntityManager();
disabled = false;
}
private void beginConversation() {
if (this.conversation.isTransient()) {
this.conversation.begin();
return;
}
throw new IllegalStateException();
}
Run Code Online (Sandbox Code Playgroud)
我在WEB-INF目录中有beans.xml文件(尽管是空的).例外情况如下:
INFO: Exception when handling error trying to reset the response.
com.google.common.collect.ComputationException: java.lang.RuntimeException: java
.lang.NullPointerException
at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdap
ter.get(ComputingConcurrentHashMap.java:397)
at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientPr
oxyProvider.java:102)
at …Run Code Online (Sandbox Code Playgroud) 我在 JBoss AS 7.1.1 上使用 JSF 2.1.7 和 Myfaces CODI 1.0.5。我<h:commandButton>的不工作。我已经阅读了要求并通过许多博客中的示例都无济于事。我的facelets代码如下
<ui:define name="pagecontent">
<h1 class="title ui-widget-header ui-corner-all">Upload Bulk Contact File</h1>
<div class="entry">
<h:form enctype="multipart/form-data" id="upload">
<p:panel closable="false" collapsed="false" header="Excel Contact Uploader"
id="pnlupload" rendered="true" toggleable="false" visible="true" widgetVar="pnlupload">
<p:growl id="msg" showDetail="true" life="3000" showSummary="true"/>
<p:fileUpload auto="true"
allowTypes="/(\.|\/)(xls)$/"
sizeLimit="1024000"
mode="advanced"
multiple="true" invalidFileMessage="Invalid file type" invalidSizeMessage="File too
large" dragDropSupport="true"
fileUploadListener="#{excelFileController.handleFileUpload}" showButtons="true"
update="msg, tblcontacts" required="false"/>
<p:scrollPanel rendered="true" style="height:200px;">
<p:dataTable draggableColumns="false" editable="false" emptyMessage="No
Contacts Uploaded" id="tblcontacts" rendered="true" rows="8"
selection="#{excelFileController.contactsSelected}"
value="#{excelFileController.contactDataModel}" var="contact" style="width:50pc;">
<p:column selectionMode="multiple" style="width:18px" …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ChartBeanPrimeFaces 的样本.这是观点:
<h:form>
<p:layoutUnit position="center">
<p:lineChart id="linear" value="#{chartBean.linearModel}" legendPosition="e"
title="Linear Chart" minY="0" maxY="1000" style="height:600px"/>
</p:layoutUnit>
</h:form>
Run Code Online (Sandbox Code Playgroud)
这是豆子:
@Named
@RequestScoped
public class ChartBean implements Serializable {
private CartesianChartModel categoryModel;
private CartesianChartModel linearModel;
public ChartBean() {
System.out.println("ChartBean constructed");
createCategoryModel();
createLinearModel();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我注意到在打开页面时调用了这个bean的构造函数两次.日志显示以下内容:
INFO:ChartBean构造了
INFO:ChartBean
所以bean被实例化了两次.这是怎么造成的,我怎么能避免这种情况?我正在与数据库进行交互,以便在UI中显示一些数据,这样就不必两次获取数据.
这可能吗:
@Inject
@MessageTransport(MessageTransportType.SMS)
public static MessageSender messageSender;
Run Code Online (Sandbox Code Playgroud)
当我试图访问这个静态变量时,我得到了一个NPE.所以我想知道,如果一般情况下不可能的话.
提前致谢.
我想为cdi编写测试用例.我在我的dao中使用了@inject.任何人都可以帮我如何为cdi编写测试用例.我尝试了下面的代码.但它不起作用.请帮我.
public class StudentTest {
StudentService stuService;
StudentDAO stuDAO;
StudentVO stuVo;
@Before
public void setUp() throws Exception {
System.out.println("In Setup");
stuVo=new StudentVO ();
stuService=new StudentService();
stuDAO=Mockito.mock(StudentDAO.class);
stuVo.setStudId("123");
stuVo.setName("user1");
Mockito.when(stuDAO.getStudent(stuVo.getStuId())).thenReturn(student);
}
@Test
public void getStudent() throws DataAccessException {
StudentVO stVO=stuService.getStudent(123);
Assert.assertEquals("123", stVO.getStuId());
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务类是
public class StudentService {
@Inject
StudentDAO stuDao;
public StudentVo getStudent(String id){
return stuDao.getStudent(id);
}
}
Run Code Online (Sandbox Code Playgroud)
在失败时跟踪它只是显示为"java.lang.NullPointerException
at com.stu.StudentService.getStudent(StudentService.java:104)
at com.stu.junit.POCJunit.getgetStudent(StudentTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at …Run Code Online (Sandbox Code Playgroud) 将@Transactional与EJB 结合使用是否可以?使用@TransactionAttribute之间有什么区别吗?
我在这里找到了@Transactional和@TransactionAttribute之间的区别 @Transactional用于POJO.我在Wildfly中意外地将它用于EJB,并且没有错误.我想知道行为是否相同或者是否存在一些差异.或者@Transactional可能会被忽略,并且会使用@TransactionAttribute的默认级别?我在规范中搜索但没有发现任何帮助.
编辑:
正如@kostja所说,不清楚提到的框架问题是什么.我的意思是来自CDI的@Transactional,而不是Spring.
我在部署两个无状态会话bean时遇到了部署错误,每个会话bean都依赖于另一个并使用@Inject.
@Stateless
class BeanA {
@Inject
BeanB b;
public void doSomething() {
b.doSomething();
}
}
@Stateless
class BeanB {
@Inject
BeanA a;
public void doSomeOtherThing() {
a.doSomeOtherThing();
}
}
Run Code Online (Sandbox Code Playgroud)
在部署时,我从Glassfish/weld获得此异常:
org.jboss.weld.exceptions.DeploymentException:WELD-001443伪范围bean具有循环依赖关系.
用@EJB而不是@Inject注入Beans,一切正常.现在我有两个问题.
首先 - 焊接内部会发生什么,这是不允许的?
第二个(可能更重要) - 这是建筑方面的不良做法,如果是,你知道任何避免它的模式吗?根据我目前的知识,允许同一层上的业务驱动服务以他们需要的任何方式相互通信.
我正在尝试使用和理解CDI,当我在简单的pojo类中使用@Inject时,它会抛出NPE。
示例Greeting.java
public Class Greeting {
public String greet() {
System.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
Test.java
import javax.inject.Inject;
public class Test {
@Inject
private Greeting greeting;
public void testGreet() {
greeting.testGreet();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用testGreet()时,它抛出NPE,为什么问候实例为null。@Inject添加依赖项的方式仅在容器管理的Bean中使用吗?注意:这里不是jar的问题。
Java EE 规范的 Servlet、EJB 和 CDI 部分描述了 bean 管理(注入等)。但是规范也没有描述(显式或隐式)bean 容器的并发属性。如果我使用了 WildFly 或 GlassFish,那么 Java EE 应用服务器可以从一个线程访问更多的 bean 对象。
所以问题是,我是否必须对volatile所有 bean 字段使用关键字,理论上可以从多个线程访问(即对于几乎所有 bean,至少对于所有用 注释的字段@Inject)?
cdi ×10
java ×5
ejb ×3
java-ee ×3
javabeans ×2
jsf ×2
jsf-2 ×2
primefaces ×2
constructor ×1
glassfish-4 ×1
jakarta-ee ×1
java-ee-6 ×1
junit ×1
junit4 ×1
scopes ×1
volatile ×1