0

我正在使用 Primefaces 3.5、Jsf 2 并且我有一个数据表,现在我实现了一个类似于 primefaces 展示(singlerowselection)的对话框,它工作正常,但我想编辑对话框中的行。此外,我希望 inputtext 填充数据表单元格中的数据,以便您可以轻松编辑它!(就像行编辑器那样,只是在一个对话框中)不像这样:http://i.stack .imgur.com/J55j0.jpg 水印有效,但它不是选项,因为如果你想编辑它,文本会消失。应该是这样的:http: //i.stack.imgur.com/cAFLo.jpg

那么,有人可以告诉我如何显示和编辑单元格数据吗?

这是xhtml(对话框不在数据表中):

<p:dialog id="dialog" header="Server Detail" widgetVar="serDialog"
  resizable="false" showEffect="clip" hideEffect="fold" dynamic="true">

  <h:panelGrid id="display" columns="3" cellpadding="4">
     <h:outputText value="Data Center Location " />
    <p:inputText id="datacenter" value="#{server.dataCenterLocation}"></p:inputText>

    <h:outputText value="Identification " />
    <h:inputText id="identification" value="#{server.identification}"></h:inputText>
    <p:watermark for="identification"
    value="#{serverBean.selectedServer.identification}"></p:watermark>

  </h:panelGrid>

     <p:growl id="growl" showDetail="true" sticky="false" />
     <p:commandButton value="Edit Server" action="#{server.onEdit}"   update="@form, growl" />
</p:dialog>

bean(name= server) 中的方法(没有 selectedServer(name =serverBean) 和 getter 和 setter):

public void onEdit() throws  Exception {
    PreparedStatement ps = null;
    Connection con = null;
    int i = 0;
    try {
        Server ser = new Server();

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
                .newInstance();
        con = DriverManager
                .getConnection("jdbc:sqlserver://...");
        String sql = "UPDATE VAI_Serverlist SET [Data Center Location]= ?,...  WHERE Identification='"
                + ser.identification + "'";

        ps = con.prepareStatement(sql);
        ps.setString(1, ser.dataCenterLocation);
        ...
        i = ps.executeUpdate();
    } catch (SQLException e) {
        System.out.println(i);
        e.printStackTrace();
    } finally {
        try {
            con.close();
            ps.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

提前致谢!

4

1 回答 1

0
<h:form id="form">   
                        <p:dataTable  id="table" var="item" value="#{ManagedBean.list()}" 


                    <f:facet name="footer">  

                                    <p:commandButton id="showUpdateButton" value="#{msgForms['actions.Edit']}" 
                                                     update=":update_form" 
                                                     icon="ui-icon-pencil"  
                                                     style="margin: 0%"
                                                     ajax="true" 
                                                     immediate="true" 
                                                     oncomplete="PF('DialogUpdate').show()"
                                                     />                                                                                                         
                                       </f:facet>  


                        </p:dataTable>

                         <h:form id="form">  


<p:dialog id="dialog_update"
                              modal="true"
                              header="${msgForms['titles.update']}" 
                              widgetVar="DialogUpdate" resizable="false"  
                              showEffect="clip" hideEffect="fold"                              
                              >  
                        <h:form id="update_form">
                            <h:panelGrid columns="3" id="panel"> 
        <!--FIELDS-->  
                            <h:outputLabel for="nombre" value="#{msgForms['fields.nombre']}:" /> 
                            <p:inputText id="nombre"  
                                         value="#{ManagedBean.selectedItem.nombre}" 
                                         required="true"
                                         requiredMessage="#{msgForms['field.required']}"
                                         >   
                                <p:ajax event="keyup" update="nombreMsg" global="false"/>  
                            </p:inputText>  

                            </h:panelGrid>   

                            <!--/FIELDS-->
                            <!-- BUTTONS ACTIONS-->  
                            <p:commandButton id="updateButtonDg" 
                                             value="#{msgForms['actions.update']}" 
                                             icon="ui-icon-pencil"    
                                             actionListener="#{ManagedBean.update()}"
                                             update=":form:"
                                             style="margin: 0%"  
                                             ajax="true"
                                             onclick="DialogUpdate.hide();" 
                                             > 
                            </p:commandButton>
                            <!-- /BUTTONS ACTIONS-->
                        </h:form>
                    </p:dialog>  




    the MANAGED BEAN..       

                    @Named("ManagedBean")

@RequestScoped
public class ManagedBean  { 
    //Spring Item Service is injected... 
    @Inject
    IItemService itemService;  //if not required because if you use dependencies injection to connect with de database
    private List<Item> filteredItems; //only if you use the  attribute: filteredValue="#{ManagedBean.filteredItems}" in the dataTable 
    private List<Item> items; 



                    public void update() {  
                    //here you save the changes in the data base
                    this.itemService().updateItem(this.selectedItem); 



    }

这或多或少是我使用的,我不能放文字代码,但我认为我没有留下任何重要的东西,所以我希望它可以帮助你。

我建议你看看教程 http://www.javacodegeeks.com/2012/04/jsf-2-primefaces-3-spring-3-hibernate-4.html

它展示了如何在您的项目中集成 jsf2 Spring3 hibernate 以及如何使用注释注入依赖项,我建议您使用 hibernate 框架与您的数据库进行交互

于 2014-04-18T11:40:18.543 回答