表单提交不会在带有请求参数的JSF 1.2页面上的ManagedBean上触发操作方法 [英] Form submit does not trigger action method on managedBean on a JSF 1.2 page with request parameters

查看:152
本文介绍了表单提交不会在带有请求参数的JSF 1.2页面上的ManagedBean上触发操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的请求,它的作用域是具有各种方法和属性的JSF 1.2托管bean.一些属性被映射到URL请求参数,以便它可以根据URL中的id属性填充实体对象.

I have a quite complicated request scoped JSF 1.2 managed bean with various methods and properties. Some of the properties are mapped to URL request parameters so that it can populate an entity object according to the id attribute in the URL.

我使用同一个托管bean,既使用一个jsf页面为该实体创建了一条新记录,又使用了另一个用于更新几个字段的记录.在创建模式下,没有url参数,使用指定的action方法提交表单就可以执行该操作.

I use the same managed bean both to create a new record for that entity using one jsf page, and another one for updating a couple of fields. In create mode, there are no url parameters, and submitting the form using an specifying an action method just does what it's told and executes the method.

但是在更新模式下,从请求参数(在URL中)获取实体的ID(使用从数据库中提取的实体数据来完美填充表格)时,提交按钮(即)不会在同一参数上调用action方法托管Bean,但是由于url中没有id,因此这次重新加载了url参数并且表单为空白的页面,通过服务类方法引用相应数据以设置实体对象时使用的id属性为空.

But when in update mode, getting the entity's id from the request parameter (in the URL) which populates the form perfectly with the entity data pulled from the database, the submit button i.e. the does not invoke the action method on the same managed bean, but reloads the page with the url parameters gone and with the form blank this time since there's no id in the url, the id property which is used when referring to the corresponding data by a service class method to set the entity object is null.

那么我如何在调用此更新模式时执行该方法?

So how would I get that method executed in this update-mode as I call it?

在听取建议后,我还包含了一些隐藏字段,这些字段包含从更新表单中的URL获得的id参数的值,

Listening to an advice, I had also included hidden fields which contain the values of id parameters obtained from the URL in the update form, which didn't seem to work.

这是有问题的JSF页面中包含表单的代码片段:

Here's the code fragment containing the form in the problematic JSF page:

<h:form id="ostOnayForm">
    <h:messages style="color:red" />

        <h:inputHidden value="#{oduncStokTalepBean.viewID}" />
        <h:inputHidden value="#{oduncStokTalepBean.adimID}" />

        <h:panelGrid id="oracleERP" columns="3"
                rendered="#{oduncStokTalepBean.aksiyon.faz==3}">
                Personel ödünç cari kodu:

        <h:inputText id="carikod" value="#{oduncStokTalepBean.oduncCariKod}"
                    required="true"
                    requiredMessage="Bu personelin 'ödünç cari kodu'nu girmelisiniz. (Eğer böyle bir kod tanımlı değilse, önce tanımı yapılmalıdır."
                    validatorMessage="Bu alana en fazla 25 karakter girebilirsiniz.">
                 <f:validateLength maximum="25" />
        </h:inputText>
        <h:message for="carikod" style="color:red" />
        </h:panelGrid>

        <h:panelGroup id="depocu1"
                rendered="#{oduncStokTalepBean.aksiyon.faz==2}">
            Personel ödünç cari kodu:
            <h:inputText value="#{oduncStokTalepBean.oduncCariKod}"
                    required="false"
                    validatorMessage="Bu alana en fazla 25 karakter girebilirsiniz.">
                <f:validateLength maximum="25" />
            </h:inputText>
        </h:panelGroup>

        <h:panelGroup id="depocu2"
                rendered="#{oduncStokTalepBean.aksiyon.faz==2}">
                <h:selectBooleanCheckbox value="#{oduncStokTalepBean.kargoyla}" />Ürün kargo firması ile teslim edilecek
        </h:panelGroup>

        <h:panelGrid columns="3"
                rendered="#{oduncStokTalepBean.onaylamaYetkisi}">
                Görüşler:
        <h:inputTextarea id="gorus" value="#{oduncStokTalepBean.gorus}"
                    required="true"
                    validatorMessage="Bu alana en fazla 255 karakter girebilirsiniz."
                    requiredMessage="Lütfen görüş de bildirin.">
        <f:validateLength maximum="255" />
        </h:inputTextarea>
        <h:message for="gorus" style="color:red" />

        <h:commandButton action="#{oduncStokTalepBean.vazgec}" value="Vazgeç" />

        <h:commandButton type="submit" action="#{oduncStokTalepBean.onayla}"
                    value="Onayla" rendered="#{oduncStokTalepBean.onaylamaYetkisi}" />
        <h:commandButton action="#{oduncStokTalepBean.reddet}"
                    value="Reddet"
                    onclick="return confirm('Bu formu reddetmek istediğinizden emin misiniz?')"
                    rendered="#{oduncStokTalepBean.onaylamaYetkisi}" />

        </h:panelGrid>
    </h:form>

