Primefaces:<p:dataTable>的数据源更改或并发操作将导致不一致的参数从jsf-el传递到支持bean

如何解决Primefaces:<p:dataTable>的数据源更改或并发操作将导致不一致的参数从jsf-el传递到支持bean

如果我有一个由requestScoped命名bean控制的dataTable,则命名bean(后备bean)从EJB查询数据,并通过EntityManager和JPA查询EJB查询数据。

对于视图部分,我使用PrimeFaces中的dataTable: 我的问题是:如果我在一个或两个浏览器中打开两个选项卡(问题相同):我在一个选项卡中删除了一行,然后转到未刷新的另一选项卡(因此删除的行仍然存在)。如果我再次按delete commandLink,则删除行当然会被删除,因为页面已刷新,但问题是它也删除了它下面的行。 顺便说一下,我的JSF版本是2.2,素数是4.0(现在我将其替换为8.0,错误仍然存​​在)。 是jsf还是primefaces错误(我从未尝试过h:commandLink和h:dataTable)还是我犯了一些错误?

You can reference below code for named bean:

    @Named(value="allIndustryController")
    @RequestScoped
    public class AllIndustryController {
    
    @Inject
    IndustryTypeBean industryTypeBean;
    
    private List<Industry> industries;
    
    public AllIndustryController() {
        
        
    }
    
    @PostConstruct
    public void init() {
        industries = industryTypeBean.getAllIndustrys();
    }


    public List<Industry> getIndustries() {
        
        return industries;
    }

    public void setIndustries(List<Industry> industries) {
        this.industries = industries;
    }
    
    public String deleteIndustry(int industryID) {
        
        try {
            
            boolean result = industryTypeBean.removeIndustry(industryID);
            if (result) {
                init();
                FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Industry " + industryID +" has been deleted succesfully"));
            }
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Delete Industry Failed"));
        }
        return "allIndustries.xhtml?faces-redirect=true";
        
    }
    
    public void updateIndustry(int industryID,String industryName) {
        Industry industry = industryTypeBean.findIndustryByID(industryID);
        industry.setIndustryName(industryName);
        industryTypeBean.editIndustry(industry);
        
    }
    
    public String addIndustry() {
        Industry industry = new Industry();
        industryTypeBean.addIndustry(industry);
        return "allIndustries.xhtml?faces-redirect=true";
        
    }

}

dataTable的代码:

<!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>All Industries</title>
    <h:outputStylesheet library="css" name="bootstrap.min.css"></h:outputStylesheet>
</h:head>
<body>
    <div class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="navbar-brand">Customer Management System</div>

        <div class="collapse navbar-collapse">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item active"><h:link class="nav-link"
                        value="Home | " outcome="index.xhtml" /></li>
                <li class="nav-item"><h:link class="nav-link"
                        value="Manage All Users | " outcome="allUsers.xhtml" /></li>
                <li class="nav-item"><h:link class="nav-link"
                        value="Manage All Customers |" outcome="allCustomers.xhtml" /></li>
                <li class="nav-item"><h:link class="nav-link"
                        value="Manage Industries |" outcome="allIndustries.xhtml" /></li>
                <h:form>
                    <li class="nav-item"><h:commandLink class="nav-link"
                            value="Log out" action="#{loginController.logout()}" /></li>
                </h:form>
            </ul>
        </div>
    </div>

    <h:form id="MyForm">
        <p:dataTable class="table table-bordered table-striped"
            id="myIndustryList" value="#{allIndustryController.industries}"
            var="industry">
            <p:column headerText="industryID">
                <p:outputLabel value="#{industry.industryID}" id="industryID" />
            </p:column>
            <p:column headerText="industryName">
                <p:inputText value="#{industry.industryName}" id="industryName" />
            </p:column>


            <p:column headerText="Operations">
                <p:commandLink value="Delete | " ajax="true"
                    action="#{allIndustryController.deleteIndustry(industry.industryID)}"
                    disabled="#{industry.industryID == null}" update="myIndustryList">
                    <p:confirm header="Confirmation" message="Are you sure?"
                        icon="pi pi-exclamation-triangle" />
                </p:commandLink>

                <p:commandButton value="update Name"
                    action="#{allIndustryController.updateIndustry(industry.industryID,industry.industryName)}"
                    oncomplete="PF('cd').show()">
                    <!-- <f:param name="userAccount" value="#{normalUser.account}" /> -->
                </p:commandButton>

            </p:column>

            <!--                            <h:link value="View | " outcome="userDetail.xhtml">
                                pass the parameter to next page,the param name is propertyID,and the value is index + 1.
                                You can get the value from next page using the indexController
                                <f:param name="userAccount" value="#{normalUser.account}" />
                                
                            </h:link> 
                            
                            <h:link value="Edit | " outcome="editUser.xhtml">
                                pass the parameter to next page,and the value is index + 1.
                                You can get the value from next page using the indexController
                                <f:param name="userAccount" value="#{normalUser.account}" />
                            </h:link>  -->


            <!-- reference:https://stackoverflow.com/questions/19362983/how-to-add-confirmation-dialog ; https://www.primefaces.org/showcase/ui/data/datatable/basic.xhtml-->


        </p:dataTable>

        <p:confirmDialog global="true" showEffect="fade" hideEffect="fade">
            <p:commandButton value="Yes" type="button"
                styleClass="ui-confirmdialog-yes" icon="pi pi-check" />
            <p:commandButton value="No" type="button"
                styleClass="ui-confirmdialog-no" icon="pi pi-times" />
        </p:confirmDialog>
        <h:outputText value=" " />

        <p:dialog header="Save the name" severity="alert" widgetVar="cd">
            <h:outputText value="The Industry Name is saved" />
        </p:dialog>

        <div>
            <h:commandButton id="add" value="Add"
                action="#{allIndustryController.addIndustry()}"
                class="btn btn-primary" />

            <h:commandButton id="viewAll" value="View All"
                action="allIndustries.xhtml?faces-redirect=true"
                class="btn btn-primary">
            </h:commandButton>
        </div>

        <!--            <div>
                <h:inputText value="#{adminApplication.searchedAccount}"
                    id="searchByAccount"
                    onchange="if (document.getElementById('MyForm:searchByAccount').value.trim() == '') {document.getElementById('MyForm:searchByAccount').value = '';} " />
                <h:commandButton id="search" value="Search by Account"
                    action="#{adminApplication.searchUserByAccount(adminApplication.searchedAccount)}"
                    class="btn btn-primary">
                </h:commandButton>
            </div> -->
    </h:form>



