我在访问 JSF 页面中的托管 bean 时遇到问题。这是我的jsf:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
template="./template.xhtml">
<ui:define name="left">
<h:form>
<h:inputText value="#{UserManager.user.username}" p:placeholder="Username" /><br /><br />
<h:inputSecret value="#{UserManager.user.password}" p:placeholder="Password" /><br /><br />
<h:inputText value="#{UserManager.user.firstname}" p:placeholder="Förnamn" /><br /><br />
<h:inputText value="#{UserManager.user.lastname}" p:placeholder="Efternamn" /><br /><br />
<h:inputText value="#{UserManager.user.phonenumber}" p:placeholder="Telefonnummer" /><br /><br />
<h:commandButton value="Register" action="#{UserManager.save}" />
</h:form>
<br /><br />
<h:form>
<h:commandLink action="#{UserManager.testDispatch}" value="Test Dispatch" />
</h:form>
</ui:define>
<ui:define name="right">
right
</ui:define>
这是模板:
<?xml version='1.0' encoding='UTF-8' ?>
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h:outputStylesheet library="css" name="bootstrap.min.css"/>
<h:outputStylesheet library="css" name="bootstrap-theme.min.css"/>
<h:outputStylesheet library="css" name="jumbotron-narrow.css" />
<title>Java EE Övning 4 Sam Gholizadeh</title>
</h:head>
<h:body>
<div class="container">
<div class="header">
<ul class="nav nav-pills pull-right">
<li class="active"><h:link value="Tweet" outcome="index" /></li>
<li>
<c:choose>
<c:when test="#{UserManager.user.userid > 0}">
<!-- Split button -->
<div class="btn-group">
<h:link class="btn" value="#{UserManager.user.username}" outcome="settings" />
<button type="button" class="btn btn-danger">Action</button>
<button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</div>
</c:when>
<c:otherwise>
<h:link value="Registrera" outcome="register" />
</c:otherwise>
</c:choose>
</li>
</ul>
<h3>Uppgift 4</h3>
</div>
<div class="row marketing">
<div class="col-lg-6">
<ui:insert name="left" />
</div>
<div class="col-lg-6">
<ui:insert name="right" />
</div>
</div>
<div class="footer">
<p>Sam Gholizadeh</p>
</div>
</div>
<h:outputScript name="resources/js/bootstrap.min.js" />
</h:body>
这是托管 bean:
@ManagedBean
@SessionScoped
public class UserManager implements Serializable {
private User user;
private FacesContext context;
@EJB
private UserService UserService;
@PostConstruct
public void init(){
user = new User();
}
public void Save(){
UserService.create(user);
}
public void testDispatch() throws IOException{
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}
public void logout() throws IOException{
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.invalidateSession();
ec.redirect(ec.getRequestContextPath() + "/index.xhtml");
}
}
我得到的错误信息是:
javax.el.PropertyNotFoundException: /register.xhtml @21,89 action="#{UserManager.testDispatch}": 目标不可达,标识符'UserManager'解析为空
没有 faces-config.xml 所以托管 bean 名称应该默认为类名?