【问题标题】:@RequestScoped Bean not working as expected? (JSF)@RequestScoped Bean 没有按预期工作? (JSF)
【发布时间】:2017-04-18 08:36:33
【问题描述】:

我有一个 JSF 页面,我可以在其中添加或删除我的数据库 (JBDC) 中的“飞机”。为了帮助解决这个问题,我有两个 bean(两个 @RequestScoped),一个“控制器”和一个“支持 bean”。

这里是 JSF 页面:

还有一些输入:

输入信息并添加照片后,“飞机”被添加到我的页面,我重新加载页面。

这是提交后页面的外观:

我的问题是,输入文本字段从 bean 中“填充”了信息,并且删除平面字段也填充了飞机 ID。我希望这些字段在刷新后为空,这就是为什么我认为使用 @RequestScoped 会很有用。此外,如果我尝试从网络浏览器刷新页面,它只会尝试重新发送表单。

如何避免使用 bean 中的数据填充字段?

另外,这里是 bean 代码:

@Named
@RequestScoped
public class AddAirplaneCtrl implements Serializable{

    @Inject
    private FlightModel flightModel;

    private ListAirplaneBB listAirplaneBB;

    protected AddAirplaneCtrl(){
        ;
    }

    @Inject
    public void addListAirplaneBB(ListAirplaneBB listAirplaneBB){
        this.listAirplaneBB = listAirplaneBB;
    }

    public String addPlane(){

        listAirplaneBB.setError(null);
        listAirplaneBB.setMessage(null);

        if(failed any of the validation parts){
            return /some/site?faces-redirect=false";
        }

        //add plane
        return /some/site?faces-redirect=true";
    }
}

还有支持 Bean:

@Named
@RequestScoped
public class ListAirplaneBB implements Serializable{

    @Inject
    private FlightModel flightModel;

    //private variable fields

    public List<Airplane> getAllPlanes(){
        return flightModel.getAirplaneList().findAll();
    }

    public int getTotalPlanes(){
        return getAllPlanes().size();
    }

    //GETTERS and SETTERS

}

最后是我的 jsf 页面:

<div class="contbox">
                    <h3>
                        <h:outputLabel value="Edit Planes" />
                    </h3> 
                    <c:if test="#{not empty listAirplaneBB.error}">
                        <div class="alert alert-danger" 
                             id="success-alert">
                            <span class="glyphicon glyphicon-remove" /> 
                            <h:outputText value="#{listAirplaneBB.error}" />
                            <button type="button" 
                                    class="close" 
                                    data-dismiss="alert">
                                <h:outputLabel value="x" />
                            </button>
                        </div>
                    </c:if>
                    <c:if test="#{not empty listAirplaneBB.message}">
                        <div class="alert alert-success">
                            <span class="glyphicon glyphicon-remove" /> 
                            <h:outputText value="#{listAirplaneBB.message}" />
                            <button type="button" 
                                    class="close" 
                                    data-dismiss="alert">
                                <h:outputLabel value="x" />
                            </button>
                        </div>
                    </c:if>

                    <h4>Add A Plane</h4>                     
                    <h:form enctype="multipart/form-data">
                    <table id="addtable" 
                           class="table">
                        <thead>
                            <tr>
                                <th>
                                    <h:outputLabel value="Plane Make" />
                                </th>
                                <th colspan="1">
                                    <h:outputLabel value="Plane Model" />
                                </th>
                                <th colspan="1">
                                    <h:outputLabel value="Plane Seats" />
                                </th>
                                <th colspan="1">
                                    <h:outputLabel value="Plane Photo" />
                                </th>
                            </tr>
                        </thead>
                        <tr>    
                            <td>  
                                <h:inputText value="#{listAirplaneBB.make}">
                                </h:inputText>
                            </td>
                            <td>
                                <h:inputText value="#{listAirplaneBB.model}">
                                </h:inputText>
                            </td>
                            <td>
                                <h:inputText value="#{listAirplaneBB.seats}">
                                </h:inputText>
                            </td>
                            <td>
                                <h:inputFile value="#{listAirplaneBB.file}" >
                                </h:inputFile>
                            </td>       
                            <td>
                                <h:commandButton value="Add Plane" 
                                                 class="btn btn-primary" 
                                                 action="#{addAirplaneCtrl.addPlane()}" >
                                </h:commandButton>
                            </td>
                        </tr>
                    </table>
                    </h:form>                 
                    <h4>Delete Plane</h4>

                    <h:form>
                        <h:panelGrid columns="3" class="table">
                            <h:outputLabel value="Plane ID" />
                            <h:inputText value="#{listAirplaneBB.airPlaneId}"/>
                            <h:commandButton value="Delete" 
                                             class="btn btn-primary"
                                             action="#{addAirplaneCtrl.deleteAirplane}" >
                            </h:commandButton>
                        </h:panelGrid>
                    </h:form>
                </div>

【问题讨论】:

  • @BalusC 你有什么可以提供帮助的信息吗?

标签: jsf primefaces jsf-2


【解决方案1】:

您的浏览器缓存输入。

JSF 2.2 解决方案

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough">
...
    <f:passThroughAttribute name="autocomplete" value="off"/>
...

jQuery 解决方案:

<h:form styleClass="form">...</h:form>

<script>
    $(function(){
        $(".form").attr("autocomplete", "off");
    });
</script>

您可以找到更多信息here

【讨论】:

  • 我在尝试时收到此错误:“属性自动完成未在组件表单中定义”。这个错误是什么意思?
  • 它在我的情况下不起作用。我很确定这是一个 bean 问题,bean 在重定向到同一页面后保持其值。它是一个 requestscoped bean。关于如何在重定向后清空这些字段的任何想法?
  • 好的。将@PostConstruct 方法写入 bean 并查找设置值。
  • 会调查并告诉你进展如何!谢谢
猜你喜欢
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2020-03-18
  • 2012-06-14
  • 2014-11-15
  • 1970-01-01
  • 2012-07-02
相关资源
最近更新 更多