我正在为我的 Web 应用程序使用 JSF 和 CDI。我创建了一个 JSF 表单和一个 RequestScoped bean 来保存表单数据。然后我创建了一个 CDI bean 来处理表单。这是表格:
<h:outputText value="Mailing Address: " />
<p:inputText value="#{profile.mailAddress}" />
<h:outputText value="Cell Phone: " />
<p:inputText value="#{profile.cellPhone}"/>
<h:outputText value="Home Phone: " />
<p:inputText value="#{profile.homePhone}"/>
<h:outputText value="Work Phone: " />
<p:inputText value="#{profile.workPhone}"/>
<p:commandButton action="#{profileController.test}" value="submit" ajax="false" />
这是请求范围的 bean:
package entities;
import java.io.Serializable;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author G2
*/
@Named
@RequestScoped
@XmlRootElement
public class Profile implements Serializable{
private String mailAddress;
private String homePhone;
private String workPhone;
private String cellPhone;
public String getMailAddress() {
return mailAddress;
}
@XmlElement
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getWorkPhone() {
return workPhone;
}
@XmlElement
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
public String getCellPhone() {
return cellPhone;
}
@XmlElement
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
}
这是 CDI bean:
package jsf.classes;
import entities.Profile;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
/**
*
* @author yg
*/
@Named
@ConversationScoped
public class ProfileController implements Serializable {
@Inject
private Profile current;
@Inject
private Conversation conversation;
public Profile getSelected() {
return current;
}
public void setSelected(Profile profile) {
this.current = profile;
}
public void beginConversation() {
if (conversation.isTransient()) {
conversation.begin();
}
}
public void endConversation() {
if (!conversation.isTransient()) {
conversation.end();
}
}
@PostConstruct
public void init() {
beginConversation();
}
}
但是,当我在表单中输入值并单击提交时,profileController.current 中的所有属性都为空。如何让profileController接收我通过表单输入的数据?