【轉】 liferay5.1.2 筆記

1、 HttpSessionListenerjavascript

<listener>
    <listener-class>com.liferay.portal.kernel.servlet.PortletSessionListenerManager</listener-class>
</listener>

liferay中,使用PortletSessionListenerManager實現HttpSessionListenerjava


2、 ServletContextListener接口的實現與應用web

ServletContextListener接口有兩方須要實現的方法:contextInitialized()contextDestroyed();spring

它會監聽Servlet容器,當應用開始的時候它會調用contextInitialized()方法;當應用關閉的時候,它一樣會調用contextDestroyed()方法.app

ServletContext對象是一個爲整個web應用提供共享的內存,任何請求均可以訪問裏面的內容。jsp

如何實如今服務啓動的時候就動態的加入到裏面的內容:咱們須要作的有:ide

1 實現servletContextListerner接口 並將要共享的經過setAttributename,data)方法提交到內存中去。工具

2)應用項目在經過getAttribute(name)將數據或到。網站

public class ServletContextLTest implements ServletContextListener{  }this

web.xml配置文件中加入

<listener>
    <listener-class>ServletContextTest.ServletContextLTest</listener-class>
</listener>

public class CreateEmployee extends HttpServlet{
       protected void service(){
              ServletContext sct=getServletConfig().getServletContext();  
              Map<Integer,String> dept=(Map<Integer,String>)sct.getAttribute("dept");   
    }
}

這樣監視器就設置好了如下通用應用調用上面的數據。

liferay中,使用PortalContextLoaderListener繼承ContextLoaderListener(是一個spring中的類,所以使用initWebApplicationContext加載),ContextLoaderListener實現了ServletContextListener接口。

 

3、 web.xml中的參數context-paraminit-param區別

      3.1) application範圍內的參數,存放在servletcontext中,在web.xml中配置以下:

<context-param>  
    <param-name>context/param</param-name>  
    <param-value>avalible during application</param-value>  
</context-param>

       3.2) servlet範圍內的參數,只能在servletinit()方法中取得,在web.xml中配置以下:

Java代碼

<servlet>  
    <servlet-name>MainServlet</servlet-name>  
    <servlet-class>com.wes.controller.MainServlet</servlet-class>  
    <init-param>  
       <param-name>param1</param-name>  
       <param-value>avalible in servlet init()</param-value>  
    </init-param>  
    <load-on-startup>0</load-on-startup>  
</servlet>

第一種參數在servlet裏面能夠經過getServletContext().getInitParameter("context/param")獲得。

第二種參數只能在servletinit()方法中經過this.getInitParameter("param1")取得。


4、UrlRewriteFilter的使用

1. web.xml中配置filter,使用UrlRewriteFilter對一些特定的url進行過濾轉發     

