`
eggtk
  • 浏览: 9590 次
文章分类
社区版块
存档分类
最新评论

openfire 3.9.1 下使用spring mvc 4.0.3+velocity 1.7插件页面开发 --修正

阅读更多

修正思路:不让openfire的pluginServlet自动加载类springmvc的DispatcherServlet改成在插件初始化的时候手动将DispatcherServlet注册到PluginServlet中.

1.web-custom.xml不定义DispatcherServlet.

2.自定义一个CustomDispatcherServlet,继承DispatcherServlet,在其构造方法中指定springMvc的配置文件路径,并且修改 配置文件中velocityConfigurer节点下的resourceLoaderPath值.

import java.io.File;

import org.springframework.web.servlet.DispatcherServlet;

import com.aomaisoft.openfire.plugin.usionmrg.web.tools.XMLEditor;

/**
 * 自定义DispatcherServlet,指定context位置
 * 
 * @author 20005
 * @createDate 2014-7-14 上午11:10:59
 */
public class CustomDispatcherServlet extends DispatcherServlet {

	private static final long serialVersionUID = 5713165491251313468L;

	public CustomDispatcherServlet() {
		super();
		String pluginDir = null;
		Character fileSep = File.separatorChar;
		String contextConfigLocation = pluginDir + fileSep + "java" + fileSep
				+ "PluginServlet-servlet.xml";
		// 更改模板目录
		XMLEditor.alterXML(pluginDir, contextConfigLocation);

		contextConfigLocation = "file:/" + contextConfigLocation;
		super.setContextConfigLocation(contextConfigLocation);
	}
}

附上XMLEditor代码 ,依赖dom4j.jar,可以到 http://www.java2s.com/去下载

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * xml节点修改
 * 
 * @author 20005
 * @createDate 2014-7-15 下午04:35:47
 */
public class XMLEditor {

	public static Document read(String fileName) {
		SAXReader reader = new SAXReader();
		Map<String, String> nsMap = new HashMap<String, String>();
		nsMap.put("beans", "http://www.springframework.org/schema/beans");
		reader.getDocumentFactory().setXPathNamespaceURIs(nsMap);
		reader.setEncoding("UTF-8");
		Document document = null;
		try {
			document = reader.read(new File(fileName));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}

	public static void alterXML(String newValue, String url) {

		OutputFormat format = new OutputFormat("\t", true);
		format.setEncoding("UTF-8");
		Document document = read(url);
		Element node = ((Element) document
				.selectSingleNode("//beans:property[@name='resourceLoaderPath']"))
				.element("value");
<span style="white-space:pre">		</span>//此处路径要做相应的修改,为你的模板路径
		String string = "file:/%s\\web\\WEB-INF\\template";
		string = String.format(string, newValue);
		node.setText(string);
		try {
			// 重新写入到XML文档
			XMLWriter writer = new XMLWriter(new FileOutputStream(url), format);
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3.在插件初始化时手动注册到PluginServlet中

@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
	// 得到spring mvc配置文件路径,修改成你相应的路径,我是放在web-inf下的
	File webXML = new File(pluginDirectory.toString() + File.separatorChar
	+ "web" + File.separatorChar + "WEB-INF" + File.separatorChar
	+ "springMVC.xml");
	// 注册servlet
	PluginServlet.registerServlets(manager, this, webXML);
}

4.完成,测试.

spring配置文件部分代码

<!-- 视图解析 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
		<!-- 在src目录下新建文件夹template用来存在velocity模板文件 -->
		<property name="prefix" value="/" />
		<property name="suffix" value=".html" />
		<property name="contentType" value="text/html;charset=utf-8"></property>
	</bean>
	<bean id="velocityConfigurer"
		class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
		<property name="resourceLoaderPath">
			<value><!-- 先不填,在程序中指定 -->
			</value>
		</property>
		<property name="velocityProperties">
			<props>
				<prop key="input.encoding">
					UTF-8
				</prop>
				<prop key="output.encoding">
					UTF-8
				</prop>
			</props>
		</property>
	</bean>



分享到:
评论
1 楼 langxuanlovehai 2017-11-29  
大神,按照你说的eclipse上可以运行,但是打包部署后,就提示错误2017.11.29 13:14:27 ERROR [pool-4-thread-1]: org.jivesoftware.com.jpush.openfire.plugin.CustomDispatcherServlet - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]
Offending resource: URL [file:C:/Users/gnet/Gitee/openfire_src/target/openfire/bin/../plugins/springcxf/web/PluginServlet-servlet.xml]

at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
不知道为什么,你发生过吗?

相关推荐

Global site tag (gtag.js) - Google Analytics