JSF - 在运行时填充值

时间:2012-01-29 13:07:02

标签: jsf

晕 我的名字是Sergie,我使用jsf spring和jpa来建立学校自动化系统。我正在学习jsf。 我需要你的帮助,三个标题

  

城市|学校|添加/删除

<c:column>
    <f:facet name="header">
    <c:outputText value="City" />
</f:facet>

    <c:outputText id="ukrCity" value="" " />
</c:column>

<c:column>
<f:facet name="header">
<c:outputText value="School" />
</f:facet>
<c:inputText id="school" value=""
        maxlength="12" " />
            </c:column>

<c:column>
<f:facet name="header">
<c:outputText value="Add/Remove" />
</f:facet>
<c:selectBooleanCheckbox
    id="addremove"
        value=""
        rendered="" />
</c:column>




 City      |           School       |  Add/Remove


 Київ               "textbox"           "checkbox"

 Харків               "textbox"           "checkbox"

城市从City类

填充
public class UkrCity {                     

private List<A> ukrCities;

public List<A> getUkrCities() {
        return ukrCities;
    }

public void setUkrCities(final List<A> ukrCities) {
        this.ukrCities= ukrCities;
    }

 private void allCities() {
        //add all cities in a list
        ukrCities.add("Київ");
        ukrCities.add("Харків");
    }
}

如何在城市下方的xhtml页面上显示ukrcities,并在学校下方的空白文本框和复选框中添加/删除tav。

谢谢你 抱歉,我的英语不好。

1 个答案:

答案 0 :(得分:1)

我重构了你的代码。这是UkrCity类。

package com.example;

import java.io.Serializable;

public class UkrCity implements Serializable {
   /**
 * 
 */
private static final long serialVersionUID = 1L;
private String name;

public UkrCity(String name) {
      this.setName(name);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

这是支持bean

package com.example;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class TableBean implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
private ArrayList<UkrCity> cities = new ArrayList<UkrCity>(Arrays.asList(new UkrCity("Київ"),new UkrCity("Харків") ));
public ArrayList<UkrCity> getCities() {
    return cities;
}
}

这是你的index.xhtml

<?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:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
  <title>UkrCities Table</title>
</h:head>
<h:body>
  <h:form>
     <h:dataTable value="#{tableBean.cities}" var="city">

        <h:column>
           <f:facet name="header">City</f:facet>
           <h:outputText value="#{city.name}" />
        </h:column>

        <h:column>
           <f:facet name="header">School</f:facet>
           <h:inputText value="" />
        </h:column>

        <h:column>
            <f:facet name="header">Add/Remove</f:facet>
            <h:selcectBooleanCheckbox value="" onclick="submit()" />
        </h:column>
     </h:dataTable>
  </h:form>
</h:body>
</html>