1.如何結束session?
你可以使用session的 invalidate方法 .
下面是一個從action方法中結束session的例子: :

public String logout()
{
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
session.invalidate();
return "login_page";
}
下面的代碼片段示例了如何在JSP頁面中結束session:
<% session.invalidate(); %>
<c:redirect url="loginPage.jsf" />
2.如何在JSP頁面中訪問web.xml中的初始化參數?
你可以使用預定義的JSF EL變量 initParam來訪問:
例如,如果你有:
<context-param>
<param-name>productId</param-name>
<param-value>2004Q4</param-value>
</context-param>
你可以使用她 #{initParam['productId']}來訪問 .例如:
Product Id: <h:outputText value="#{initParam['productId']}"/>
3.如何從java代碼中訪問web.xml 中的初始化參數?
你可以使用externalContext的 getInitParameter 方法得到他們.例如 如果你的參數如下:
<context-param>
<param-name>connectionString</param-name>
<param-value>jdbc:oracle:thin:scott/tiger@cartman:1521:O901DB</param-value>
</context-param>
你可以使用下面代碼訪問connectionString :
FacesContext fc = FacesContext.getCurrentInstance();String connection = fc.getExternalContext().getInitParameter("connectionString");
4.如何從backing bean中得到當前頁面的URL?
你可以通過FacesContext得到一個Http Request對象的引用,如下:
FacesContext fc = FacesContext.getCurrentInstance();HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
然后使用普通的request方法來得到路徑信息.還可以使用另外一種方法:
context.getViewRoot().getViewId();
將返回你當前JSP(JSF view IDs 基本上只是JSP path names)頁面的名字.
5.如何添加上下文路徑到outputLink的URL中?
在當前的JSF實現中,當在outputLink 中定義的路徑以'/'開始時,沒有添加上下文路徑到URL中,要彌補該問題請在URL中使用 #{facesContext.externalContext.requestContextPath} 前綴.例如:
<h:outputLink value="#{facesContext.externalContext.requestContextPath}/myPage.faces">
6.如何使用URL字符串來傳遞參數到JSF程序中?
如果你有下面的URL: http:
FacesContext fc = FacesContext.getCurrentInstance();String id = (String) fc.getExternalContext().getRequestParameterMap().get("id");
在JSF頁面上,你也可以使用預定義的變量訪問同樣的參數,例如:
<h:outputText value="#{param['id']}" />
注意: 你必須直接調用該JSF頁面,并且使用servlet 映射 (mapping).
7.如何在頁面重新載入的時候保留h:inputSecret中的密碼?
設置redisplay=true, it is false by default.
8.如何使用h:outputText輸出HTML標簽?
h:outputText有一個 escape 屬性用來處理html 標簽. 默認值為true.這意味著所有特殊的符合都被轉義為'&'代碼. 請看下面示例: <h:outputText value="<b>This is a text</b>"/> 打印的結果是: <b>This is a text</b> 而 <h:outputText escape="false" value="<b>This is a text</b>"/> 打印的結果是: This is a text 當用戶點擊Command Link后如何顯示確認對話框?
h:commandLink指定了 onclick 屬性為內部使用. 因此你不可以使用她了, 該問題已經在JSF1.2中修復了,對于JSF1.2以前的版本,你可以在onclick以前使用 onmousedown 事件 <script language="javascript"> function ConfirmDelete(link) { var delete = confirm('Do you want to Delete?'); if (delete == true) { link.onclick(); } }</script>
<h:commandLink action="delete" onmousedown="return ConfirmDelete(this);"> <h:outputText value="delete it"/></h:commandLink>
9.在調用ValueChangeListener 方法后如何重新裝載頁面?
在 ValueChangeListener的最后,調用 FacesContext.getCurrentInstance().renderResponse()
如何實現"請等待..."頁面? 在客戶端實現可能很簡單.你可以包裝JSP頁面(或者你想要隱藏的一部分)到一個div中,然后你可以添加更多div,當用戶點擊提交按鈕時這些div出現.這些div可以包含gif動畫和其他內容. 場景:當用戶點擊按鈕,調用JS函數,該函數隱藏頁面并且顯示"請等待..."div.你可以使用CSS來自定義外觀:下面是一個正常工作的例子: <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle basename="demo.bundle.Messages" var="Message"/>
<html>
<head>
<title>Input Name Page</title>
<script>
function gowait() {
document.getElementById("main").style.visibility="hidden";
document.getElementById("wait").style.visibility="visible";
}
</script>
</head>
<body bgcolor="white">
<f:view>
<div id="main">
<h1><h:outputText value="#{Message.inputname_header}"/></h1>
<h:messages style="color: red"/>
<h:form id="helloForm">
<h:outputText value="#{Message.prompt}"/>
<h:inputText id="userName" value="#{GetNameBean.userName}" required="true">
<f:validateLength minimum="2" maximum="20"/>
</h:inputText>
<h:commandButton onclick="gowait()" id="submit"
action="#{GetNameBean.action}" value="Say Hello" />
</h:form>
</div>
<div id="wait" style="visibility:hidden; position: absolute; top: 0; left: 0">
<table width="100%" height ="300px">
<tr>
<td align="center" valign="middle">
<h2>Please, wait
</h2>
</td>
</tr>
</table>
</div>
</f:view>
</body>
</html>

如果你想有一個動畫gif圖片在
"請等待..."中,當表單提交后該圖片應該從新加載.因此,再一次指定圖片的id,并且添加經過一段時間延時后重新加載的代碼.下面是個例子: <script>
function gowait() {
document.getElementById("main").style.visibility="hidden";
document.getElementById("wait").style.visibility="visible";
window.setTimeout('showProgress()', 500);
}
function showProgress(){
var wg = document.getElementById("waitgif");
wg.src=wg.src;
}
</script>

.
<img id="waitgif" src="animated.gif">
posted on 2008-12-01 09:55
Vincent-chen 閱讀(298)
評論(0) 編輯 收藏 所屬分類:
JSF