在springmvc中使用Ueditor

Ueditor下载地址:

http://ueditor.baidu.com/website/download.html

下载后直接解压缩。我主要实现文件上传和form表单提交数据。

一、配置文件修改

uedit.config.js

var URL = window.UEDITOR_HOME_URL; //主要是本地ueditor文件目录

serverUrl: URL + "jsp/config.jhtml"  // 服务器统一请求接口路径

conf.json

"imageUrlPrefix": "http://localhost:8080/fetchbaike/ueditor/jsp/upload/", /* 图片访问路径前缀 */

二 、页面修改

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <script type="text/javascript" charset="utf-8" src="./ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="./ueditor/ueditor.all.min.js"> </script>
    <!--建议手动加在语言,避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型,比如你在配置项目里配置的是英文,这里加载的中文,那最后就是中文-->
 <script type="text/javascript" charset="utf-8" src="./ueditor/lang/zh-cn/zh-cn.js"></script>
tyle type="text/css">
        div{
            width:100%;
        }
    </style>
</head>
<body>

<form id="form"  method="post" action="./textarea.jhtml">
        <input type="text" name="goodsname" value="goodsname"  />
        <script type="text/plain" id="myEditor" name="myEditor">
          <p>欢迎使用UEditor!</p>
     </script>
        <input type="submit" value="提交"/>
</form>

<!-- editor的初始化 -->
<script type="text/javascript">

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor(‘editor‘)就能拿到相关的实例
   
    var editor_a = UE.getEditor(‘myEditor‘,{ 
    initialFrameWidth : 400,
       initialFrameHeight: 300});
    
   
   <!--用于上传文件跳转到controller-->
    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    
    UE.Editor.prototype.getActionUrl = function(action) {
  
        if (action == ‘uploadimage‘ || action == ‘uploadscrawl‘ || action == ‘uploadimage‘
                || action == ‘uploadvideo‘ || action == ‘uploadfile‘) {
            return ‘./ueditor/uploadimage.jhtml‘;
        }else {
            return this._bkGetActionUrl.call(this, action);
        }
    }
    
</script>
</body>

三、后台操作

@Controller
@RequestMapping(value="/ueditor")
public class UeditorController {

@RequestMapping(value="/jsp/config")
public void config(HttpServletRequest request,HttpServletResponse response,String action) throws Exception {
   
    request.setCharacterEncoding("utf-8");
    response.setHeader("Content-Type", "text/html");
    String rootPath=request.getSession().getServletContext().getRealPath("/");
    //会加载conf.json文件,注意路径问题
    response.getWriter().write(new ActionEnter(request,rootPath).exec());
}

@RequestMapping(value="/uploadimage")
@ResponseBody
public Map<String,Object> uploadimage(@RequestParam("upfile") MultipartFile[] multipartFiles,  HttpServletRequest request,HttpServletResponse response) throws Exception {
    Map<String,Object> map=new HashMap<String,Object>();
    String path=request.getSession().getServletContext().getRealPath("/ueditor/jsp/upload/");
    System.out.println("path++"+path);
    if(multipartFiles!=null && multipartFiles.length>0){
        //循环遍历
        for (MultipartFile multipartFile : multipartFiles) {
        
            //原来图片的名称
            String OriginalFilename=multipartFile.getOriginalFilename();
            
            //获得图片新名称
            String newFileName=getFileNewName(OriginalFilename.substring(OriginalFilename.lastIndexOf(".")));
           
            //创建文件
            File targetFile=new File(path,newFileName);
            if(!targetFile.exists()){
                targetFile.mkdirs();
            }
            String state="SUCCESS";
            try {
                multipartFile.transferTo(targetFile);
                map.put("original", OriginalFilename);
                map.put("name",newFileName);
                //注意url会和conf.json中的路径配合找到图片
                map.put("url", newFileName);
                map.put("state", "SUCCESS");
            } catch (Exception e) {
            state="FAIL";
            }
        }
    }
    return map;
}
时间: 2024-12-19 09:12:43

在springmvc中使用Ueditor的相关文章

SpringMVC中文件上传的客户端验证

SpringMVC中文件上传的客户端验证 客户端验证主要思想:在jsp页面中利用javascript进行对文件的判断,完成验证后允许上传 验证步骤:1.文件名称 2.获取文件的后缀名称 3.判断哪些文件类型允许上传 4.判断文件大小 5.满足条件后跳转后台实现上传 前台界面(验证上传文件是否格式满足要求): <body> <h2>文件上传</h2> <form action="upload01" method="post" 

springmvc 中 Instantiation of bean failed实例化Bean失败错误

配置springMVC 项目时,启动tomcat服务器报错: 错误: 信息: Initializing Spring FrameworkServlet 'springmvc' 2015-8-19 9:57:35 org.apache.catalina.core.ApplicationContext log 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error cre

.NET中应用Ueditor(富文本编辑)的配置和使用

一.Ueditor的下载 1.百度编辑器下载地址:http://ueditor.baidu.com/website/download.html 2.下载完整源码包,解压到任意目录,解压后的源码目录结构如下所示: 3.编辑器展示: 二.Ueditor的使用 1.添加到项目中如图所示: 2.新建项目中进行引用 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestUeditor.a

关于Springmvc中include与Sitemesh装饰器的基本使用

关于Springmvc中include与Sitemesh装饰器的使用 !!!转载请注明出处=>http://www.cnblogs.com/funnyzpc/p/7283443.html 静态包含:example:<%@include file="xxx.jsp"%> 文件的包含是发生在 jsp向servlet转换时期 ,相当于将jsp编译成html静态文件,由于对包含的文件不再编译(直接拷贝到父页面),则只产生一个class文件. 动态包含:example<j

详解SpringMVC中Controller的方法中参数的工作原理

前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html SpringMVC中Controller的方法参数可以是Integer,Double,自定义对象,ServletRequest,ServletResponse,ModelAndView等等,非常灵活.本文将分析SpringMVC是如何对这些参数进行处理的,

在SpringMVC中 /* 和 / 的区别

<url-pattern> / </url-pattern>:会匹配到 /springmvc 这样的路径型url,而不会匹配到像 .jsp 这样的后缀型的url. <url-pattern> /* </url-pattern>:会匹配到所有的url:路径型url 和后缀型的url (包括/springmvc,.jsp,.js,和.html等). 在SpringMVC中需要添加这个标签:<mvc:default-servlet-handler />

springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序

springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/0309/19/834950_359080244.shtml

springMVC中Dispatcher中的/和/*的区别

1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配.2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个DispatcherServlet有/和/*,先匹配的是/*这个 3. 当配置相同的情况下,DispathcherServlet配置成/和/*的区别< 一 > / :使用/配置路径,直接访问到jsp,不经springDispatcherServlet< 二 > /*:配置/*路径,不能访问到多视图的

详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] good

目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口介绍 HandlerMethodArgumentResolver与HandlerMethodReturnValueHandler接口的具体应用 常用HandlerMethodArgumentResolver介绍 常用HandlerMethodReturnValueHandler介绍 本文开头现象解释以及解决方案 编写自定义的HandlerMet