gpt4 book ai didi

java - 尝试建立一个可在多个地方使用的列表

转载 作者:行者123 更新时间:2023-12-01 19:58:38 24 4
gpt4 key购买 nike

我正在尝试开发和理解类和 jsf 标签之间的通信。我发现jsf标签直接使用类而不创建任何实例,而当需要在bean之间进行通信时,必须构建类的实例。由于需要动态列表,然后将选定的值复制到不同的 bean,我尝试为动态列表构建自定义标签,但在需要将选定的值复制到需要保存其他 bean 值的 bean 时陷入困境用于构建列表。

以下是最小的可重现示例

pinnacleTags.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib>
<namespace>pinnacleTags/facelets</namespace>
<tag>
<tag-name>PinnacleCombo</tag-name>
<source>pinnacleCombo.xhtml</source>
<attribute>
<name>actionListenerBeanMethod</name>
</attribute>
</tag>
</facelet-taglib>

列表标签

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<ui:composition>
<o:methodParam name="method" value="#{actionListenerBeanMethod}" />
<p:dialog id="dlgTBL" modal="true" showEffect="bounce" widgetVar="dlg" resizable="false">
<p:dataTable var="result" id="tbl" widgetVar="dtlTBL"
value="#{liveRangeService.tableData}"
filteredValue="#{liveRangeService.filteredData}"
paginator="false"
scrollable="true" rowIndexVar="rowindex" scrollHeight="500"
scrollRows="50" liveScroll="true"
filterDelay="1100"
>
<p:ajax event="rowSelect" listener="#{method}" />
<f:facet name="header">
<p:outputPanel layout="inline" styleClass="tabSpacer">
<h:outputText value="Global Filter:" />
<p:inputText id="globalFilter" onkeyup="PF('dtlTBL').filter()" style="width:150px;margin-left:10px;"/>
</p:outputPanel>
</f:facet>

<p:column width="50">
<f:facet name="header">
<h:outputText value="Sr." />
</f:facet>
<p:commandButton value="#{rowindex}" style="width: 49px" action="#{method}"/>
</p:column>

<p:columns value="#{liveRangeService.tableHeaderNames}"
var="mycolHeader"
width="#{colIndex==0?'10%':colIndex==1?'70%':colIndex==2?'10%':colIndex==3?'10%':'0'}"
columnIndexVar="colIndex"
sortBy="#{result[mycolHeader]}"
filterBy="#{result[mycolHeader]}"
filterMatchMode="contains"
>
<f:facet name="header">
<h:outputText value="#{mycolHeader}" />
</f:facet>
<h:outputText value="#{result[mycolHeader]}" />
<br />
</p:columns>
</p:dataTable>
</p:dialog>
</ui:composition>
</h:body>
</html>

动态列表 Bean

package classes;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.primefaces.PrimeFaces;

@ManagedBean(name="liveRangeService", eager = true)
@RequestScoped

public class LiveRangeService implements Serializable {
private static List< Map<String, String> > tableData;
public static List< Map<String, String> > filteredData;
private static List<String> tableHeaderNames;
private String tableColWidths;

public List<Map<String, String>> getTableData() {
return tableData;
}
public List<String> getTableHeaderNames() {
return tableHeaderNames;
}

public LiveRangeService() {

}

public static void LiveRangeServicesss(int noOfRows) {
tableData = new ArrayList< Map<String, String> >();
filteredData = new ArrayList< Map<String, String> >();
tableHeaderNames = new ArrayList<String>();
try {
tableData.clear();
tableHeaderNames.clear();
filteredData.clear();
} catch (Exception e) {
System.out.println("error:!" + e.getMessage());
}
tableHeaderNames.add("ID");
tableHeaderNames.add("Title");
tableHeaderNames.add("Opn_Amt");
tableHeaderNames.add("Smr_Amt");

for (int i = 0; i < noOfRows; i++) {
Map<String, String> playlist = new HashMap<String, String>();
playlist.put("ID", "101000" + i);
playlist.put("Title", "Share Capital - Mr. " + i);
playlist.put("Opn_Amt", "0");
playlist.put("Smr_Amt", "0");
tableData.add(playlist);
}
filteredData=tableData;
System.out.println("Filled " + filteredData.size() + ", " + noOfRows);
String dlgTBL="form:dlgTBL";
String dlg="PF('dlg').show();";
PrimeFaces.current().ajax().update(dlgTBL);
PrimeFaces.current().executeScript(dlg);
}

public String getTableColWidths() {
return tableColWidths;
}

public void setTableColWidths(String tableColWidths) {
this.tableColWidths = tableColWidths;
}

public List<Map<String, String>> getFilteredData() {
return filteredData;
}

public void setFilteredData(List<Map<String, String>> filteredData) {
this.filteredData = filteredData;
}
}

测试Bean

package classes;


import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.PrimeFaces;
import org.apache.commons.beanutils.PropertyUtils;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Administrator
*/

@ManagedBean(name="test")
@SessionScoped

public class Test implements Serializable {
public String id;
public String title;

public Test() {
id="1";
title="Testing";
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public void onRowSelect(int row) {
try {
System.out.println("Start:");
String i="ID";
String t="Title";
String ci="id";
String ct="title";
this.getClass().getDeclaredField(ci).set(this, LiveRangeService.filteredData.get(row).get(i));
this.getClass().getDeclaredField(ct).set(this, LiveRangeService.filteredData.get(row).get(t));
PrimeFaces.current().ajax().update("form:inp");
PrimeFaces.current().ajax().update("form:inp1");
PrimeFaces.current().executeScript("PF('dlg').hide();");
System.out.println("End:");
} catch (Exception e) {
System.out.println("Error! " + e.getMessage() );
}
}
}

最佳答案

假设您的问题是如何从 applicationScoped LiveRangeService 内访问 sessionScoped test bean:您不能简单地以这种方式实例化 ManagedBean:

test t = new test();

实例由 JSF 管理,并根据需要实例化,其生命周期由其范围定义。

请阅读How to access property of one managed bean in another managed bean以及博客文章 Communication in JSF 2.0 - Injecting managed beans in each other由 BalusC 链接。

您的示例还有几个其他问题:

  1. 您的bean的范围似乎没有正确选择,请阅读How to choose the right bean scope?
  2. 您不遵循 Java 命名约定。类名应以大写字符开头,而字段和变量应以小写字符开头。例如。将 class test 重命名为 class Test 并将 private String CopyValuesToClass; 重命名为 private String copyValuesToClass;
  3. 到目前为止,该示例还不是最小。您应该删除所有与理解问题无关的代码。
  4. 您应该考虑迁移到最新版本的 JSF 和 CDI,因为 @ManagedBean 已被弃用:Migrate JSF managed beans to CDI managed beans

关于java - 尝试建立一个可在多个地方使用的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59019293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com