欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2 + kindeditor在线编辑器 整合

程序员文章站 2024-01-28 14:47:10
...

在struts2中用kindeditor在线编辑的话,上传文件会遇到上传失败的问题,因为所有请求都会被struts2拦截,解决这种方法有很多,下面为其中之一。

1、JS代码片段

<script>
	
	KindEditor.ready(function(K) {
			var editor1 = K.create('textarea[name="content1"]', {
				cssPath : '<%=basePath%>kindeditor-4.1.10/plugins/code/prettify.css',
				uploadJson : '<%=basePath%>kindeditor-4.1.10/jsp/upload_json.jsp',
				fileManagerJson : '<%=basePath%>kindeditor-4.1.10/jsp/file_manager_json.jsp',
				allowFileManager : true,
				afterCreate : function() {
					var self = this;
					K.ctrl(document, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
					K.ctrl(self.edit.doc, 13, function() {
						self.sync();
						document.forms['example'].submit();
					});
				}
			});
			prettyPrint();
		});

 

2、HTML代码片段

<tr> 
<th scope="row" abbr="Model" class="spec" style="width: 300px;"><a style="color:  fe8b0f">*</a>商品详情:</th> 
<td>

<textarea name="content1"></textarea>
</td>
</tr> 

 

3、新建一个过滤器类,随便一个包都行。

package util;
import java.io.IOException;  
import javax.servlet.FilterChain;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.http.HttpServletRequest; 
 

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class KindeditorUrlFilter extends StrutsPrepareAndExecuteFilter {
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) 
throws IOException, ServletException {  
       HttpServletRequest request = (HttpServletRequest) req;  
       //不过滤的url  
       String url = request.getServletPath();
     
       if ("/kindeditor-4.1.10/jsp/file_manager_json.jsp".equals(url)) {  
          
          
           chain.doFilter(req, res);  
       }else if("/kindeditor-4.1.10/jsp/upload_json.jsp".equals(url)){
    	   chain.doFilter(req, res);
       }else{  
           //System.out.println("使用默认的过滤器");  
           super.doFilter(req, res, chain);  
       }  
   }  
}

 

4、修改web.xml

<filter>
		<filter-name>struts2</filter-name>
		<!--<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>-->
		<!-- 手动过滤工具,在线编辑器模块,继承struts2 -->
		<filter-class>util.KindeditorUrlFilter</filter-class>
	
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 

 

完成。