2010-12-01 58 views
0

我有一個非常簡單的JSF 2.0項目。動態調用消息包?

我有一個index.xhtml文件給我看拉什莫爾山的圖片。 在這個頁面上,我可以點擊圖片,我希望它去「president.xhtml」 - 沒有問題。一個簡單的動作=「」 ...

我的問題是,我的消息包文件(messages.properties)設置了靜態鍵和值,例:

jeffersonPageTitle=Thomas Jefferson 
rooseveltPageTitle=Theodore Roosevelt 
lincolnPageTitle=Abraham Lincoln 
washingtonPageTitle=George Washington 

在我的「總統.xhtml「文件,我希望它根據我點擊的內容顯示這些標題。在上面的代碼

<h:form> 
    <span class="presidentPageTitle">#{msgs['rushmore.president'],PageTitle}</span> 
    <br /> 
    <h:graphicImage library="images" name="jefferson.jpg" styleClass="leftImage" /> 
    <span class="presidentDiscussion">#{msgs.washingtonDiscussion}</span> 
    <br /> 
    <h:commandLink action="index" styleClass="backLink">${msgs.indexLinkText}</h:commandLink> 
</h:form> 

下聯是我的問題 - 我不知道如何來引用GET-方法在我的Java代碼。代碼是在這裏:

@ManagedBean // or @Named 
@RequestScoped 
public class Rushmore { 
private String outcome = null; 
private Rectangle washingtonRect = new Rectangle(70, 30, 40, 40); 
private Rectangle jeffersonRect = new Rectangle(115, 45, 40, 40); 
private Rectangle rooseveltRect = new Rectangle(135, 65, 40, 40); 
private Rectangle lincolnRect = new Rectangle(175, 62, 40, 40); 

public Rushmore() {} 

public void handleMouseClick(ActionEvent e) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    String clientId = e.getComponent().getClientId(context); 
    Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap(); 


    int x = new Integer((String) requestParams.get(clientId + ".x")).intValue(); 
    int y = new Integer((String) requestParams.get(clientId + ".y")).intValue(); 

    outcome = null; 

    if (washingtonRect.contains(new Point(x, y))) { 
    outcome = "washington"; 
    } 

    if (jeffersonRect.contains(new Point(x, y))) { 
    outcome = "jefferson"; 
    } 

    if (rooseveltRect.contains(new Point(x, y))) { 
    outcome = "roosevelt"; 
    } 
    if (lincolnRect.contains(new Point(x, y))) { 
    outcome = "lincoln"; 
    } 
    System.out.println(requestParams.keySet()); 
} 

public String getPresident() { 
    return outcome; 
} 

public String navigate() { 
    if(outcome != null) { 
    return "president"; 
    } 
    else return null; 
} 
} 

這是getPresident方法我想在president.xhtml文件到達...

任何幫助,將不勝感激:)

+0

與您所關注的示例有什麼不同? IIRC它是Cay Horstmanns的例子之一。 – 2010-12-01 20:40:21

回答

0

使用c:set

<html xmlns:c="http://java.sun.com/jsp/jstl" ...> 
... 
<c:set var="pageTitle" value="#{rushmore.president}PageTitle" /> 
<span class="presidentPageTitle">#{msgs[pageTitle]}</span>