我正在Java EE中使用CDI,但是它似乎不起作用。
我有一个非常简单的示例,其中包含2个文本字段。输入名称并提交时,输入的名称应显示在下一页。
但是我收到以下错误:
HTTP Status 500 - Internal Server Error
javax.servlet.ServletException: /index.xhtml @12,72 value="#{userBean.firstName}": Target Unreachable, identifier 'userBean' resolved to null
javax.el.PropertyNotFoundException: /index.xhtml @12,72 value="#{userBean.firstName}": Target Unreachable, identifier 'userBean' resolved to null
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'userBean' resolved to null
这些是我的3个文件:
index.xhtml
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="First name" for="firstName"/>
<h:inputText id="firstName" value="#{userBean.firstName}"/>
<h:outputLabel value="Last name" for="lastName"/>
<h:inputText id="lastName" value="#{userBean.lastName}"/>
<h:commandButton action="result" value="send"/>
</h:panelGrid>
</h:form>
result.xhtml
First name: #{userBean.firstName}
Last name: #{userBean.lastName}
UserBean.java
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
public class UserBean {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}