0

我想在记录保存后清除输入文本字段。但价值观依然存在。如何清除表单文本字段的值?请帮助提前感谢。我的赞助商.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>

并且赞助商光束.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 "";
    }
4

2 回答 2

-1

@SessionScoped beans 通过用户会话存储其数据,这就是为什么在页面中可以看到会话数据成员的值。

数据成员应该在 action 方法中设置为默认值,这样提交的值就不会再次出现在页面中。

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 中访问数据库不是一个好习惯。您应该将数据访问与托管 bean 分开,以实现 低耦合 高内聚原则

也可以看看

DAO 教程 - 数据层

于 2013-07-24T12:03:00.340 回答
-1

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

save方法结束时:

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

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

于 2013-07-24T11:49:56.080 回答