</body>
</html>
  

----------------------------------更新---------------- ---------------

实际上,我通过使用再次检查了由命名bean方法传递的那个来解决此问题。由于任务繁重,我忘记了更新。 (抱歉 而且,如果我在页面上使用某种搜索功能(我过滤了存储行业的ArrayList),则无法在正确的行上执行删除操作:

我想我可以将问题总结如下: 每当我在支持bean中更新实际dataTable的数据源时,我都无法在支持bean的方法中传递参数(参数将不一致,这似乎是索引问题?只是一个疯狂的猜测)。无论如何,f:param总是可以给出正确的答案

对于更多感兴趣的人,我发布了IndustryType.java代码,尽管它与问题无关。问题仍然在于素数如何处理xhtml文件。欢迎获得更详细的解释,并希望这篇文章对更多人有用:

package beans;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

import entity.Industry;
import repository.IndustryRepository;

@Named
@SessionScoped
public class IndustryTypeBean implements Serializable {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @EJB
    IndustryRepository industryRepository;
    
    public List<Industry> getAllIndustrys(){
        try {
        List<Industry> industrys = industryRepository.getAllIndustries();
        return industrys;
        }catch (Exception ex) {
            Logger.getLogger(IndustryTypeBean.class.getName()).log(Level.SEVERE,null,ex);
        }
        return null;
    }
    
    public boolean addIndustry(Industry Industry) {
        try {
            industryRepository.addIndustry(Industry);
            return true;
        }catch(Exception ex) {
            Logger.getLogger(IndustryTypeBean.class.getName()).log(Level.SEVERE,ex);
        }
        return false;
        
    }
    
    public boolean removeIndustry(int industryID) {
        try {
            industryRepository.deleteIndustry(industryID);
            return true;
        } catch(Exception ex) {
            Logger.getLogger(IndustryTypeBean.class.getName()).log(Level.SEVERE,ex);
        }
        return false;
        
    }
    
    public boolean editIndustry(Industry Industry) {
        try {
            industryRepository.updateIndustry(Industry);
            return true;
        }catch(Exception ex) {
            Logger.getLogger(IndustryTypeBean.class.getName()).log(Level.SEVERE,ex);
        }
        return false;
        
    }
    
    public Industry findIndustryByID (int industryID) {
        try {
            
            return industryRepository.findIndustryByID(industryID);
        }catch(Exception ex) {
            Logger.getLogger(IndustryTypeBean.class.getName()).log(Level.SEVERE,ex);
        }
        return null;    
    }
    

}

在JSF中,viewscope和flowscope有CDI,因此您必须使用其他实现。

textbook quote

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-