liferay5.1.2note

遨豪(大连)科技有限公司-aukcell

网址:www.auckcell.com

 

 

一.       HttpSessionListener

<listener>

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

</listener>

liferay中,使用PortletSessionListenerManager实现HttpSessionListener

二.       ServletContextListener接口的实现与应用

ServletContextListener接口有两方需要实现的方法:contextInitialized()contextDestroyed();

它会监听Servlet容器,当应用开始的时候它会调用contextInitialized()方法;当应用关闭的时候,它同样会调用contextDestroyed()方法.

ServletContext对象是一个为整个web应用提供共享的内存,任何请求都可以访问里面的内容。

如何实现在服务启动的时候就动态的加入到里面的内容:我们需要做的有:

1 实现servletContextListerner接口 并将要共享的通过setAttributename,data)方法提交到内存中去。

2)应用项目在通过getAttribute(name)将数据或到。

public class ServletContextLTest implements ServletContextListener{  }

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接口。

 

三.       web.xml中的参数context-paraminit-param区别

web.xml中的参数context-paraminit-param区别?

(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:

<context-param>  

            <param-name>context/param</param-name>  

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

   </context-param>

(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")取得。

四.       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>

 

 

 

五.       如何实现一个过滤器

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>

六.       java.util.Properties类的使用

l        创建一个属性文件sample.properties

 foo=bar
fu=baz

l        编写一个类装载properties文件

import java.util.*;  

import java.io.*;  

 

public class testProperties {  

    public static void main(String args[]) throws Exception {  

      Properties prop = new Properties();  

      FileInputStream fis =   

        new FileInputStream("sample.properties");  

      prop.load(fis);  

      prop.list(System.out);  

      System.out.println("/nThe foo property: " +  

          prop.getProperty("foo"));  

    }  

}

七.       EventListener的使用

 

八.       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,然后由它进行组合页面。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值