??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产精品一区在线观看不卡,亚洲欧洲综合,亚洲精品之草原avav久久http://www.aygfsteel.com/Vincent-chenxj/category/36153.htmlzh-cnTue, 02 Dec 2008 16:19:14 GMTTue, 02 Dec 2008 16:19:14 GMT60JSF问题集锦http://www.aygfsteel.com/Vincent-chenxj/articles/243633.htmlVincent-chenVincent-chenMon, 01 Dec 2008 01:55:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/243633.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/243633.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/243633.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/243633.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/243633.html1.如何l束session?   
  • 你可以用session?nbsp;invalidateҎ(gu) .   
  • 下面是一个从actionҎ(gu)中结束session的例? :   
  • public String logout() {    
      FacesContext fc 
    = FacesContext.getCurrentInstance();    
      HttpSession session 
    = (HttpSession) fc.getExternalContext().getSession(false);    
      session.invalidate();    
      
    return "login_page";    
    }
       

     

  • 下面的代码片D늤例了如何在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']}来访?nbsp;.例如:   
  • Product Id: <h:outputText value="#{initParam['productId']}"/>   
  •   
  •   
  • 3.如何从java代码中访问web.xml 中的初始化参?   
  • 你可以用externalContext?nbsp;getInitParameter Ҏ(gu)得到他们.例如 如果你的参数如下:   
  •  

    <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Ҏ(gu)来得到\径信?q可以用另外一U方?   
  • context.getViewRoot().getViewId();   
  • 返回你当前JSP(JSF view IDs 基本上只是JSP path names)面的名?   
  •   
  •   
  • 5.如何d上下文\径到outputLink的URL?   
  • 在当前的JSF实现?当在outputLink 中定义的路径?/'开始时,没有d上下文\径到URL?要I补该问题请在URL中?nbsp;#{facesContext.externalContext.requestContextPath} 前缀.例如:   
  • <h:outputLink value="#{facesContext.externalContext.requestContextPath}/myPage.faces">   
  •   
  •   
  • 6.如何使用URL字符串来传递参数到JSFE序?   
  • 如果你有下面的URL: http://your_server/your_app/product.jsf?id=777, 你可以用下面的代码来访问所传递的参数:     
  • FacesContext fc = FacesContext.getCurrentInstance();String id = (String) fc.getExternalContext().getRequestParameterMap().get("id");    
  • 在JSF面?你也可以使用预定义的变量讉K同样的参?例如:    
  • <h:outputText value="#{param['id']}" />   
  • 注意: 你必ȝ接调用该JSF面,q且使用servlet 映射 (mapping).   
  •   
  •   
  • 7.如何在页面重新蝲入的时候保留h:inputSecret中的密码?   
  • 讄redisplay=true, it is false by default.   
  •     
  •   
  • 8.如何使用h:outputText输出HTML标签?   
  •   
  • h:outputText有一?nbsp; escape 属性用来处理html 标签. 默认gؓ(f)true.q意味着所有特D的W合都被转义?&'代码. L(fng)下面CZ:  <h:outputText value="<b>This is a text</b>"/> 打印的结果是:  <b>This is a text</b>  ?nbsp;<h:outputText escape="false" value="<b>This is a text</b>"/>  打印的结果是:  This is a text  当用L(fng)击Command Link后如何显C确认对话框?   
  • h:commandLink指定?nbsp;onclick 属性ؓ(f)内部使用. 因此你不可以使用她了, 该问题已l在JSF1.2中修复了,对于JSF1.2以前的版?你可以在onclick以前使用  onmousedown 事g  <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 Ҏ(gu)后如何重新装载页?   
  • ?nbsp;ValueChangeListener的最?调用  FacesContext.getCurrentInstance().renderResponse()   
  • 如何实现"L(fng)?.."面? 在客L(fng)实现可能很简?你可以包装JSP面(或者你惌隐藏的一部分)C个div?然后你可以添加更多div,当用L(fng)?yn)L交按钮时q些div出现.q些div可以包含gif动画和其他内? 场景:当用L(fng)?yn)L?调用JS函数,该函数隐藏页面ƈ且显C?/span>"L(fng)?.."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囄?/span>"L(fng)?.."?当表单提交后该图片应该从新加?因此,再一ơ指定图片的id,q且dl过一D|间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">   


    Vincent-chen 2008-12-01 09:55 发表评论
    ]]>
    jsfl束sessionhttp://www.aygfsteel.com/Vincent-chenxj/articles/243401.htmlVincent-chenVincent-chenSat, 29 Nov 2008 05:06:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/243401.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/243401.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/243401.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/243401.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/243401.html 下面是一个从actionҎ(gu)中结束session的例? :

     

    public String logout() {

      FacesContext fc 
    = FacesContext.getCurrentInstance();
      HttpSession session 
    = (HttpSession) fc.getExternalContext().getSession(false);
      session.invalidate();
      
    return "login_page";
    }
      

     

    下面的代码片D늤例了如何在JSP面中结束session:

    
    
    <% session.invalidate(); %>
    <c:redirect url="loginPage.jsf" /> 


    Vincent-chen 2008-11-29 13:06 发表评论
    ]]>
    生成Excelq在客户端下载的JSF实现http://www.aygfsteel.com/Vincent-chenxj/articles/242968.htmlVincent-chenVincent-chenThu, 27 Nov 2008 03:36:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242968.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242968.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242968.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242968.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242968.html阅读全文

    Vincent-chen 2008-11-27 11:36 发表评论
    ]]>
    JSF实现文g下蝲http://www.aygfsteel.com/Vincent-chenxj/articles/242959.htmlVincent-chenVincent-chenThu, 27 Nov 2008 03:25:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242959.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242959.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242959.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242959.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242959.html 用户从页面点击连接(linkQ,下蝲相关的文Ӟ该文件存在服务器端,或者由服务器端生成文g,没有物理文gQ下载后面不蟩转?br />
    JSP代码Q?/strong>


    <h:commandLink actionListener="#{productBean.downloadAction}" styleClass="highLightLink">
       <h:outputText value="download"/>
       <f:param name="productId" value="#{productBean.id}"/>
    </h:commandLink>



    Backing bean设计?qing)代码?x)
    注意q是个Action listenerҎ(gu)Q没有返回|q且有javax.faces.event.ActionEvent参数


       public void downloadAction(ActionEvent event) {
        try {
          String fileName="D:\\temp\\images\\products\\" + this.id + ".xls";
          logger.debug("file name=" + fileName);
          ByteArrayOutputStream baos=this.serviceLocator.getFileService().downloadFile(fileName); //调用ServiceҎ(gu)Q获得文件的ByteArrayOutputStream
          HttpServletResponse response=FacesUtils.getServletResponse();
          response.setHeader("Content-disposition", "attachment; filename=" + id+ ".xls" ); //不是内嵌昄(inline)Q而是作ؓ(f)附g下蝲
          response.setContentLength(baos.size());
          ServletOutputStream sos=response.getOutputStream();
          baos.writeTo(sos);
          baos.close();
          sos.flush();
        } catch (IOException ex) {      
          logger.debug(ex);
        }  
      }



    Service代码Q?/strong>
    q个实现是一个从已经存在的物理文件获得输出流的范例,至于由Server在内存中生成输出也是一样处理,例如生成一个Excel文gQ再让用户下载?br />

    public ByteArrayOutputStream downloadFile(String fileName) throws IOException {
        FileInputStream fis=new FileInputStream(fileName);
        BufferedInputStream bis=new BufferedInputStream(fis);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        BufferedOutputStream bos=new BufferedOutputStream(baos);
        
        int i;
        while((i=bis.read())!=-1) {
          bos.write(i);
        }
        bos.flush();//提交文g,很关?br />     bis.close();
        return baos;
      }



    Vincent-chen 2008-11-27 11:25 发表评论
    ]]>
    JSF 文g上传http://www.aygfsteel.com/Vincent-chenxj/articles/242953.htmlVincent-chenVincent-chenThu, 27 Nov 2008 03:17:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242953.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242953.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242953.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242953.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242953.html后台实现c:(x)

     

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import javax.faces.context.FacesContext;

    import org.apache.myfaces.custom.fileupload.UploadedFile;


    public class Fileupload
    {
        
    private UploadedFile _upFile;
        
    private String _name = "";

       
    public UploadedFile getUpFile()
        
    {
            
    return _upFile;
        }


     

    public void setUpFile(UploadedFile upFile)    
        
    {
            _upFile 
    = upFile;
        }


        
    public String getName()
        
    {
            
    return _name;
        }


        
    public void setName(String name)
        
    {
            _name 
    = name;
        }


        
    public String upload() throws IOException
        
    {
            FacesContext facesContext 
    = FacesContext.getCurrentInstance();
            
    int start=_upFile.getName().lastIndexOf("\\")==-1?_upFile.getName().lastIndexOf("\\"):_upFile.getName().lastIndexOf("\\")+1;
            String file
    =_upFile.getName().substring(start,_upFile.getName().length());
            String path
    =Fileupload.class.getResource("/").getPath().substring(1,Fileupload.class.getResource("/").getPath().length());
            path
    =path.replace("%20"" ")+file;
      
    try {
       FileOutputStream keyFOS 
    = new FileOutputStream(path);
       keyFOS.write(_upFile.getBytes());
       keyFOS.close();
      }
     catch (Exception e) {
       System.out.println(
    "lalala");
      }

      
            facesContext.getExternalContext().getApplicationMap().put(
    "fileupload_bytes", _upFile.getBytes());
            facesContext.getExternalContext().getApplicationMap().put(
    "fileupload_type", _upFile.getContentType());
            facesContext.getExternalContext().getApplicationMap().put(
    "fileupload_name", _upFile.getName());
            
    return path;
        }


    public boolean isUploaded()
        
    {
            FacesContext facesContext 
    = FacesContext.getCurrentInstance();
            
    return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes")!=null;
        }

    }


     

    前台实现QMyfaces

     

    <t:inputFileUpload id="fileupload"
                                   value
    ="#{action$fixing.myFile}"         
                                   storage
    ="file"
                                   styleClass
    ="fileUploadInput"
                                   maxlength
    ="200000"/>
          
    <h:commandButton value="#{prompt['Submit']}"
           action
    ="#{action$fixing.sel}" />

     

     

    后台理BeanQ?/span>

    import org.apache.myfaces.custom.fileupload.UploadedFile;

     

    public class FixingAction{

    private UploadedFile myFile;

     

    public UploadedFile getMyFile() {
      return myFile;
     }

     

     

    public void setMyFile(UploadedFile myFile) {
      this.myFile = myFile;
     }


        public String sel(){
           Fileupload file=new Fileupload();

    String path=file.setUpFile(this.myFile);

    System.out.println("文g存放路径Q?+path);

    return null;

      }
    }


    Vincent-chen 2008-11-27 11:17 发表评论
    ]]>
    jsf 另存为的实现http://www.aygfsteel.com/Vincent-chenxj/articles/242946.htmlVincent-chenVincent-chenThu, 27 Nov 2008 02:39:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242946.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242946.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242946.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242946.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242946.html 

    public String btnSaveXML_action() {
            
    // TODO: Process the action. Return value is a navigation
            
    // case name where null will return to the same page.
            try {
                
    //get the appId
                Integer appId = Integer.valueOf(txtMemory1.getText().toString());
                String appName 
    = datamanagementservice.getAppNameByAppId(appId);
                Document doc 
    = writeXML(appId, appName);
                TransformerFactory tFactory 
    = TransformerFactory.newInstance();
                Transformer transformer 
    = tFactory.newTransformer();
                DOMSource source 
    = new DOMSource(doc);

                
    //the file's loadpath
                String loadPath = "c:\\temp\\" + appName + ".xml";
                
    //if the file no exist then create it
                File f = new File("c:\\temp\\");
                f.mkdirs();
                StreamResult result 
    = new StreamResult(new java.io.File(loadPath));
                transformer.transform(source, result);

                
    //new a download file
                File t_file = new File(loadPath);
                
    long l;
                
    for (l = 0L; l == 0L; l = t_file.length()) {

                    t_file 
    = new File(loadPath);
                }

                InputStream in 
    = new FileInputStream(t_file);
                
    if (in != null{
                    String filename 
    = t_file.getName();
                    filename 
    = java.net.URLEncoder.encode(filename, "utf-8");

                    FacesContext.getCurrentInstance().responseComplete();
                    HttpServletResponse httpServletResponse 
    = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                    httpServletResponse.reset();
                    httpServletResponse.setContentType(
    "application/x-msdownload");
                    String rrr 
    = "attachment; filename=" + filename;
                    httpServletResponse.setHeader(
    "Content-Disposition", rrr);
                    httpServletResponse.setContentLength((
    int) l);

                    
    byte b[] = new byte[2048];
                    
    for (int len = 0; (len = in.read(b)) > 0;) {
                        httpServletResponse.getOutputStream().write(b, 
    0, len);
                    }

                    in.close();
                }

            }
     catch (Exception e) {
                e.printStackTrace();
            }

            getRequestBean1().setSelectAppId((Integer) dropDown1.getSelected());

            
    return null;
        }


    Vincent-chen 2008-11-27 10:39 发表评论
    ]]>
    JSF和Richfaces中用框枉(Frameset)Q包括了PanelMenu http://www.aygfsteel.com/Vincent-chenxj/articles/242064.htmlVincent-chenVincent-chenSat, 22 Nov 2008 17:05:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242064.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242064.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242064.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242064.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242064.htmlW一U,普通的Ҏ(gu):
    惛_JSF控g中操作框枉FramesetQ可以在左框枉QQ一框架)里声明一个FormQ设定好它的target="mainFrame"QQ一框架늚idQ,q样当点击这个Form里面的CommandButton或者CommandLink的时候,JSF处理好业务逻辑后,可以将To-View-ID指定的页面{C框架里面去昄了?br />

    <h:form target="mainFrame">
    <h:commandButton action="index">
    </h:form

    mainFrame

    点击左边的commandButton之后Qindex处理之后的To-View-ID指定的页面就?x)显C在mainFrame里面?

    W二U方法:(x)
            <script>
                    
    function forward(p) {
                         window.parent.mainFrame.location.href
    ="usermanagement/" + p + ".faces"
    ;
                     }

            
    </script>
    d一个蟩转函敎ͼ然后分情冉|加蟩转:(x)
    当不需要Action的时?
    <rich:panelMenuItem label="角色信息l护" onclick="forward('rolelist')" />
    q样可以蟩转了

    当需要先q行Action操作再蟩转的时?br />
    <rich:panelMenuItem label="新徏角色"
                                    action
    ="#{user.addRoleMain}" oncomplete="forward('roledetail')" />
        public String addRoleMain()
        
    {
            ClearRole();
            
    return null
    ;
        }

    q样Q在Action完成后,菜单不?x)变Q主框架会(x)跌{到指定页面?br />
    OK,上面两种Ҏ(gu)基本能满_发需要了?


    Vincent-chen 2008-11-23 01:05 发表评论
    ]]>
    JSF(Java Server Faces)的StateHolder的作用和使用Ҏ(gu) http://www.aygfsteel.com/Vincent-chenxj/articles/242063.htmlVincent-chenVincent-chenSat, 22 Nov 2008 17:04:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242063.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242063.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242063.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242063.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242063.html阅读全文

    Vincent-chen 2008-11-23 01:04 发表评论
    ]]>
    在JSF控g中操作框枉Frameset http://www.aygfsteel.com/Vincent-chenxj/articles/242051.htmlVincent-chenVincent-chenSat, 22 Nov 2008 16:42:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242051.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242051.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242051.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242051.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242051.html
    惛_JSF控g中操作框枉FramesetQ可以在左框枉QQ一框架)里声明一个FormQ设定好它的target="mainFrame"QQ一框架늚idQ,q样当点击这个Form里面的CommandButton或者CommandLink的时候,JSF处理好业务逻辑后,可以将To-View-ID指定的页面{C框架里面去昄了?br />

    <h:form target="mainFrame">
    <h:commandButton action="index">
    </h:form

    mainFrame

    点击左边的commandButton之后Qindex处理之后的To-View-ID指定的页面就?x)显C在mainFrame里面?


    Vincent-chen 2008-11-23 00:42 发表评论
    ]]>
    JSF面中支持的Expression Language(EL) http://www.aygfsteel.com/Vincent-chenxj/articles/242050.htmlVincent-chenVincent-chenSat, 22 Nov 2008 16:39:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242050.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242050.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242050.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242050.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242050.html以下是可使用的EL帔R的列表:(x)




    Vincent-chen 2008-11-23 00:39 发表评论
    ]]>
    后来E序控制前台UI控g的方?http://www.aygfsteel.com/Vincent-chenxj/articles/242049.htmlVincent-chenVincent-chenSat, 22 Nov 2008 16:38:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242049.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242049.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242049.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242049.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242049.htmlW一U方法:(x)
    JSF面中的代码Q?br />
    <h:form id="messageForm">
    <h:outputText id="outputMessage"/>
    <h:commandButton value="Get Message"
    actionListener
    ="#{testForm.sendMessage}"/>
    </h:form>
    后台E序Q?br />
    public void sendMessage(ActionEvent e)
    {
    FacesContext context 
    = FacesContext.getCurrentInstance();
    UIViewRoot view 
    = context.getViewRoot();
    HtmlOutputText output 
    =
    (HtmlOutputText)view.findComponent(
    "messageForm:outputMessage");
    output.setStyle(
    "color:blue");
    output.setValue(
    "Who's the Mann?");
    }
    之后Q当点击Get Message的时候页面就?x)在outputMessage的地方输?Who's the Mann?
    W二U方法:(x)
    <h:inputText value="#{registrationForm.comments}"
    binding
    ="#{registrationForm.commentsInput}"/>
    commentsInput是RegistrationForm中的一个HtmlInputTextcd的一个变量,通过binding属性将前台UI控g和后台同cd的变量绑定,可以在后台直接操作了?/span>

    Vincent-chen 2008-11-23 00:38 发表评论
    ]]>
    Java Server Faces Events And Listeners (JSF中的事g与监听器)http://www.aygfsteel.com/Vincent-chenxj/articles/242048.htmlVincent-chenVincent-chenSat, 22 Nov 2008 16:37:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242048.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242048.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242048.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242048.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242048.htmlJSF的一个核心就是事件与监听。JSF事g分ؓ(f)以下几种Q?br /> 1、Value-change eventsQ值改变事Ӟ

    <h:inputText valueChangeListener="#{myForm.processValueChanged}"/>
    <h:panelGrid binding="#{myForm.changePanel}" rendered="false">

    </h:panelGrid>


     

    public void processValueChanged(ValueChangeEvent event)
    {
    HtmlInputText sender 
    =
     (HtmlInputText)event.getComponent();
    sender.setReadonly(
    true
    );
    changePanel.setRendered(
    true
    );
    }

    当inputText的值改变的时候会(x)触发processValueChanged事gQ此事情内部通过参数Q获取HtmlInputText的控Ӟ然后讑֮readonlyQ同时设定panelGrid的模型changePanel的属性,因ؓ(f)panelGridl定了changePanelq个模型?br />
    2、Action events(动作事g)

    <h:commandButton type="submit" value="Login" action="success"
    immediate
    ="true"/>

    上面q个是静态outcome事gQ在faces-config.xml中配|相应的属性后Q点击该按钮?x){向相应的Ҏ(gu)?br />

    <h:commandButton value = "Test Action Listener" actionListener = "#{testActionListener.doSomeAction}" />

    如果d了actionListener属性的话,点击之后Q在转向其他面之前Q会(x)触发指定的事Ӟ处理之后再{向?br /> q里的doSomeActionҎ(gu)的声明是void doSomeAction(ActionEvent actionEvent)?br />

    <h:commandButton type="submit" value="Login"
    action
    ="#{loginForm.login}"/>

    上面q个是动态outcome事gQ当点击该按钮之后会(x)直接触发loginForm中的login事gQlogin事gq回的是一个字W串Q在login处理完相应操作之后需要返回一个属性中已经配置q的字符Ԍ例如successQ然后JSFҎ(gu)faces-config.xml中的配置Q蟩转到相应的页面。login的声明一般是String login();

    当然也可以通过E序来添加监听器Q不q监听器c需要实现ActionListener接口Q?br />

    UIComponent myComponent = new UIComponent();
    myComponent.addActionListener(
    new
     TestActionListener());
    myComponent.addActionListener(
    new
     CustomActionListener());


    3、Data model eventsQ数据模型事Ӟ

    FacesContext facesContext = FacesContext.getCurrentInstance();
    dataTable 
    =
     (HtmlDataTable)facesContext.getApplication().createComponent(
    HtmlDataTable.COMPONENT_TYPE);
    DataModel myDataModel 
    = new
     ResultSetDataModel(myResultSet);
    myDataModel.addDataModelListener(
    new
     DataModelListener()
    {
    public void
     rowSelected(DataModelEvent e)
    {
    FacesContext.getCurrentInstance().getExternalContext().
    log(
    "row selected:" +
     e.getRowIndex());
    }

    }
    );
    dataTable.setValue(myDataModel);

    上面的代码攑օ初始化程序中Q当行的选择改变的时候就?x)触发相应的日志打印?br /> q是因ؓ(f)q段代码ldataTable控g定义了一个数据模型,数据和相应的事g全部存在q个数据模型里面Q然后将dataTable与这个模型进行绑定即可用它所有的数据和事件。点击dataTable上面的一行自然也׃(x)出发相应的行选择事g?

    4.Phase eventsQ阶D事Ӟ

    q个事g是ؓ(f)了捕捉一些阶D和q程而设定的Q有以下几个阶段可以捕捉Q?br />

     

    ANY_PHASE M阶段
    APPLY_REQUEST_VALUES  h处理阶段的应用请求值阶D?br /> INVOKE_APPLICATION h处理阶段的调用程序阶D?br /> PROCESS_VALIDATIONS h处理阶段的处理验证阶D?br /> RENDER_RESPONSE  h处理阶段的描q响应阶D?br /> RESTORE_VIEW h处理阶段的恢复视ND?br /> UPDATE_MODEL_VALUES h处理阶段的更新模型值阶D?/span>

     

    具体可以参?/span>http://java.sun.com/javaee/javaserverfaces/1.1_01/docs/api/javax/faces/event/PhaseId.html

    通过讑֮在实现类中设定相应的PhaseId以及(qing)d到faces-config.xml中就可以捕捉相应阶段事gq添加响应的代码

    实现c?/span>

     

    import javax.faces.event.PhaseEvent;
    import
     javax.faces.event.PhaseId;
    import
     javax.faces.event.PhaseListener;

    public class CustomPhaseListener implements
     PhaseListener
    {    
        
    public
     CustomPhaseListener()
        
    {
        }


        
    public void afterPhase(PhaseEvent event)
        
    {
            System.out.println(
    "After Phase->" +
     event.getPhaseId());    
        }


        
    public void beforePhase(PhaseEvent event)
        
    {
            System.out.println(
    "Before Phase->" +
     event.getPhaseId());
        }


        
    public PhaseId getPhaseId()
        
    {
            
    return PhaseId.ANY_PHASE;//选择事g标识的常量,此ؓ(f)M阶段

        }
        
    }


    在faces-config.xml中添?br />

    <lifecycle>
        
    <phase-listener>
            net.javabeat.articles.jsf.events.actions.TestActionListener
        
    </phase-listener>
    </lifecycle>


    当然可以d无限个的监听?br />

    <lifecycle>
        
    <phase-listener>PhaseListener1</phase-listener>
        
    <phase-listener>PhaseListener2</phase-listener>
    </lifecycle>


    下面是请求处理的生命周期Q大家可以直观的看一下各个阶D|行顺序:(x)
     
    各个阶段的说明以?qing)所能触发的事g
     



    Vincent-chen 2008-11-23 00:37 发表评论
    ]]>
    JSF与后台程序的交互 http://www.aygfsteel.com/Vincent-chenxj/articles/242045.htmlVincent-chenVincent-chenSat, 22 Nov 2008 16:21:00 GMThttp://www.aygfsteel.com/Vincent-chenxj/articles/242045.htmlhttp://www.aygfsteel.com/Vincent-chenxj/comments/242045.htmlhttp://www.aygfsteel.com/Vincent-chenxj/articles/242045.html#Feedback0http://www.aygfsteel.com/Vincent-chenxj/comments/commentRss/242045.htmlhttp://www.aygfsteel.com/Vincent-chenxj/services/trackbacks/242045.html如果Managed-Bean已经被创建ƈ存在Q则可以通过下面的Ş式访问session范围内的Managed-Bean
    org.test.TestBean x = (org.test.TestBean) request.getSession().getAttribute("testBean");


    如果不存在,则需要先讉K一下Managed-BeanQ让他被触发而创?br />
    org.test.TestBean x = (org.test.TestBean) Util.getBean("testBean"); 

    //In Util.java:

    public static Object getBean(String expr){
    FacesContext context 
    =
     FacesContext.getCurrentInstance();    
    Application app 
    =
     context.getApplication(); 
    ValueBinding binding 
    = app.createValueBinding("#{" + expr + "}"
    );
    Object value 
    =
     binding.getValue(context);  
    return
     value;
    }


    为JSF指定ActionQ可以这样写Q?br />
                 <h:commandButton action="#{user.verify}" value="Say Hello!">                 
                 
    </h:commandButton>
    UsercM的代?
        public String verify()
        
    {
            
    return "hello";
        }
    faces-config.xml?
     <managed-bean>
      
    <description>User Name Bean</description>
      
    <managed-bean-name>user</managed-bean-name>
      
    <managed-bean-class>demo.User</managed-bean-class>
      
    <managed-bean-scope>session</managed-bean-scope>
     
    </managed-bean>
     
    <navigation-rule>
      
    <from-view-id>/pages/inputUserName.jsp</from-view-id>
      
    <navigation-case>
       
    <from-outcome>hello</from-outcome>
       
    <to-view-id>/pages/hello.jsp</to-view-id>
      
    </navigation-case>
     
    </navigation-rule>
    q样Q点?yn)L钮,׃(x)转到verifyҎ(gu)中进行验证,然后q回相应l果的字W串QjsfҎ(gu)q回的结果进行相应的面跌{?br />

    Vincent-chen 2008-11-23 00:21 发表评论
    ]]>
    վ֩ģ壺 | ٰ| | | | ɽ| ˷| ƽ| | ǹ| | ɽ| Ҷ| Ϳ| | Ϻӿ| | | | | | | ʤ| | | Ȫ| | | Ȫ| | | ɽ| | ֣| ǰ| ܿ| ͼ| | ƺ| | ÷|