如何创建可编辑的<h:panelgrid> </h:panelgrid>

时间:2012-07-24 17:16:45

标签: java jsf jsf-2

我发现这个example如何在JSF页面中创建可编辑的h:datatable

我想用h:panelGrid创建类似的内容:

<h:panelGrid columns="2">

    <h:panelGroup>User ID</h:panelGroup>
    <h:panelGroup>
        <h:outputText value="#{AccountProfileController.settings['USERID']}"/>
        <h:inputText value="#{AccountProfileController.settings['USERID']}"/>
    </h:panelGroup>

    <h:panelGroup>Group ID</h:panelGroup>
    <h:panelGroup>
        <h:outputText value="#{AccountProfileController.settings['GROUPID']}"/>
        <h:inputText value="#{AccountProfileController.settings['GROUPID']}"/>
    </h:panelGroup>

    <h:panelGroup>Special Number</h:panelGroup>
    <h:panelGroup>
        <h:outputText value="#{AccountProfileController.settings['SPECIALNUMBER']}"/>
        <h:inputText value="#{AccountProfileController.settings['SPECIALNUMBER']}"/>
    </h:panelGroup>

    <h:panelGroup>Username</h:panelGroup>
    <h:panelGroup>
        <h:outputText value="#{AccountProfileController.settings['USERNAME']}"/>
        <h:inputText value="#{AccountProfileController.settings['USERNAME']}"/>
    </h:panelGroup>
</h:panelGrid>     

托管bean:

import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.glassfish.osgicdi.OSGiService;

@Named("AccountProfileController")
@ViewScoped
public class AccountProfile implements Serializable {

    @Resource(name = "jdbc/Oracle")
    private DataSource ds;

    private int id;
    // This Hash Map will be used to store setting and value
    private HashMap<String, String> settingsMap = new HashMap<>();

    public HashMap<String, String> getsettings()
    {
        return settingsMap;
    }

    public AccountProfile()
    {
        // get the ID value 
        try
        {
            this.id = Integer.parseInt((String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
        }
        catch (Exception e)
        {
            this.id = 0;
        }
    }

    @PostConstruct
    public void initData() throws SQLException
    {
        //     settingsMap = new HashMap<String, String>();

        if (ds == null)
        {
            throw new SQLException("Can't get data source");
        }
        // Initialize a connection to Oracle
        Connection conn = ds.getConnection();

        if (conn == null)
        {
            throw new SQLException("Can't get database connection");
        }
        // With SQL statement get all settings and values
        PreparedStatement ps = conn.prepareStatement("SELECT * from USERS where USERID = ?");
        ps.setInt(1, id);
        try
        {
            //get data from database        
            ResultSet result = ps.executeQuery();
            while (result.next())
            {
                // Put the the data from Oracle into Hash Map                
                settingsMap.put("USERID", result.getString("USERID"));
                settingsMap.put("GROUPID", result.getString("GROUPID"));
                settingsMap.put("SPECIALNUMBER", result.getString("SPECIALNUMBER"));
                settingsMap.put("USERNAME", result.getString("USERNAME"));
                settingsMap.put("PASSWD", result.getString("PASSWD"));
                settingsMap.put("DATETOCHANGEPASSWD", result.getString("DATETOCHANGEPASSWD"));
                settingsMap.put("ADDRESS", result.getString("ADDRESS"));
                settingsMap.put("STATEREGION", result.getString("STATEREGION"));
                settingsMap.put("COUNTRY", result.getString("COUNTRY"));
                settingsMap.put("USERSTATUS", result.getString("USERSTATUS"));
                settingsMap.put("TELEPHONE", result.getString("TELEPHONE"));
                settingsMap.put("DATEUSERADDED", result.getString("DATEUSERADDED"));
                settingsMap.put("USEREXPIREDATE", result.getString("USEREXPIREDATE"));
                settingsMap.put("DATEUSERLOCKED", result.getString("DATEUSERLOCKED"));
                settingsMap.put("CITY", result.getString("CITY"));
                settingsMap.put("EMAIL", result.getString("EMAIL"));
                settingsMap.put("DESCRIPTION", result.getString("DESCRIPTION"));
            }
        }
        finally
        {
            ps.close();
            conn.close();
        }
    }

    // Editable
    boolean editable;

    public boolean isEditable()
    {
        return editable;
    }

    public void setEditable(boolean editable)
    {
        this.editable = editable;
    }
}

示例中的JSF页面使用alue="#{order.orderList}" var="o"来显示数据。在我的情况下,我使用Java hashmap来显示数据,我不知道如何使用hashmap执行此操作。在我的案例中,您能告诉我如何创建可编辑的h:panelGrid

祝福

0 个答案:

没有答案