后备豆:

public class OduncStokTalepBean extends SurecBean {

    private String oduncCariKod;
    private Boolean kargoyla;

    private OduncStokTalep oduncStokTalep = new OduncStokTalep("ost", "Ödünç Stok Talebi");
    private List<SelectItem> depoListesi = new ArrayList<SelectItem>();
    private OduncStokAdres osa = new OduncStokAdres();

    private OduncStokAdresJpaController osaServ = new OduncStokAdresJpaController();

    public OduncStokTalepBean() {
        super();
    }

    @PostConstruct
    public void initializeOST() {
        if (FacesUtil.getSessionAttribute("GO_Person_id") != null ) {
            GO_Person_id = Integer.valueOf((String) FacesUtil.getSessionAttribute("GO_Person_id"));
            if (viewID != null) {

                System.out.println("ostBean got viewID:"+viewID);
                this.oduncStokTalep = ostServ.findOduncStokTalep(viewID);
                this.editModu = true;
                this.oduncCariKod = this.oduncStokTalep.getPersonelOduncCariKodu();
                this.kargoyla = this.oduncStokTalep.getKargoylaTeslim();
                System.out.println("ost: "+oduncStokTalep.getId()+" * "+oduncStokTalep.toString());
                if (adimID != null) {
                    this.aksiyon = aServ.findAkisAdim(adimID);
                    this.setAksiyonModu(true);
                } else {
                    System.out.println("adimID was null, reading it from Surec...");
                    if (oduncStokTalep.getPendingAction()!=null) {
                        this.aksiyon = aServ.findAkisAdim(oduncStokTalep.getPendingAction());
                        System.out.println("Found :"+aksiyon.getId()+" "+aksiyon.getAktor()+aksiyon.getAdimTanim());
                        this.setAksiyonModu(true);
                    } else {
                        System.out.println("oduncStokTalep.getPendingAction() seems also null :"+oduncStokTalep.getPendingAction());
                        this.setAksiyonModu(false);
                    }
                }

                if (editModu && GO_Person_id!=null) {

                    if (oduncStokTalep.getSuAnKimde().contains(GO_Person_id.toString()) )
                        this.onaylamaYetkisi = true;

                    if (this.aksiyon != null && this.aksiyon.getAktor().contains(GO_Person_id.toString())) 
                        this.onaylamaYetkisi = true;
                }

            } else {
                System.out.println("viewID is null.");
                System.out.println("initializing OST...");
                this.editModu = false;
                this.setAksiyonModu(false);

                oduncStokTalep.setPersonId(GO_Person_id);        
            }
        } else oturumActirt();
    }

    // THE FOLLOWING METHOD IS EXECUTED PERFCETLY WITH A BLANK FORM AND WITH NO GET PARAMETER
    public String kaydet(){
        Long formId;
        System.out.println("invoking ost kaydet()...");

        ostServ.doPersist(oduncStokTalep);

        formId = oduncStokTalep.getId();
        System.out.println("OST MB obtained Id:"+formId);
        if (formId != null) {
            doSomeOtherStuff();
        }

        FacesUtil.setSessionAttribute("surecView", null);
        FacesUtil.setSessionAttribute("surecAdim", null);
        FacesUtil.setSessionAttribute("surecID", null);

        FacesContext fc = FacesUtil.getFacesContext();
        fc.addMessage(null, new FacesMessage("Ödünç Stok talebiniz başarıyla oluşturuldu ve "+formId+" no'lu ile sisteme kaydedildi."));

        oduncStokTalep = new OduncStokTalep("ost", "Ödünç Stok Talebi");

        return "KAYDETVEGONDER";
    }

