使用struts2完成ckeditor和图片上传

代码地址如下:
http://www.demodashi.com/demo/12427.html

使用struts2完成ckeditor和ckeditor图片上传

ckeditor版本ckeditor_4.8.0_full
struts2版本struts2.5

  • 解压压缩包,将解压后的文件夹ckeditor直接拷贝至WebContent下
  • 在jsp中引用ckeditor.js
<script type="text/javascript" src="${pageContext.request.contextPath }/js/ckeditor/ckeditor.js"></script>
  • 在文本域textarea下方编写如下js代码
<script type="text/javascript">CKEDITOR.replace("editor");</script>

注意:replace中写的是textarea的name名称

  • 完整代码:

  • 在demo.action中设置属性,名称为editor,提供getter和setter方法
  • 在struts.xml中配置好action
<action name="demo" class="demo.DemoAction" method="demo">
    <result>/result.jsp</result>
</action>
  • 启动服务器查看效果:

在jsp页面中可以不必写提交按钮,上方有一个保存,效果相同,需要使用editor full版本,该版本相较于标准版本功能更多。
点击保存即可在result.jsp界面中查看,后台打印是html样式。

解决图片上传

  • 上传按钮显示解决(默认是没有图片上传的)

在editor文件夹目录下的config.js中添加如下代码:

config.filebrowserUploadUrl="ckeditorUpload.action";
//url地址为一会上传至服务器执行的action
  • 图片预览中英文解决

在editor文件夹目录下的config.js中添加如下代码:

config.image_previewText=' ';
  • 效果

  • 实现上传功能

上面的只是一个上传页面。也就相当于一个HTML的form表单,要配置点击“上传到服务器上”按钮后请求的Action。可以在ckeditor/config.js中配置。该表单的上传标签的name为upload。
在config.js中添加如下代码:

config.filebrowserUploadUrl="ckeditorUpload.action";
//url地址为一会上传至服务器执行的action

该行代码其实在上传按钮显示时就已经配置过。

  • struts.xml中代码:
<action name="ckeditorUpload" class="demo.DemoAction" method="upload">
</action>
  • 文件上传代码,思路与struts文件上传相同,只需要加上几句即可,代码如下:
    private File upload;    //editor默认的上传表单的标签名为upload
    private String uploadContentType;
    private String uploadFileName;

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String upload() throws Exception{

        HttpServletResponse response = ServletActionContext.getResponse();    
        response.setCharacterEncoding("GBK");    
        PrintWriter out = response.getWriter();  

        String realPath=ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(realPath);

        // CKEditor提交的很重要的一个参数    
        String callback = ServletActionContext.getRequest().getParameter("CKEditorFuncNum"); 
        //request.put("callback", callback);

        FileOutputStream fout=
                new FileOutputStream(new File(file,getUploadFileName()));

        FileInputStream in=new FileInputStream(getUpload());

        byte[] buffer=new byte[1024];
        int len=0;
        while((len=in.read(buffer))>0)
            fout.write(buffer,0,len);
        //返回“图像”选项卡,并显示预览图片
        //一定要在流关闭之前写下面三句话,否则图片显示不出来,也不报错
        out.println("<script type=\"text/javascript\">");    
        out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + ServletActionContext.getRequest().getContextPath() + "/images/" + uploadFileName + "','')");    
        out.println("</script>");

        out.close();
        in.close();

        return SUCCESS;
    }
  • 效果截图:

  • 点击上传到服务器,自动跳转至图像信息选项卡,并显示预览图片

  • 点击确定:

  • 点击保存,即可在result.jsp页面中查看:

  • 控制台打印文本域中的信息:

项目结构


使用struts2完成ckeditor和图片上传

代码地址如下:
http://www.demodashi.com/demo/12427.html

注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要安装 `@ckeditor/ckeditor5-vue2` 和 `@ckeditor/ckeditor5-build-classic` 两个依赖包。 ``` npm install --save @ckeditor/ckeditor5-vue2 @ckeditor/ckeditor5-build-classic ``` 然后在你的 Vue 组件中引入 CKEditor 组件: ```vue <template> <div> <ckeditor :editor="editor" v-model="content" :config="editorConfig"></ckeditor> </div> </template> <script> import ClassicEditor from '@ckeditor/ckeditor5-build-classic' import CKEditor from '@ckeditor/ckeditor5-vue2' export default { components: { ckeditor: CKEditor.component }, data() { return { content: '', editorConfig: { // 配置项 }, editor: ClassicEditor } } } </script> ``` 在上面的代码中,我们引入了 `ClassicEditor`,它是一个预先配置好的编辑器,包含了常用的插件,如加粗、斜体、链接等。我们也可以自定义 `editorConfig`,来配置编辑器。 下面是一个常用的配置项示例: ```js editorConfig: { toolbar: { items: [ 'bold', 'italic', 'link', '|', 'bulletedList', 'numberedList', '|', 'imageUpload', 'blockQuote', 'insertTable', 'undo', 'redo' ] }, image: { toolbar: [ 'imageTextAlternative', '|', 'imageStyle:full', 'imageStyle:side', '|', 'imageResize', '|', 'imageUpload', 'imageUpload', 'imageUpload', 'imageUpload' ], styles: [ 'full', 'side' ] }, language: 'zh-cn', table: { contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells', 'tableCellProperties', 'tableProperties' ] }, licenseKey: '', simpleUpload: { uploadUrl: '/your/upload/url', headers: { 'X-CSRF-TOKEN': 'CSRF-Token' } } } ``` 上面的配置中,我们开启了图片上传功能,并且添加了表格插入、图片上传、撤销、重做等常用功能。同时也设置了语言为中文。 如果需要添加额外的插件,可以使用 `@ckeditor/ckeditor5-*` 的包名,比如添加字数统计插件: ``` npm install --save @ckeditor/ckeditor5-word-count ``` 然后在 `editorConfig` 中添加: ```js import WordCount from '@ckeditor/ckeditor5-word-count/src/wordcount'; editorConfig: { plugins: [WordCount], toolbar: [ 'wordCount' ] } ``` 这样就可以在编辑器中添加字数统计功能了。 我们也可以自定义上传图片的方法,需要在配置项中添加 `simpleUpload` 选项: ```js editorConfig: { simpleUpload: { uploadUrl: '/your/upload/url', headers: { 'X-CSRF-TOKEN': 'CSRF-Token' }, // 自定义上传方法 async upload(file) { // 这里写上传逻辑 const formData = new FormData(); formData.append('file', file); const response = await axios.post('/your/upload/url', formData, { headers: { 'Content-Type': 'multipart/form-data', 'X-CSRF-TOKEN': 'CSRF-Token' } }); return { default: response.data.url }; } } } ``` 在上面的代码中,我们使用了 axios 发送了一个 `multipart/form-data` 的请求,然后返回上传的图片地址。 希望以上内容能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值