记录保存后重置输入字段

时间:2013-07-24 11:43:03

标签: hibernate jsf

我想在记录保存后清除输入文本字段。但价值仍然存在。如何清除表单文本字段的值?请提前帮助谢谢。 我的sponsor.xhtml是

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
   <style type="text/css">
            .ui-growl{
                left:20px;
            }
        </style>

</h:head>
<body>
    <ui:composition template="#{localeControllerBean.path}">
    <ui:define name="head">
        <style>.ui-growl{ #{msg['msg']}: 20px; }</style>
        </ui:define>
        <ui:define name="content">

            <h:form enctype="multipart/form-data" dir="#{localeControllerBean.direction}" id="form1">
                <div id="add">
                <p:growl  id="msgs" autoUpdate="true" display="icon" style="left:20px"></p:growl>

                </div>
                <p:panel header="#{msg['sponsor_detail']}" style="font-size:small">
                    <h:panelGrid columns="2">
                <f:event listener="#{localeControllerBean.islang}" type="preRenderView" />

                        <p:outputLabel for="sname" value="#{msg['sponsor_name']}"
                            styleClass="label" />
                        <p:inputText id="sname" value="#{sponsorBean.sponsor_name}"
                            required="true" requiredMessage="#{msg['sponsor_name_msg']}"
                            styleClass="input">
                            <f:validator validatorId="sponsorValidator" />
                        </p:inputText>

                        <p:outputLabel for="sadd" value="#{msg['sponsoraddress']}:"
                            styleClass="label" />
                        <p:inputText id="sadd" value="#{sponsorBean.s_address}"
                            required="true" requiredMessage="#{msg['Sponsor_address_msg']}"
                            styleClass="input" />

                        <p:outputLabel for="smb" value="#{msg['sponsor_mbno']}:"
                            styleClass="label" />
                        <p:inputText id="smb" value="#{sponsorBean.s_mobile_no}"
                            required="true" requiredMessage="#{msg['sponsor_mbno_msg']}"
                            styleClass="input" />

                        <p:outputLabel for="ss" value="#{msg['sponsor_status']}:" styleClass="label" />
                        <p:inputText id="ss" value="#{sponsorBean.status}" required="true"
                            requiredMessage="#{msg['sponsor_mbno_msg']}" styleClass="input" />
                    </h:panelGrid>

                    <h:panelGrid columns="2" cellpadding="8" style="margin-left:6%">
                    <p:commandButton id="submit" value="#{msg['save']}" ajax="false"
                        action="#{sponsorBean.save}"  style=""
                        update="msgs" icon="ui-icon-disk"/>

                    <p:commandButton type="reset" value="#{msg['reset']}"
                        style="" ajax="false" icon="ui-icon-arrowrefresh-1-e" actionListener="#{sponsorBean.reset}/>
 </h:panelGrid>
                </p:panel>
            </h:form>
        </ui:define>
    </ui:composition>
</body>
</html>

和sponsorbeam.java文件是

package com.pms.main;

import java.util.List;
import java.util.Set;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.validation.ConstraintViolationException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.event.ColumnResizeEvent;
import pojo.TblSponsor;
import com.pms.hmb.Tbl_company;
import com.pms.hmb.Tbl_companydocs;
import com.pms.hmb.Tbl_employee;
import com.pms.hmb.Tbl_sponsor;
import com.pms.util.HibernateUtil;
@ManagedBean
@SessionScoped
public class SponsorBean {
    private int s_id;
    private String sponsor_name;
    private String s_address;
    private String s_mobile_no;
    private String status;
    private Set<Tbl_company> tbl_companies=null;
    Tbl_sponsor selectsponsor=new Tbl_sponsor();

 public Set<Tbl_company> getTbl_companies() {
        return tbl_companies;
    }
    public void setTbl_companies(Set<Tbl_company> tbl_companies) {
        this.tbl_companies = tbl_companies;
    }





    public Tbl_sponsor getSelectsponsor() {
        return selectsponsor;
    }
    public void setSelectsponsor(Tbl_sponsor selectsponsor) {
        this.selectsponsor = selectsponsor;
    }
    public int getS_id() {
        return s_id;
    }
    public void setS_id(int s_id) {
        this.s_id = s_id;
    }
    public String getSponsor_name() {
        return sponsor_name;
    }
    public void setSponsor_name(String sponsor_name) {
        this.sponsor_name = sponsor_name;
    }
    public String getS_address() {
        return s_address;
    }
    public void setS_address(String s_address) {
        this.s_address = s_address;
    }
    public String getS_mobile_no() {
        return s_mobile_no;
    }
    public void setS_mobile_no(String s_mobile_no) {
        this.s_mobile_no = s_mobile_no;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;

    }



    public void onResize(ColumnResizeEvent event) {  
        FacesMessage msg = new FacesMessage("Column " + event.getColumn().getClientId() + " resized", "W:" + event.getWidth() + ", H:" + event.getHeight());  

        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  

    public String save()
    {
    Tbl_sponsor tbl_sponsor=new Tbl_sponsor();
    tbl_sponsor.setName(this.sponsor_name);
    tbl_sponsor.setS_address(s_address);
    tbl_sponsor.setS_mobile_no(s_mobile_no);
    tbl_sponsor.setStatus(status);

//      tbl_company.setComapany(tbl_company);
        Tbl_company tbl_company1=new Tbl_company();
//      tbl_company1.setComapnyname("a");
//      tbl_company1.setAddress("b");
//      tbl_company1.setEmail("c");
//      tbl_company1.setMobileNumber("66");
//      tbl_company1.setLogoAlt(name);
//      tbl_company1.setImageType(type);
//      tbl_company1.setLogo(byteslogo);
//      tbl_company1.setEmail(email);
//      tbl_company1.setComapany(tbl_company1);
//      tbl_company.setComapany(null);
//tbl_company.setComapany(tbl_company1);

//  tbl_company.setTbl_companies(tbl_company);
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null;

        try {
            tx = session.beginTransaction();
            session.save(tbl_company1);
            session.save(tbl_sponsor);

//          session.save(tblemployee);
//          session.save(tblikama);
            tx.commit();
            FacesContext context = FacesContext.getCurrentInstance();
            String value = context.getApplication().evaluateExpressionGet(context, "#{msg['sponsor_added']}", String.class);

            context.addMessage(null, new FacesMessage(value));

        }
        catch(ConstraintViolationException e)
        {
//  logger.error("error code 409 while creating employee:Constraintvoilation Exception:"+e.getMessage());
            e.printStackTrace();

        }
        catch(Exception e)
        {
//          logger.error("error code 500 while creating employee: Exception:"+e.getMessage());
            e.printStackTrace();


        }
        finally
        {

            session.close();

        }

        return "";
    }

2 个答案:

答案 0 :(得分:-1)

保存后尝试清除String对象的所有SponsorBean字段:

save方法结束时:

    ...
    this.sponsor_name = null;
    this.s_address = null;
    this.s_mobile_no = null;
    return "";
}

您是否考虑使用请求或会话范围的bean而不是会话作用域?他们可能会解决您的问题。

答案 1 :(得分:-1)

@SessionScoped bean通过用户会话存储其数据,这就是在页面中看到会话数据成员的值的原因。

应在操作方法中将数据成员设置为其默认值,以便再次在页面中看不到提交的值。

this.s_id = 0;
this.sponsor_name = null;
this.s_address = null;
this.s_mobile_no = null;
this.status = null;
return "";

或者您可以将托管bean的范围更改为@RequestScoped@RequestScoped bean是根据每个请求从淀粉中创建的。

此外,访问托管bean中的数据库不是一个好习惯。您应该根据原则low coupling high cohesion

将数据访问权限与托管bean分开

另见

DAO tutorial - the data layer