简体   繁体   English

丰富的面孔和数据表

[英]Rich faces and dataTable

I have the question regarding rich faces and beans. 我有关于丰盛的面孔和豆类的问题。 I have a jsp page that is using richfaces and inside it I have the: rich:extendedDatatable component, that takes data from my MainBean as ArrayList (this bean queries the mySQL and puts results to the ArrayList that populates the dataTable later on). 我有一个使用Richfaces的jsp页面,里面有:rich:extendedDatatable组件,该组件将MainBean中的数据作为ArrayList接收(此bean查询mySQL并将结果放入稍后填充dataTable的ArrayList中)。 There are 4 columns in datatable, first 3 are h:outputLabels and the last one is checkbox. 数据表中有4列,前3列是h:outputLabels,最后一列是复选框。 Now I have a question: how can I get information from selected row ? 现在我有一个问题:如何从选定的行中获取信息? I mean, when user clicks checkbox, I want to take the id/name or whatever that is associated to this particular row, then when user clicks on Apply changed a4j: button I will update the database and when user logs in back again he will see updated info: eg checkbox is selected/not selected now because the user checked that. 我的意思是,当用户单击复选框时,我要获取ID /名称或与此特定行关联的任何名称,然后当用户单击“应用更改”时,我将更新数据库,当用户再次登录时,他将查看更新的信息:例如,由于用户选中了复选框,因此现在选中/未选中复选框。 I believe that is a simple query for someone who worked with it. 我相信这是与之合作的人的简单查询。 For me ex. 对我来说 flash developer it would be easy in as3, but here I didnt find the solution yet, please help. Flash开发人员在as3中会很容易,但是在这里我还没有找到解决方案,请帮忙。

Update : 更新

Let me explain and publish the code. 让我解释一下并发布代码。 When user logs in, then I make a query to database where I have 2 tables. 当用户登录时,然后我查询有2个表的数据库。 First one is with "activities" (act_id, name, description, date) and second one is called "common" (that stores user_id and act_id in it). 第一个带有“活动”(act_id,名称,描述,日期),第二个称为“公共”(在其中存储user_id和act_id)。 My idea is to store data that tells me, which user is assigned to each activity. 我的想法是存储告诉我哪些数据分配给每个活动的数据。 In this case, eg user with id 1, is using activity 1 2 and 3, and user with id 2 is using activity 2 and 4, then when query from database returns a result, I simply create an ArrayList with apropriate data. 在这种情况下,例如,具有ID 1的用户正在使用活动1 2和3,而具有ID 2的用户正在使用活动2和4,那么当从数据库中查询返回结果时,我只创建了一个包含适当数据的ArrayList。 So when user clicks on checkbox, then insert query is made, when user unselect the checkbox, then delete query to database is made (based on user id and act_id) here is the code for query method, first I must get user_id: 因此,当用户单击复选框时,进行插入查询,当用户取消选择复选框后,对数据库进行删除查询(基于用户ID和act_id),这是查询方法的代码,首先我必须获取user_id:

public String login() {
    Statement stmt2 = null;
    Statement stmt3 = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    ResultSet rs3 = null;
    String sql = "SELECT * from user";
    String sql2 = "SELECT * from activities";
    try {
      conn = DriverManager.getConnection(jdbcUrl, user, pass);
      stmt = conn.createStatement();
      stmt2 = conn.createStatement();
      boolean selected = true;

      rs = stmt.executeQuery(sql);
      rs2 = stmt2.executeQuery(sql2);
       while(rs.next()) {
        if (username != null && password != null) {
          if (rs.getString("username").equals(username)
              && rs.getString("password").equals(password)) {
            id = rs.getString("user_id");
            stmt3 = conn.createStatement();
            while(rs2.next()){
              selected = false;
              String aid = rs2.getString("act_id");
              String name = rs2.getString("name");
              String desc = rs2.getString("desc");
              String date = rs2.getString("date");
              String sql3 = "SELECT * from common where uid="+id+"";
              rs3 = stmt3.executeQuery(sql3);
              while(rs3.next()) {
                 if(rs3.getString("aid").equals(aid)){
                   activities.add(new Activity(name, desc, date, true));
                   selected = true;
                 }

              }
              if(!selected)
                activities.add(new Activity(name, desc, date, false));
            }
            return "success";
          }
        }
    } 
    }catch (SQLException sqle) {
      sqle.printStackTrace();
    } finally {
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
      }
    }
    return "failure";
  }

And rich faces view: 和丰富的面孔视图:

<a4j:form>
<rich:extendedDataTable id="activities" value="#{mainBean.activities}" var="acts" sortMode="single">
        <rich:column label="Name" sortable="true" sortBy="#{acts.name}">
        <f:facet name="header"> 
                 <h:outputText value="Name" />
          </f:facet>
        <h:outputLabel value="#{acts.name}" />
        </rich:column>
        <rich:column label="Description" sortable="true" sortBy="#{acts.description}">
        <f:facet name="header"> 
                 <h:outputText value="Description" />
          </f:facet>
        <h:outputLabel value="#{acts.description}" />
        </rich:column>
        <rich:column label="Date" sortable="true" sortBy="#{acts.date}">
        <f:facet name="header"> 
                 <h:outputText value="Date" />
          </f:facet>
        <h:outputLabel value="#{acts.date}" />
        </rich:column>  
        <rich:column label="Selected" sortable="true" sortBy="#{acts.selected}">
        <f:facet name="header"> 
                 <h:outputText value="Selected" />
          </f:facet>
        <h:selectBooleanCheckbox value="#{acts.selected}" />
        </rich:column>
        </rich:extendedDataTable>
        <h:commandButton value="Apply changes" action="#{mainBean.addActivity}" />
        </a4j:form>

JSF has already updated the beans in the arraylist. JSF已经更新了arraylist中的bean。 Just persist it the usual way in the action method: 只需以常规方法在action方法中坚持下去即可:

public void save() {
    mainBeanDAO.save(mainBeans);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM