1.如何結(jié)束session?
你可以使用session的
invalidate方法 .
下面是一個(gè)從action方法中結(jié)束session的例子: :
public String logout() {
FacesContext fc =
FacesContext.getCurrentInstance();
HttpSession session = (HttpSession)
fc.getExternalContext().getSession(false);
session.invalidate();
return "login_page";
}
下面的代碼片段示例了如何在JSP頁(yè)面中結(jié)束session:
<% session.invalidate(); %>
<c:redirect url="loginPage.jsf" />
2.如何在JSP頁(yè)面中訪問(wèn)web.xml中的初始化參數(shù)?
你可以使用預(yù)定義的JSF EL變量
initParam來(lái)訪問(wèn):
例如,如果你有:
<context-param>
<param-name>productId</param-name>
<param-value>2004Q4</param-value>
</context-param>
你可以使用她 #{initParam['productId']}來(lái)訪問(wèn) .例如:
Product Id: <h:outputText
value="#{initParam['productId']}"/>
3.如何從java代碼中訪問(wèn)web.xml
中的初始化參數(shù)?
你可以使用externalContext的
getInitParameter 方法得到他們.例如 如果你的參數(shù)如下:
<context-param>
<param-name>connectionString</param-name>
<param-value>jdbc:oracle:thin:scott/tiger@cartman:1521:O901DB</param-value>
</context-param>
你可以使用下面代碼訪問(wèn)connectionString :
FacesContext fc = FacesContext.getCurrentInstance();String
connection =
fc.getExternalContext().getInitParameter("connectionString");
4.如何從backing bean中得到當(dāng)前頁(yè)面的URL?
你可以通過(guò)FacesContext得到一個(gè)Http Request對(duì)象的引用,如下:
FacesContext fc =
FacesContext.getCurrentInstance();HttpServletRequest request =
(HttpServletRequest)
fc.getExternalContext().getRequest();
然后使用普通的request方法來(lái)得到路徑信息.還可以使用另外一種方法:
context.getViewRoot().getViewId();
將返回你當(dāng)前JSP(JSF view IDs 基本上只是JSP path
names)頁(yè)面的名字.
5.如何添加上下文路徑到outputLink的URL中?
在當(dāng)前的JSF實(shí)現(xiàn)中,當(dāng)在outputLink
中定義的路徑以'/'開(kāi)始時(shí),沒(méi)有添加上下文路徑到URL中,要彌補(bǔ)該問(wèn)題請(qǐng)?jiān)赨RL中使用
#{facesContext.externalContext.requestContextPath} 前綴.例如:
<h:outputLink
value="#{facesContext.externalContext.requestContextPath}/myPage.faces">
6.如何使用URL字符串來(lái)傳遞參數(shù)到JSF程序中?
如果你有下面的URL: http://your_server/your_app/product.jsf?id=777,
你可以使用下面的代碼來(lái)訪問(wèn)所傳遞的參數(shù):
FacesContext fc = FacesContext.getCurrentInstance();String id =
(String)
fc.getExternalContext().getRequestParameterMap().get("id");
在JSF頁(yè)面上,你也可以使用預(yù)定義的變量訪問(wèn)同樣的參數(shù),例如:
<h:outputText value="#{param['id']}" />
注意: 你必須直接調(diào)用該JSF頁(yè)面,并且使用servlet 映射 (mapping).
7.如何在頁(yè)面重新載入的時(shí)候保留h:inputSecret中的密碼?
設(shè)置redisplay=true, it is false by default.
8.如何使用h:outputText輸出HTML標(biāo)簽?
h:outputText有一個(gè) escape 屬性用來(lái)處理html
標(biāo)簽. 默認(rèn)值為true.這意味著所有特殊的符合都被轉(zhuǎn)義為'&'代碼.
請(qǐng)看下面示例: <h:outputText
value="<b>This is a text</b>"/>
打印的結(jié)果是: <b>This is a
text</b> 而 <h:outputText escape="false"
value="<b>This is a text</b>"/>
打印的結(jié)果是: This is a text
當(dāng)用戶點(diǎn)擊Command Link后如何顯示確認(rèn)對(duì)話框?
h:commandLink指定了 onclick 屬性為內(nèi)部使用. 因此你不可以使用她了,
該問(wèn)題已經(jīng)在JSF1.2中修復(fù)了,對(duì)于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.在調(diào)用ValueChangeListener
方法后如何重新裝載頁(yè)面?
在
ValueChangeListener的最后,調(diào)用
FacesContext.getCurrentInstance().renderResponse()
如何實(shí)現(xiàn)"請(qǐng)等待..."頁(yè)面?
在客戶端實(shí)現(xiàn)可能很簡(jiǎn)單.你可以包裝JSP頁(yè)面(或者你想要隱藏的一部分)到一個(gè)div中,然后你可以添加更多div,當(dāng)用戶點(diǎn)擊提交按鈕時(shí)這些div出現(xiàn).這些div可以包含gif動(dòng)畫(huà)和其他內(nèi)容.
場(chǎng)景:當(dāng)用戶點(diǎn)擊按鈕,調(diào)用JS函數(shù),該函數(shù)隱藏頁(yè)面并且顯示"請(qǐng)等待..."div.你可以使用CSS來(lái)自定義外觀:下面是一個(gè)正常工作的例子:
<%@ taglib uri="
<%@ taglib uri="
<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>
如果你想有一個(gè)動(dòng)畫(huà)gif圖片在"請(qǐng)等待..."中,當(dāng)表單提交后該圖片應(yīng)該從新加載.因此,再一次指定圖片的id,并且添加經(jīng)過(guò)一段時(shí)間延時(shí)后重新加載的代碼.下面是個(gè)例子:
<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">