js文件中如下:
$(pageInit); function pageInit(){ //这是用来在线编写代码的插件 以后程序题会用得到 内置在full样式里了 即用tools:'full'就可以调用 var plugins={ Code:{c:'btnCode',t:'代码',h:1,e:function(){ var _this=this; var htmlCode='<div><select id="xheCodeType"><option value="html">Html/Xml</option><option value="js">Javascript</option><option value="css">Css</option><option value="php">Php</option><option value="java">Java</option><option value="py">Python</option><option value="pl">Perl</option><option value="rb">Ruby</option><option value="cs">C#</option><option value="c">C++/C</option><option value="vb">Vb/Asp</option><option value="">其它</option></select></div><div><textarea id="xheCodeValue" wrap="soft" spellcheck="false" style="300px;height:100px;" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div>'; var jCode=$(htmlCode),jType=$('#xheCodeType',jCode),jValue=$('#xheCodeValue',jCode),jSave=$('#xheSave',jCode); jSave.click(function(){ _this.loadBookmark(); _this.pasteHTML('<pre class="prettyprint lang-'+jType.val()+'">'+_this.domEncode(jValue.val())+'</pre>'); _this.hidePanel(); return false; }); _this.showDialog(jCode); }} }; //不用哪种就注释掉 /*全部样式*/ //$('.myeditor').xheditor({tools:'full',skin:'o2007silver',plugins:plugins,loadCSS:'<style>pre{margin-left:1em;border-left:5px solid #68B839;padding:0 1em;}</style>',upImgUrl:'xheditor',upImgExt:'jpg,jpeg,gif,png,bmp',forcePtag:false}); //$('.myeditor').xheditor({skin:'o2007silver',html5Upload:false,upMultiple:'1',upImgUrl:'xheditor',upImgExt:'jpg,jpeg,gif,bmp,png',upLinkUrl:'Upload.do',upLinkExt:'zip,rar,txt,docx,doc,xls,xlsx,pdf'}); /*简单样式*/ //$('.myeditor').xheditor({tools:'mini',skin:'o2007silver'}); $('.myeditor').xheditor({tools:'full',skin:'o2007silver',plugins:plugins,loadCSS:'<style>pre{margin-left:1em;border-left:5px solid #68B839;padding:0 1em;}</style>', upMultiple:false, upImgUrl: "/onlinetest/JRPC", upImgExt: "jpg,jpeg,gif,bmp,png", onUpload:insertUpload }); function insertUpload(msg) { var _msg = msg.toString(); var _picture_name = _msg.substring(_msg.lastIndexOf("/")+1); var _picture_path = Substring(_msg); var _str = "<input type='checkbox' name='_pictures' value='"+_picture_path+"' checked='checked' onclick='return false'/><label>"+_picture_name+"</label><br/>"; $("#myeditor").append(_msg); //$("#uploadList").append(_str); } //处理服务器返回到回调函数的字符串内容,格式是JSON的数据格式. function Substring(s){ return s.substring(s.substring(0,s.lastIndexOf("/")).lastIndexOf("/"),s.length); } /*自定义样式*/ //修改tools参数即可 完整按钮表: //|:分隔符 /:强制换行 Cut:剪切 Copy:复制 Paste:粘贴 Pastetext:文本粘贴 Blocktag:段落标签 Fontface:字体 FontSize:字体大小 Bold:粗体 Italic:斜体 //Underline:下划线 Strikethrough:中划线 FontColor:字体颜色 BackColor:字体背景色 SelectAll:全选 Removeformat:删除文字格式 Align:对齐 List:列表 Outdent:减少缩进 //Indent:增加缩进 Link:超链接 Unlink:删除链接 Anchor:锚点 Img:图片 Flash:Flash动画 Hr:插入水平线 //Table:表格 Source:切换源代码模式 Preview:预览当前代码 Print:打印 Fullscreen:切换全屏模式 //$('.myeditor').xheditor({tools:'Cut,Copy,Paste,Pastetext,Underline,Removeformat,Source,Link,Unlink,FontSize,Preview,Fullscreen',skin:'o2007silver'}); }
这里使用的servlet,那么就需要在strust2中加入servlet.就需要添加filter,否则struts2
Filter类如下:
import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class UploadFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; String target = request.getRequestURI(); target = target.lastIndexOf("?") > 0 ? target.substring(target .lastIndexOf("/") + 1, target.lastIndexOf("?") - target.lastIndexOf("/")) : target.substring(target .lastIndexOf("/") + 1); // System.out.println(target); if (this.includes.contains(target)) { RequestDispatcher rdsp = request.getRequestDispatcher(target); // System.out.println("go..............." + rdsp); rdsp.forward(req, resp); } else chain.doFilter(req, resp); } private ArrayList<String> includes = new ArrayList<String>(); public void init(FilterConfig config) throws ServletException { this.includes.addAll(Arrays.asList(config.getInitParameter( "includeServlets").split(","))); } }
然后web.xml文件中在sturts2的filter前面增加:
<filter> <filter-name>JSONRPCServlet</filter-name> <filter-class>hhxy.jsjcxsy.onlinetest.web.struts2.filter.UploadFilter</filter-class> <init-param> <param-name>baseDir</param-name> <param-value>/ARTICLE_IMG</param-value> </init-param> <init-param> <param-name>fileExt</param-name> <param-value>jpg,jpeg,gif,bmp,png</param-value> </init-param> <init-param> <param-name>maxSize</param-name> <param-value>1024000</param-value> </init-param> <init-param> <param-name>dirType</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>includeServlets</param-name> <param-value>JRPC</param-value> <!--注:JRPC为下文配置的Servlet JSONRPCServlet过滤的请求配置 --> </init-param> </filter> <filter-mapping> <filter-name>JSONRPCServlet</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>JSONRPCServlet</servlet-name> <servlet-class>hhxy.jsjcxsy.onlinetest.servlet.UploadFileServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JSONRPCServlet</servlet-name> <url-pattern>/JRPC</url-pattern> </servlet-mapping>