Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 托管bean的多个实例_Jsf_Jsf 2_Primefaces - Fatal编程技术网

Jsf 托管bean的多个实例

Jsf 托管bean的多个实例,jsf,jsf-2,primefaces,Jsf,Jsf 2,Primefaces,我正在使用prime faces选项卡显示多个输入表单。问题是,有时我需要实例化相同形式的2。当然,它们都使用相同的托管Bean,这会导致第一个初始化表单的输入用相同的数据覆盖另一个表单。我需要能够把不同的数据在每一种形式,并提交它们共同。每一张表格都会进入一个列表,并转发以供计算。我一直在阅读scope,但唯一有效的是“Sessioned”。会话只允许托管Bean的一个实例,并将表单数据放入列表中,“Request,View scope”不会将数据分别放入列表中,也不会保存数据。托管Bean是

我正在使用prime faces选项卡显示多个输入表单。问题是,有时我需要实例化相同形式的2。当然,它们都使用相同的托管Bean,这会导致第一个初始化表单的输入用相同的数据覆盖另一个表单。我需要能够把不同的数据在每一种形式,并提交它们共同。每一张表格都会进入一个列表,并转发以供计算。我一直在阅读scope,但唯一有效的是“Sessioned”。会话只允许托管Bean的一个实例,并将表单数据放入列表中,“Request,View scope”不会将数据分别放入列表中,也不会保存数据。托管Bean是可序列化的,并且在会话范围内。我读过另一篇文章,但它们不起作用。有什么想法吗

支持Bean

@ManagedBean
@SessionScoped

public class RequestCalculation {
    private CalculationRequest calcReq;
    private List<ViewTabs> formTabs;
    private String id;
    private boolean calcButton;

    public RequestCalculation() {
        calcReq = new CalculationRequest();
        formTabs = new ArrayList<ViewTabs>();
        calcButton = false;

    }

    public String loadForm(TcsBase loadForm) {

        id = loadForm.getId();

        ViewTabs tab = new ViewTabs();
        tab.setFormTitle("Form".concat(id));
        tab.setFormPage("Form".concat(id).concat(".xhtml"));
        formTabs.add(0, tab);
        calcReq.getFormCollection().add(0, loadForm);
        loadCalcButton();
        return "Main";
    }

    public void loadCalcButton() {
        if (formTabs.isEmpty())
            isCalcButton();
        else {
            calcButton = true;
        }
    }

    public void onTabClosed(TabCloseEvent e) {
        TabView tabView = (TabView) e.getComponent();
        int closingTabIndex = tabView.getChildren().indexOf(e.getTab());
        removeForm(closingTabIndex);
        formTabs.remove(closingTabIndex);
        loadCalcButton();
    }

    public void removeForm(int index) {
        TcsBase formIndex = calcReq.getFormCollection().get(index);
        String formId = formIndex.getId();

        // Creates a new instance of the selected form
        FacesContext fc = FacesContext.getCurrentInstance();
        fc.getELContext().getELResolver()
                .setValue(fc.getELContext(), null, "form".concat(formId), null);

        calcReq.getFormCollection().remove(index);
        formTabs.remove(index);
    }

    public String calculate() {
    CalculateService service = new CalculateService();
        CalculatorInterface calculateInterface = service.getCalculatePort();

        XStream xstream = new XStream(new StaxDriver());
        xstream.registerConverter(new JodaTimeConverter());

        // Here is where the client request (input) is converted to an Xml
        // string before going
        // to the Web Service

        String xml = xstream.toXML(calcReq);
        String request = calculateInterface.calculate(xml);

        // Here the response back from the Web Service is converted back from
        // Xml to a string
        // to be displayed to the user in Xhtml

        calcReq = (CalculationRequest) xstream.fromXML(request);

        FacesContext fc = FacesContext.getCurrentInstance();

        for (int i = 0; i < calcReq.getFormCollection().size(); i++) {

            TcsBase newFrm = calcReq.getFormCollection().get(i);
            String frmId = newFrm.getId();
            fc.getELContext()
                    .getELResolver()
                    .setValue(fc.getELContext(), null, "form".concat(frmId),
                            newFrm);
        }

        return null;

    }

    public List<ViewTabs> getFormTabs() {
        return formTabs;
    }

    public void setFormTabs(List<ViewTabs> formTabs) {
        this.formTabs = formTabs;
    }

    public boolean isCalcButton() {
        return calcButton;
    }