<filter>
              <filter-name>URL Rewrite Filter</filter-name>
              <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
              <init-param>
                     <param-name>logLevel</param-name>
                     <param-value>ERROR</param-value>
              </init-param>
              <init-param>
                     <param-name>statusEnabled</param-name>
                     <param-value>false</param-value>
              </init-param>
       </filter>
       <filter-mapping>
              <filter-name>URL Rewrite Filter</filter-name>
              <url-pattern>/*</url-pattern>
       </filter-mapping>

         2) WEB-INF下面配置urlrewrite.xml,這個文件

<urlrewrite>
       <rule>
              <from>(.*)/blog/blogs/rss(.*)</from>
              <to type="permanent-redirect">$1/blog/-/blogs/rss$2</to>
       </rule>
       <rule>
              <from>^/c/journal/view_article_content/?groupId=14&amp;articleId=155291$</from>
              <to type="permanent-redirect">/web/guest/home/-/journal/rss/14/news</to>
       </rule>
       <rule>
              <from>^/web/guest/community/forums/message_boards(.*)$</from>
              <to type="permanent-redirect">/web/guest/community/forums/-/message_boards$1</to>
       </rule>
       <rule>
              <from>^/web/guest/home/journal/rss/14/news$</from>
              <to type="permanent-redirect">/web/guest/home/-/journal/rss/14/news</to>
       </rule>
</urlrewrite>

 

5、如何實現一個過濾器

1.      所在的類實現Filter接口

package cn.mldn.lxh.filter 
import java.io.*; 
import javax.servlet.*; 
public class FirstFilter implements Filter { 
    public void init(FilterConfig config)throws ServletException    { 
          System.out.println("過濾器初始化");
          String 參數值 = config.getInitParameter("參數名稱");
     } 
  public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)  throws IOException,ServletException { 
         System.out.println(過濾doFilter); 
    } 
      public void destroy(){ 
         System.out.println(過濾器銷燬); 
    } 
}


2.      web.xml文件配置

<filter> 
    <filter-name>first </filter-name> 
    <filter-class>cn.mldn.lxh.filter.FirstFilter </filter-class>
    <init-param> 
         <param-name>參數名稱 </param-name> 
         <param-value>參數值 </param-value> 
    </init-param>
</filter> 
<filter-mapping> 
    <filter-name>first </filter-name>
    <url-pattern>/* </url-pattern> 
</filter-mapping>


6、Easyconf組件的使用

能夠參考這個網站http://easyconf.sourceforge.net/

主要是一個用來讀取配置文件的工具。

private ComponentProperties getProperties() {
       return EasyConf.getComponent("my-component").getProperties();
    }
    //read a propertie
    String skin = getProperties().getString("skin");
    //using default value
    String skin = getProperties().getString("skin", "blue");
    // behaviour when a property does not exist
      (Return a default value, or throw exception)
    //numeric property types
    value = getProperties().getBigDecimal("big-decimal");
    value = getProperties().getBigInteger("big-integer");
    value = getProperties().getBoolean("boolean");
    value = getProperties().getByte("byte");
    value = getProperties().getDouble("double");
    value = getProperties().getFloat("float");
    value = getProperties().getInteger("integer");
    value = getProperties().getLong("long");
    value = getProperties().getShort("short");
    //Multivaluated properties
    List values = getProperties().getList("multivaluated");
    String[] array = getProperties().getStringArray("multivaluated");
   // as a result of this automatic conversion if the value of a single valued property contains commas 
   // they must be scaped with a double slash. Example
   // single-valued=one//,two//,three
   //Properties with class names
    database-configuration-class=com.germinus.easyconf.example.DatabaseConf
    Class configuredClass=getProperties().getClass("database-configuration-class");
    //Including other files  special property include-and-override
    include-and-override=external-file.properties
    include-and-override=mycomponent-${environment.name}.properties
    include-and-override=external-file1.properties,external-file2.properties
    include-and-override=external-file3.properties


九.       Liferay屬性文件的加載

liferay中屬性文件的加載主要有以下幾個類:

com.liferay.portal.util.PropsFiles類中包含以下三個靜態變量,指明配置文件名稱:    

public static final String CAPTCHA = "captcha";
public static final String CONTENT_TYPES = "content-types";
public static final String PORTAL = "portal";

com.liferay.portal.kernel.configuration.Configuration接口類,向外提供配置服務:

public void addProperties(Properties properties);
    public boolean contains(String key);
    public String get(String key);
    public String get(String key, Filter filter);
    public String[] getArray(String key);
    public String[] getArray(String key, Filter filter);
    public Properties getProperties();
    public void removeProperties(Properties properties);
    public void set(String key, String value);

com.liferay.portal.kernel.configuration.ConfigurationImpl實現類,在實現類中,使用了不少外部的組件,好比:easyconf.

十.       Liferay啓動過程分析

輸入http://localhost:8080/後,根據web.xml配置文件中的<welcome-file-list>標籤找到index.jsp

index.jsp中,導入com.liferay.portal.util.PortalUtil1),PortalUtil調用com.liferay.portal.util.Portal接口的實現類com.liferay.portal.util.PortalImpl2),getPathMain()方法取得pathmain變量,而後使用<body onload="javascript: location.replace('<%= mainPath %>')">進行頁面跳轉。

因爲第一次訪問用戶沒有登陸,所以urlpathmain)匹配爲/c/*,所以被MainServlet截獲對應com.liferay.portal.servlet.MainServlet3,而後由它進行組合頁面。

相關文章
相關標籤/搜索