Struts通过加载插件的形式初始化系统常量以及其他信息
一:通过插件的形式加载系统中定义的常量:
思路:首先定义一个常量类存放系统常量,或者定义一个xml或properties文件中然后进行读取,
然后把相应的常量放入map中
最后编写插件类,加载map类
最后把插件类放入struts-config.xml中启动的时候进行加载
这样在其他页面中就可以随意访问了。
1:首先定义在常量类中定义一些常量:
package com.topwqp.common.constants;
public class Globals {
public static final String AUTHOR = "topwqp";
public static final String PLUGINTEST = "puginTest";
}
2:把常量放在map中,
package com.topwqp.common.constants;
import java.util.HashMap;
import java.util.Map;
public class Constants {
private Map globals;
public Constants(){
globals = new HashMap();
globals.put("AUTHOR", Globals.AUTHOR);
globals.put("PLUGINTEST", Globals.PLUGINTEST);
}
public Map getGlobals() {
return globals;
}
public void setGlobals(Map globals) {
this.globals = globals;
}
}
3:编写对应的插件类:
插件类需要实现PlugIn接口关于这个接口的介绍在下面
package com.topwqp.plugin;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.topwqp.common.constants.Constants;
public class ConstantsPlugin implements PlugIn{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
System.out.println("load constans success. then you can access constans.......");
// TODO Auto-generated method stub
Constants constants = new Constants();
servlet.getServletContext().setAttribute("Constants", constants);
}
}
关于PlugIn接口的源码如下:意思就是需要实现init() 其中init方法用于系统启动的一些设置在这个方法中进行编写,
deploy方法是系统关闭时候的一些操作在这个方法中进行设置。
源码如下:
/*
* $Id: PlugIn.java 471754 2006-11-06 14:55:09Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts.action;
import org.apache.struts.config.ModuleConfig;
import javax.servlet.ServletException;
/**
* <p>A <strong>PlugIn</strong> is a configuration wrapper for a
* module-specific resource or service that needs to be notified about
* application startup and application shutdown events (corresponding to when
* the container calls <code>init</code> and <code>destroy</code> on the
* corresponding {@link ActionServlet} instance). <code>PlugIn</code> objects
* can be configured in the <code>struts-config.xml</code> file, without the
* need to subclass {@link ActionServlet} simply to perform application
* lifecycle activities.</p>
*
* <p>Implementations of this interface must supply a zero-argument
* constructor for use by {@link ActionServlet}. Configuration can be
* accomplished by providing standard JavaBeans property setter methods, which
* will all have been called before the <code>init()</code> method is
* invoked.</p>
*
* <p>This interface can be applied to any class, including an Action
* subclass. </p>
*
* @version $Rev: 471754 $ $Date: 2005-05-14 01:09:32 -0400 (Sat, 14 May 2005)
* $
* @since Struts 1.1
*/
public interface PlugIn {
/**
* <p>Receive notification that our owning module is being shut down.</p>
*/
void destroy();
/**
* <p>Receive notification that the specified module is being started
* up.</p>
*
* @param servlet ActionServlet that is managing all the modules in this
* web application
* @param config ModuleConfig for the module with which this plug-in is
* associated
* @throws ServletException if this <code>PlugIn</code> cannot be
* successfully initialized
*/
void init(ActionServlet servlet, ModuleConfig config)
throws ServletException;
}
然后把编写的插件类在struts-config.xml中进行配置即可,当application在tomcat中启动 时候,首先加载web.xml的文件,web.xml中有相应的初始化设置加载指定的文件:
及struts-config.xml文件:
当然struts允许同时加载多个文件如下我的web.xml的配置如下:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml, /WEB-INF/struts-config-1.xml, /WEB-INF/struts-config-2.xml
</param-value>
</init-param>
<init-param>
<param-name>config/admin</param-name>
<param-value>
/WEB-INF/struts-config-admin.xml
</param-value>
</init-param>
<init-param>
<param-name>config/common</param-name>
<param-value>
/WEB-INF/struts-config-common.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
把编写的插件配置在struts-config.xml中即可:
<plug-in className="com.topwqp.plugin.ConstantsPlugin"></plug-in>
在对应的jsp页面中访问:
<p>
Field name: AUTHOR<br />
Field value: <bean:write name="Constants"
property="globals(AUTHOR)"/><br />
</p>
<p>
Field name: PluginTest<br />
Field value: <c:out value="${Constants.globals.PLUGINTEST}"/><br />
</p>
2:创建一个插件记录应用的启动时间以及已经运行了多久
思路:定义时间追踪类,该类中有应用的启动时间,以及方法获取距离启动时间多久,然后在插件中放入这个类的实例,最后在jsp页面中进行显示即可。
1:定义时间类:
package com.topwqp.plugin;
import java.util.Date;
public class TimeTracker {
private long startUpTimeMillis;
private Date startupOn;
public TimeTracker (){
startUpTimeMillis = System.currentTimeMillis();
startupOn = new Date();
}
// how long the application have started up;
public long getUptime(){
return System.currentTimeMillis() - startUpTimeMillis;
}
// get the application startup time
public Date getStartupOn(){
return startupOn;
}
}
这个类中有个默认的构造方法,当对象创建的时候对应的启动时间就产生了,有一个getUptime方法,用去动态生成距离启动时间的毫秒数。
2:创建对应的插件类:
package com.topwqp.plugin;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
public class TimeTrackerPlugin implements PlugIn{
private String contextKey ;
public void setContextKey(String contextKey){
this.contextKey = contextKey;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("do nothing............");
}
@Override
public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
// TODO Auto-generated method stub
servlet.getServletContext().setAttribute(contextKey, new TimeTracker());
}
}
3:在struts-config.xml中配置插件:
<plug-in className="com.topwqp.plugin.TimeTrackerPlugin">
<set-property property="contextKey" value="timeTracker"/>
</plug-in>
4:在jsp页面中获取对应的时间:
<h4> 系统启动日期: since <bean:write name="timeTracker" property="startupOn" format="MM/dd/yyyy HH:mm"/> <br> 已经运行时间:for <bean:write name="timeTracker" property="uptime"/> milliseconds! </h4>
然后启动应用访问即可:
两个插件的访问页面显示效果:

浙公网安备 33010602011771号