    public void setCalcButton(boolean calcButton) {
        this.calcButton = calcButton;
    }
@ManagedBean
@会议范围
公共类请求计算{
私人计算请求calcReq;
私人列表表格标签;
私有字符串id;
私有布尔calcButton;
公共计算(){
calcReq=新计算请求();
formTabs=新的ArrayList();
calcButton=false;
}
公共字符串加载窗体(TcsBase加载窗体){
id=loadForm.getId();
视图选项卡=新的视图选项卡();
tab.setFormTitle(“Form.concat(id));
tab.setFormPage(“Form.concat(id.concat)(.xhtml”);
formTabs.add(0,tab);
calcReq.getFormCollection().add(0,loadForm);
loadCalcButton();
返回“Main”;
}
公共void loadCalcButton(){
if(formTabs.isEmpty())
isCalcButton();
否则{
calcButton=true;
}
}
公共无效onTabClosed(TabCloseEvent e){
选项卡视图选项卡视图=(选项卡视图)e.getComponent();
int closingtab index=tabView.getChildren().indexOf(e.getTab());
removeForm(closingTabIndex);
formTabs.remove(closingTabIndex);
loadCalcButton();
}
公共void removeForm(int索引){
TcsBase formIndex=calcReq.getFormCollection().get(索引);
字符串formId=formIndex.getId();
//创建所选表单的新实例
FacesContext fc=FacesContext.getCurrentInstance();
fc.getELContext().getELResolver()
.setValue(fc.getELContext(),null,“form”.concat(formId),null);
calcReq.getFormCollection().remove(索引);
formTabs.删除(索引);
}
公共字符串计算(){
CalculateService服务=新的CalculateService();
CalculatorInterface calculateInterface=service.getCalculatePort();
XStream XStream=newxstream(new statxdriver());
registerConverter(新的JodaTimeConverter());
//这里是将客户机请求(输入)转换为Xml的地方
//出发前先挂线
//到Web服务
字符串xml=xstream.toXML(calcReq);
字符串请求=calculateInterface.calculate(xml);
//在这里,从Web服务返回的响应从
//将Xml转换为字符串
//在Xhtml中向用户显示
calcReq=(CalculationRequest)xstream.fromXML(请求);
FacesContext fc=FacesContext.getCurrentInstance();
对于(int i=0;i
}

**Html菜单**

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<head>

</head>

<f:view>
    <h:body>

        <h:commandLink action="#{requestCalculation.loadForm(formA)}" value="FormA"  /> <br/>
        <h:commandLink action="#{requestCalculation.loadForm(formB)}" value="FormB" /> <br/><br/><br/>

    </h:body>
</f:view>
</html>





Html主页

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<f:view>    

    <h:head>
        <title> TCS </title>

    </h:head>

    <h:form >


        <p:layout style="min-width:400px;min-height:700px " >
            <p:layoutUnit  position="north" style="text-align:center">
            <p style="text-align:center; font-size:28px">Tax Computation Service</p>
            </p:layoutUnit>

            <p:layoutUnit header="Menu" position="west" style="min-width:190px; min-height:50px; ">
                <p:panelMenu>
                    <p:submenu label="Forms">
                        <p:submenu label="Individual Forms">
                            <p:menuitem>
                                <ui:include src="Menu.xhtml" />
                            </p:menuitem>
                        </p:submenu>
                    </p:submenu>
                </p:panelMenu>
            </p:layoutUnit>

            <p:layoutUnit position="center" >
                <p:tabView  onTabShow="focus" widgetVar="tabView">
                    <p:ajax event="tabClose" listener="#{requestCalculation.onTabClosed}"/>
                    <c:forEach items="#{requestCalculation.formTabs}" var="listItem">
                        <p:tab  title="#{listItem.formTitle}"  closable="true" >
                            <ui:include src="#{listItem.formPage}" />
                         </p:tab>
                    </c:forEach>
                </p:tabView>

                    <p:panelGrid columns="0" >
                        <p:commandButton value="Calculate" action = "#{requestCalculation.calculate}"   ajax="false" rendered="#{requestCalculation.calcButton}" /> 
                    </p:panelGrid>
            </p:layoutUnit>

        </p:layout>

    </h:form>

</f:view>   
</html>

TCS
税务计算服务

格式Html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

    <f:view>

        <h:head>
            <title> FormA</title>
        </h:head>

        <h:body>

            <p:focus />

            <p:panelGrid id="panelGridA" columns="2">
                <p:outputLabel value="Form ID: "  style="width: 725px" />
                <p:outputLabel value="#{formA.id}"  />        

                <p:outputLabel value="4.  . . "  style="width: 725px" />        
                <p:inputText id="input1" style="text-align:right" value="#{formA.line4}" converter="bdNullableConverter" onfocus="this.select()"/> 
                <p:outputLabel value="5. . . . . . . "  style="width: 725px" />        
                <p:inputText style="text-align:right" value="#{formA.line5}" converter="bdNullableConverter" onfocus="this.select()" /> 
                <p:outputLabel value="6.  . . . . . . . "  style="width: 725px" />        
                <p:outputLabel value="#{formA.line6}" converter="bdNullableConverter" /> 
            </p:panelGrid>
        </h:body>

    </f:view>
</html>

形式

如果您想使用manager bean的多个实例,那么您可以在faces-config.xml文件中以不同的名称声明它,并单独使用它。参见示例

<managed-bean>
    <managed-bean-name>productSearchForm</managed-bean-name>
    <managed-bean-class>com.company.package.ProductSearchForm</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>productChildFilter</managed-bean-name>
    <managed-bean-class>com.company.package.ProductSearchForm</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
    <managed-bean-name>productAttachFilter</managed-bean-name>
    <managed-bean-class>com.company.package.ProductSearchForm</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

产品搜索表单
com.company.package.ProductSearchForm
一场
productChildFilter
com.company.package.ProductSearchForm
一场
产品附加过滤器
com.company.package.ProductSearchForm
一场

不要尝试使用一个托管bean的多个实例,而是使用一个托管bean,通过hashmap或arraylist或任何您想要使用的方式访问一个类的多个实例。就像在普通的java编程中一样。不能有两个同名的变量:

@ViewScoped
@Named
public class RequestCalculations {

    Map<String, RequestCalculation> hm;

    @PostConstruct
    public init() {
        hm = new HashMap<>();
        // prepopulate if known upfront
        hm.put("1", new RequestCalculation());
        hm.put("2", new RequestCalculation());
    }

    public HashMap<String, RequestCalculation> getCalculations() {
        return hm;
    }
}
您可能需要通过include将其传递给include
#{requestCalculations.calculations[myTabIndex]}