`
zengjinliang
  • 浏览: 301700 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

JSF问题集锦-General Questions

    博客分类:
  • jsf
阅读更多
java 代码
  1. 1.如何结束session?   
  2. 你可以使用session的 invalidate方法 .   
  3. 下面是一个从action方法中结束session的例子: :   
  4. public String logout() {   
  5.   FacesContext fc = FacesContext.getCurrentInstance();   
  6.   HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);   
  7.   session.invalidate();   
  8.   return "login_page";   
  9. }    
  10. 下面的代码片段示例了如何在JSP页面中结束session:   
  11. <% session.invalidate(); %>   
  12. <c:redirect url="loginPage.jsf" />   
  13.   
  14.   
  15. 2.如何在JSP页面中访问web.xml中的初始化参数?   
  16. 你可以使用预定义的JSF EL变量  initParam来访问:   
  17. 例如,如果你有:   
  18. <context-param>   
  19.  <param-name>productId</param-name>   
  20.  <param-value>2004Q4</param-value>   
  21. </context-param>   
  22. 你可以使用她 #{initParam['productId']}来访问 .例如:   
  23. Product Id: <h:outputText value="#{initParam['productId']}"/>   
  24.   
  25.   
  26. 3.如何从java代码中访问web.xml 中的初始化参数?   
  27. 你可以使用externalContext的 getInitParameter 方法得到他们.例如 如果你的参数如下:   
  28. <context-param>   
  29.  <param-name>connectionString</param-name>   
  30.  <param-value>jdbc:oracle:thin:scott/tiger@cartman:1521:O901DB</param-value>   
  31. </context-param>   
  32. 你可以使用下面代码访问connectionString :   
  33. FacesContext fc = FacesContext.getCurrentInstance();String connection = fc.getExternalContext().getInitParameter("connectionString");    
  34.   
  35.   
  36. 4.如何从backing bean中得到当前页面的URL?   
  37. 你可以通过FacesContext得到一个Http Request对象的引用,如下:   
  38. FacesContext fc = FacesContext.getCurrentInstance();HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();    
  39. 然后使用普通的request方法来得到路径信息.还可以使用另外一种方法:   
  40. context.getViewRoot().getViewId();   
  41. 将返回你当前JSP(JSF view IDs 基本上只是JSP path names)页面的名字.   
  42.   
  43.   
  44. 5.如何添加上下文路径到outputLink的URL中?   
  45. 在当前的JSF实现中,当在outputLink 中定义的路径以'/'开始时,没有添加上下文路径到URL中,要弥补该问题请在URL中使用 #{facesContext.externalContext.requestContextPath} 前缀.例如:   
  46. <h:outputLink value="#{facesContext.externalContext.requestContextPath}/myPage.faces">   
  47.   
  48.   
  49. 6.如何使用URL字符串来传递参数到JSF程序中?   
  50. 如果你有下面的URL: http://your_server/your_app/product.jsf?id=777, 你可以使用下面的代码来访问所传递的参数:     
  51. FacesContext fc = FacesContext.getCurrentInstance();String id = (String) fc.getExternalContext().getRequestParameterMap().get("id");    
  52. 在JSF页面上,你也可以使用预定义的变量访问同样的参数,例如:    
  53. <h:outputText value="#{param['id']}" />   
  54. 注意: 你必须直接调用该JSF页面,并且使用servlet 映射 (mapping).   
  55.   
  56.   
  57. 7.如何在页面重新载入的时候保留h:inputSecret中的密码?   
  58. 设置redisplay=true, it is false by default.   
  59.     
  60.   
  61. 8.如何使用h:outputText输出HTML标签?   
  62.   
  63. 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后如何显示确认对话框?   
  64. 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>   
  65. <h:commandLink action="delete" onmousedown="return ConfirmDelete(this);">  <h:outputText value="delete it"/></h:commandLink>   
  66.     
  67.   
  68. 9.在调用ValueChangeListener 方法后如何重新装载页面?   
  69. 在 ValueChangeListener的最后,调用  FacesContext.getCurrentInstance().renderResponse()   
  70. 如何实现"请等待..."页面? 在客户端实现可能很简单.你可以包装JSP页面(或者你想要隐藏的一部分)到一个div中,然后你可以添加更多div,当用户点击提交按钮时这些div出现.这些div可以包含gif动画和其他内容. 场景:当用户点击按钮,调用JS函数,该函数隐藏页面并且显示"请等待..."div.你可以使用CSS来自定义外观:下面是一个正常工作的例子: <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   
  71. <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>   
  72. <f:loadBundle basename="demo.bundle.Messages" var="Message"/>   
  73. <html>   
  74. <head>   
  75.   <title>Input Name Page</title>   
  76.   <script>   
  77.     function gowait() {   
  78.       document.getElementById("main").style.visibility="hidden";   
  79.       document.getElementById("wait").style.visibility="visible";   
  80.     }   
  81.    </script>   
  82.       
  83.  </head>   
  84.  <body bgcolor="white">   
  85.   <f:view>   
  86.     <div id="main">   
  87.        <h1><h:outputText value="#{Message.inputname_header}"/></h1>   
  88.        <h:messages style="color: red"/>   
  89.        <h:form id="helloForm">   
  90.          <h:outputText value="#{Message.prompt}"/>   
  91.          <h:inputText id="userName" value="#{GetNameBean.userName}" required="true">   
  92.            <f:validateLength minimum="2" maximum="20"/>   
  93.          </h:inputText>   
  94.          <h:commandButton onclick="gowait()" id="submit"  
  95.                action="#{GetNameBean.action}" value="Say Hello" />   
  96.        </h:form>   
  97.     </div>   
  98.     <div id="wait" style="visibility:hidden; position: absolute; top: 0; left: 0">   
  99.        <table width="100%" height ="300px">   
  100.          <tr>   
  101.            <td align="center" valign="middle">   
  102.              <h2>Please, wait...</h2>   
  103.            </td>   
  104.          </tr>   
  105.        </table>   
  106.     </div>   
  107.   </f:view>   
  108.  </body>   
  109. </html>    
  110.   
  111. 如果你想有一个动画gif图片在"请等待..."中,当表单提交后该图片应该从新加载.因此,再一次指定图片的id,并且添加经过一段时间延时后重新加载的代码.下面是个例子: <script>   
  112.  function gowait() {   
  113.    document.getElementById("main").style.visibility="hidden";   
  114.    document.getElementById("wait").style.visibility="visible";   
  115.    window.setTimeout('showProgress()', 500);   
  116.  }   
  117.   function showProgress(){   
  118.    var wg = document.getElementById("waitgif");   
  119.    wg.src=wg.src;   
  120.  }   
  121. </script>   
  122. ....   
  123. <img id="waitgif" src="animated.gif">   
  124.   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics