JSF问题小结

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://your_server/your_app/product.jsf?id=777, 你可以使用下面的代码来访问所传递的参数:  

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" οnmοusedοwn="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 οnclick="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="/blog/animated.gif">

 [转自:http://www.ceapet.com/blog/index.php?option=com_content&view=article&id=185:jsf&catid=3:jboss&Itemid=62]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值