    // This method is never even called form the update form :(((((
    public String onayla() throws NonexistentEntityException, Exception {
        System.out.println("Onayla() invoked for OST "+oduncStokTalep.getId());
        if (this.onaylamaYetkisi) {         
            System.out.println("onaylama yetkisi de var.");

            SomeActionController ac1 = new SomeActionController();

            if (aksiyon == null) 
                    aksiyon = ac1.getPendingAction("ost", this.viewID, String.valueOf(this.GO_Person_id));

            ac1.adimiTamamla(this.GO_Person_id, "ost", this.viewID, aksiyon.getId(), true, this.gorus);
        }

        oduncStokTalep = new OduncStokTalep("ost", "Ödünç Stok Talebi");

        return "DASHBOARD";
    }

    // Neither this one via <h:commandButton action="#{oduncStokTalepBean.test}" value="Test Action Method" />
    public void test() {
        System.out.println("Test OK");
    }

    // getters and setters, etc.

}

和faces-config片段:

and the faces-config fragment:

<managed-bean>
  <managed-bean-name>surecBean</managed-bean-name>
  <managed-bean-class>net.ozar.wf.jsfmanaged.SurecBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>viewID</property-name>
   <property-class>java.lang.Long</property-class>
   <value>#{param.id}</value>
  </managed-property>
  <managed-property>
   <property-name>adimID</property-name>
   <property-class>java.lang.Long</property-class>
   <value>#{param.aid}</value>
  </managed-property>
  <managed-property>
   <property-name>surecID</property-name>
   <property-class>java.lang.String</property-class>
   <value>#{param.sid}</value>
  </managed-property>
 </managed-bean>
 <managed-bean>
  <managed-bean-name>oduncStokTalepBean</managed-bean-name>
  <managed-bean-class>net.ozar.wf.jsfmanaged.OduncStokTalepBean</managed-bean-class>
  <managed-bean-scope>request</managed-bean-scope>
  <managed-property>
   <property-name>viewID</property-name>
   <property-class>java.lang.Long</property-class>
   <value>#{param.id}</value>
  </managed-property>
  <managed-property>
   <property-name>adimID</property-name>
   <property-class>java.lang.Long</property-class>
   <value>#{param.aid}</value>
  </managed-property>
 </managed-bean>

推荐答案

您正在使用rendered属性来切换组件的可见性.与请求范围的Bean一起使用时,您需要确保在处理表单提交时,与显示表单时一样,对rendered属性的相同条件进行了评估.当按钮或其父组件之一的rendered属性的条件评估为false时,JSF将不会调用该按钮.

You're using the rendered attribute to toggle the visibility of the components. When using this with a request scoped bean, you need to ensure that the same condition for the rendered attributes is been evaluated during processing the form submit as it was during displaying the form. When the condition of the rendered attribute of the button or one of its parent components evaluates false, then JSF won't invoke the button.

如果不能确保在bean的(后期)构造期间可以保留相同的条件,则需要将bean放入视图范围(仅JSF 2.0),或使用Tomahawk的<t:saveState>将bean保留在视图范围中.后续请求.

If you cannot ensure that the same condition can be preserved during bean's (post)construction, then you need to put the bean in the view scope (JSF 2.0 only) or to use Tomahawk's <t:saveState> to retain the bean in the subsequent request.

  • How to call an action method of a UICommand Component which was rendered conditionally?
  • JSF 1.2: How to keep request scoped managed bean alive across postbacks on same view?

这篇关于表单提交不会在带有请求参数的JSF 1.2页面上的ManagedBean上触发操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