一、 HttpSessionListener
com.liferay.portal.kernel.servlet.PortletSessionListenerManager
在liferay中,使用PortletSessionListenerManager实现HttpSessionListener。
二、 ServletContextListener接口的实现与应用
ServletContextListener接口有两方需要实现的方法:contextInitialized()和contextDestroyed();
它会监听Servlet容器,当应用开始的时候它会调用contextInitialized()方法;当应用关闭的时候,它同样会调用contextDestroyed()方法.
ServletContext对象是一个为整个web应用提供共享的内存,任何请求都可以访问里面的内容。
如何实现在服务启动的时候就动态的加入到里面的内容:我们需要做的有:
1) 实现servletContextListerner接口 并将要共享的通过setAttribute(name,data)方法提交到内存中去。
2)应用项目在通过getAttribute(name)将数据或到。
public class ServletContextLTest implements ServletContextListener{ }
在web.xml配置文件中加入
ServletContextTest.ServletContextLTest
public class CreateEmployee extends HttpServlet{ protected void service(){ ServletContext sct=getServletConfig().getServletContext(); Mapdept=(Map )sct.getAttribute("dept"); }}
这样监视器就设置好了, 以下通用应用调用上面的数据。
在liferay中,使用PortalContextLoaderListener继承ContextLoaderListener(是一个spring中的类,因此使用initWebApplicationContext加载),ContextLoaderListener实现了ServletContextListener接口。
三、 web.xml中的参数context-param和init-param区别
3.1) application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
context/param avalible during application
3.2) servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
Java代码
MainServlet com.wes.controller.MainServlet param1 avalible in servlet init() 0
第一种参数在servlet里面可以通过getServletContext().getInitParameter("context/param")得到。
第二种参数只能在servlet的init()方法中通过this.getInitParameter("param1")取得。
四、UrlRewriteFilter的使用
1. 在web.xml中配置filter,使用UrlRewriteFilter对一些特定的url进行过滤转发
URL Rewrite Filter org.tuckey.web.filters.urlrewrite.UrlRewriteFilter logLevel ERROR statusEnabled false URL Rewrite Filter /*
2) 在WEB-INF下面配置urlrewrite.xml,这个文件
(.*)/blog/blogs/rss(.*) $1/blog/-/blogs/rss$2 ^/c/journal/view_article_content/?groupId=14&articleId=155291$ /web/guest/home/-/journal/rss/14/news ^/web/guest/community/forums/message_boards(.*)$ /web/guest/community/forums/-/message_boards$1 ^/web/guest/home/journal/rss/14/news$ /web/guest/home/-/journal/rss/14/news
五、如何实现一个过滤器
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文件配置
first cn.mldn.lxh.filter.FirstFilter 参数名称 参数值 first /*
六、Easyconf组件的使用
可以参考这个网站
主要是一个用来读取配置文件的工具。
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启动过程分析
输入后,根据web.xml配置文件中的<welcome-file-list>标签找到index.jsp。
在index.jsp中,导入com.liferay.portal.util.PortalUtil,(1),PortalUtil调用com.liferay.portal.util.Portal接口的实现类com.liferay.portal.util.PortalImpl(2),的getPathMain()方法取得pathmain变量,然后使用<body onload="javascript: location.replace('<%= mainPath %>')">进行页面跳转。
由于第一次访问用户没有登录,因此url(pathmain)匹配为/c/*,因此被MainServlet截获对应com.liferay.portal.servlet.MainServlet(3),然后由它进行组合页面。