由于某种原因,我的 xhtml 页面找不到我的 CDI 托管 bean。例如,我已经看到了很多关于这个问题的资源(识别和解决 javax.el.PropertyNotFoundException: Target Unreachable),但我仍然没有解决这个问题。
这是我要注入的 Bean。
import java.io.Serializable;
import javax.faces.bean.ApplicationScoped;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@SessionScoped
@Named
public class MyMB implements Serializable{
private Integer max;
private static final long serialVersionUID = 1L;
public MyMB(){
super();
max = 1;
}
public void setMax(Integer max) {
this.max = max;
}
public Integer getMax(){
return this.max;
}
public String priority(){
return "priority";
}
}
这就是我要注入它的地方。
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@SessionScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
String name = "myBean";
@Inject
private MyMB myMB;
MyMB getMyMB(){
return myMB;
}
}
这是我的 xhtml 页面。
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<h:outputStylesheet library="css" name="todo.css" />
<title>bean settings</title>
</h:head>
<body>
<h:outputText value="Edit myBean"/>
ManagedBean is: #{myBean.name}
<h:form>
<h:inputText value="#{myBean.myMB.max}"
converterMessage="Only numbers please" />
<h:commandButton value="Speichern" type="submit" action="#{myBean.myMB.priority}" />
</h:form>
</body>
</html>
由于某种原因,xhtml 页面似乎找不到 myBean 类,我收到此错误消息。
/myPage.xhtml @20,105 value="#{myBean.myMB.max}":目标无法到达,标识符“myBean”解析为空