如何从jsf表单中获取“#{readeredit.reader.tel}”的值?

时间:2016-05-17 15:23:49

标签: forms jsf jpa jsf-2

我使用NetBeans工具从实体生成一个jsf表单(coures,我正在使用JPA)。我想从jsf页面获取表单数据,因为我不知道如何获取“#{的值readeredit.reader.tel}“。我的目标是获取编辑数据并从表单中发布。我从get方法中获取默认值。但是我发现我无法从表单中获取值。 页面xhtml代码:

<h:body>
        <f:view>
            <h:form  >
                <h1><h:outputText value="读者信息编辑"/></h1>
                <h:panelGrid columns="2" > 
                    <h:outputLabel value="姓名:" for="name" />
                    <h:inputText id="name" value="#{readeredit.reader.name}" title="Name" />
                    <h:outputLabel value="性别:" for="sex" />
                    <h:inputText id="sex" value="#{readeredit.reader.sex}" title="Sex" />
                    <h:outputLabel value="学号:" for="barcode" />
                    <h:inputText id="barcode" value="#{readeredit.reader.barcode}" title="Barcode" />
                    <h:outputLabel value="密码:" for="pwd" />
                    <h:inputText id="pwd" value="#{readeredit.reader.pwd}" title="Pwd" />
                    <h:outputLabel value="职位:" for="vocation" />
                    <h:inputText id="vocation" value="#{readeredit.reader.vocation}" title="Vocation" />
                    <h:outputLabel value="生日:" for="birthday" />
                    <h:inputText id="birthday" value="#{readeredit.reader.birthday}" title="Birthday" >
                        <f:convertDateTime pattern="yyyy-MM-dd" />
                    </h:inputText>
                    <h:outputLabel value="电话:" for="tel" />
                    <h:inputText id="tel" value="#{readeredit.reader.tel}" title="Tel" />
                    <h:outputLabel value="Email:" for="email" />
                    <h:inputText id="email" value="#{readeredit.reader.email}" title="Email" />
                    <h:outputLabel value="备注:" for="remark" />
                    <h:inputTextarea rows="4" cols="30" id="remark" value="#{readeredit.reader.remark}" title="Remark" />
                </h:panelGrid>
                    <h:commandButton value="保存" action="#{readeredit.edit()}" />
            </h:form>
        </f:view>

    </h:body>

生成jsf页面需要一个返回实体的方法。所以我写这个方法, 代码在这里:

public TbReader getReader() {
    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tid");
    if (id != null && !id.isEmpty()) {
        this.typeid = id;
    }

reader = uecon.findTbReader(Integer.parseInt(typeid.trim()));
return reader;

}

我的jsf托管bean的整个代码在这里:

@Named(value = "readeredit")
@SessionScoped
public class readeredit implements Serializable {

    @Resource
    UserTransaction utx;
    @PersistenceUnit(unitName = "BMsPU")
    private EntityManagerFactory emf;
    TbReaderJpaController uecon;
    private String typeid;
    private TbReader reader;

    public TbReader setReader(TbReader reader) {
        this.reader = reader;
        return reader;
    }

    public String getTypeid() {
        return typeid;
    }

    public void setTypeid(String typeid) {
        this.typeid = typeid;
    }

    @PostConstruct
    public void init() {
        uecon = new TbReaderJpaController(utx, emf);
        this.setTypeid(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tid"));
    }

    public TbReader getReader() {
        String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tid");
        if (id != null && !id.isEmpty()) {
            this.typeid = id;
        }
        reader = uecon.findTbReader(Integer.parseInt(typeid.trim()));
        return reader;
    }
    public String edit(TbReader rd) throws Exception {
        String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tid");
        if (id != null && !id.isEmpty()) {
            this.typeid = id;
        }
        try {
            TbReader editReader = new TbReader();
           editReader= this.setReader(getReader());
            editReader.setId(Integer.parseInt(typeid.trim()));
            uecon.edit(editReader);
            FacesMessage message = new FacesMessage("保存成功!");
            FacesContext.getCurrentInstance().addMessage(null, message);
        } catch (Exception e) {
            FacesMessage message = new FacesMessage("保存失败!");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
        return "reader";
    }
    public readeredit() {
    }

}

当我芬兰语编辑表单数据时,我几乎找不到获取表单值的方法。现在我很有兴趣.Plz告诉我。

0 个答案:

没有答案