按本方法在部署时会现在问题.修正后的链接为:http://blog.csdn.net/eggtk/article/details/38079183
要求:
开发openfire插件,插件使用spring mvc+velocity显示页面.
1.首先导入依赖包:,我是使用maven下载好依赖包后得到的插件的lib目录下的.
2.建立spring mvc配置文件PluginServlet-servlet.xml: 参考http://kodak-zhou.iteye.com/blog/970682 来的.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- spring mvc 配置部分 -->
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan
base-package="org.jivesoftware.openfire.plugin.web.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="velocityProperties">
<props>
<prop key="file.resource.loader.class">
<!-- 配置velocity模板加载器为类路径加载器 -->
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<!-- 在src目录下新建文件夹template用来存在velocity模板文件 -->
<property name="prefix" value="template/" />
<property name="suffix" value=".mv" />
</bean>
</beans>
因为openfire在插件初始化时会将插件目录的web/WEB-INF/web-custom.xml 里配置的servlet通过pluginServlet.registerServlets()方法注册到pluginServlet中,所有的servlet class都使用Object instance = servletClass.newInstance()进行实例化.所以根据spring mvc配置文件的命名将spring mvc配置文件命名为PluginServlet-servlet.xml,并且放在openfire/plugins/admin/webapp/WEB-INF目录下.
3.在插件的源代码目录下新建文件夹template用来存放模板文件.
controller使用srping提供的注解,完成
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
*
* @author 20005
* @createDate 2014-5-31 上午11:43:32
*/
@Controller
@RequestMapping(value="usionmrg/webs")
public class ConTest {
@RequestMapping(value="aa")
public String aa(HttpServletRequest request,Model model) {
model.addAttribute("a",123);
return "a";
}
}
4.模板文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
$a
<img src="/plugins/usionmrg/image/s.png">
</body>
</html>
最后将插件部署,输入controller的访问路径就可以了