1、 HttpSessionListenerjavascript
<listener> <listener-class>com.liferay.portal.kernel.servlet.PortletSessionListenerManager</listener-class> </listener>
在liferay中,使用PortletSessionListenerManager实现HttpSessionListener。java
2、 ServletContextListener接口的实现与应用web
ServletContextListener接口有两方须要实现的方法:contextInitialized()和contextDestroyed();spring
它会监听Servlet容器,当应用开始的时候它会调用contextInitialized()方法;当应用关闭的时候,它一样会调用contextDestroyed()方法.app
ServletContext对象是一个为整个web应用提供共享的内存,任何请求均可以访问里面的内容。jsp
如何实如今服务启动的时候就动态的加入到里面的内容:咱们须要作的有:ide
1) 实现servletContextListerner接口 并将要共享的经过setAttribute(name,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-param和init-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范围内的参数,只能在servlet的init()方法中取得,在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")获得。
第二种参数只能在servlet的init()方法中经过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&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.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),而后由它进行组合页面。