2009-06-17 66 views
3

我有一個無序的List導航,我想將其轉換爲JSF代碼。 其實,沒有JSF標籤..將HTML轉換爲JSF

這裏是原始HTML導航:

<ul id="navbar"> 
<li id="articles"><a href="Link1">Articles</a></li> 
<li id="topics"><a href="Link2" title="Topics">Topics</a></li> 
.... 
</ul> 

..所以我應該怎麼辦?

回答

4

做特別模板的一種方法是使用Facelets而不是JSP。由於這些已成爲JSF 2.0的一部分(在JEE6中提供),我認爲現在是考慮它們的好時機。

例如,對於豆:

public class LiBean implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private String href; 
    private String title; 

    public String getHref() { return href; } 

    public void setHref(String href) { this.href = href; } 

    public String getTitle() { return title; } 

    public void setTitle(String title) { this.title = title; } 

} 

...和:

public class UlBean implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private final List<LiBean> listItems = new ArrayList<LiBean>(); 

    public UlBean() { 
    String[] links = { "http://www.sun.com", "Sun", 
     "http://www.oracle.com", "Oracle", 
     "http://www.ibm.com", "IBM", 
     "http://stackoverflow.com", 
     "<stackoverflow.com> & encoded output" }; 
    for (int i = 0; i < links.length; i++) { 
     LiBean item = new LiBean(); 
     item.setHref(links[i]); 
     item.setTitle(links[++i]); 
     listItems.add(item); 
    } 
    } 

    public List<LiBean> getListItems() { 
    return listItems; 
    } 

} 

...在會話範圍定義:

<managed-bean> 
    <managed-bean-name>ulBean</managed-bean-name> 
    <managed-bean-class>beans.UlBean</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
    </managed-bean> 

...可以是用於此Facelet頁面:

<?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:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>repeat</title> 
</head> 
<body> 
<ul> 
    <ui:repeat value="#{ulBean.listItems}" var="item"> 
    <li><a href="#{item.href}"><h:outputText 
     value="#{item.title}" /></a></li> 
    </ui:repeat> 
</ul> 
</body> 
</html> 

......產生這種輸出:

<?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"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>repeat</title> 
</head> 
<body> 
<ul> 
    <li><a href="http://www.sun.com">Sun</a></li> 
    <li><a href="http://www.oracle.com">Oracle</a></li> 
    <li><a href="http://www.ibm.com">IBM</a></li> 
    <li><a href="http://stackoverflow.com">&lt;stackoverflow.com&gt; &amp; encoded output</a></li> 
</ul> 
</body> 
</html> 

如果您在使用JSP堅持,沒有在覈心實現任何將讓你這樣做(唯一的重複控制是dataTable)。您可能會在3rd party component libraries中找到某種東西。

+1

也許值得補充的是,Facelets在混合JSF組件和標準HTML(不需要f:verbatim)時沒有問題。如果需要,您可以直接添加列表項目。 – Damo 2009-06-18 15:15:22