我正在尝试在JBoss Wildfly中运行JSF演示。它来自Wildfly的快速入门演示。这里有两个文件,CreateController.java和JSF文件。我收到了这个错误:
Context Path:/wildfly-greeter
Servlet Path:/faces
Path Info:/create.xhtml
Query String:null
Stack Trace
javax.servlet.ServletException: /create.xhtml @29,75 value="#{newdUser.username}": Target Unreachable, identifier 'newdUser' resolved to null
javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
为什么生产者领域" newUser"无法到达?我没有在演示中改变任何东西。
@Named
@RequestScoped
public class CreateController {
@Inject
private FacesContext facesContext;
@Inject
private UserDao userDao;
@Named
@Produces
@RequestScoped
private User newUser = new User();
public void create() {
try {
userDao.createUser(newUser);
String message = "A new user with id " + newUser.getId() + " has been created successfully";
facesContext.addMessage(null, new FacesMessage(message));
} catch (Exception e) {
String message = "An error has occured while creating the user (see log for details)";
facesContext.addMessage(null, new FacesMessage(message));
}
}
}
CreateControl.xhtml
<h:panelGrid columns="3">
<h:outputLabel for="username">Enter username:</h:outputLabel>
<h:inputText id="username" value="#{newdUser.username}" />
<h:message for="username" />
</h:panelGrid>
<h:commandButton action="#{createController.create}"
value="Add User" />
答案 0 :(得分:0)
好像你不小心把一个&#39; d&#39;在
value="#{newdUser.username}"
而不是
value="#{newUser.username}"
它不在Github的来源。
IMO你可以简化代码
@Named
@RequestScoped
public class CreateController {
@Inject
private UserDao userDao;
private User newUser = new User(); // +getter/setter
public void create() {
try {
userDao.createUser(newUser);
String message = "A new user with id " + newUser.getId() + " has been created successfully";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message));
} catch (Exception e) {
String message = "An error has occured while creating the user (see log for details)";
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message));
}
}
}
和
<h:panelGrid columns="3">
<h:outputLabel for="username">Enter username:</h:outputLabel>
<h:inputText id="username" value="#{createController.newUser.username}" />
<h:message for="username" />
</h:panelGrid>
<h:commandButton action="#{createController.create}" value="Add User" />
希望它有效。