SSM项目中,使用freemaker加UEditor富文本编辑器

在一个SSM项目中使用到了UEditor富文本编辑器,但是该编辑器使用的是JSP,这个项目不使用JSP,而是使用了freemaker,查找了一些资料对UEditor进行更改。

首先是替换掉UEditor使用的controller.jsp,把该文件重命名为oldController.jsp,然后把ueditor.config.js文件中的

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

改为:

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

这里更改的原因是要使用spring mvc来拦截该请求,但是.jsp后缀的请求不会被拦截,所以更改了请求接口路径。

然后新增一个controller来处理该请求:

先看controller.jsp的内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="com.baidu.ueditor.ActionEnter"
    pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%

    request.setCharacterEncoding( "utf-8" );
    response.setHeader("Content-Type" , "text/html");

    String rootPath = application.getRealPath( "/" );

    out.write( new ActionEnter( request, rootPath ).exec() );

%>

处理该请求的controller:

@Controller
@RequestMapping("/static/ueditor")
public class UEditorController {


    @RequestMapping("/jsp/controller")
    public void writePage(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        request.setCharacterEncoding( "utf-8" );
        response.setHeader("Content-Type" , "text/html");

        String rootPath = request.getSession().getServletContext().getRealPath("/");
        response.getWriter().write(new ActionEnter(request, rootPath).exec());
    }
}

在UEditor中的ConfigManager类会使用rootPath跟请求的URL去获取配置文件config.json,如果UEditor提示未完成初始化的问题,可以Debug进去看看生成的路径是否正确,而且保存图片的路径也是由rootPath加上config.json中的imagePathFormat决定的。

到这里就已经解决了项目中没有使用JSP的问题。

但是如果增加了SpringMVC上传配置:

    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1000000000" />
    </bean>

那么会与UEditor自带的上传组件起冲突,我理解的原因是CommonsMultipartResolver类把HttpServletRequest 转化成MultipartHttpServletRequest了,参考了https://www.cnblogs.com/vincent4code/p/5809858.html,但是在我的程序中,该方法会导致在config.josn配置的上传路径丢失掉第一个文件夹,如”imagePathFormat”: “/ueditor/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}”,存储到本地变成目录/upload/image/{yyyy}{mm}{dd}/{time}{rand:6},依旧报未找到上传数据 ,debug了方法作出了修改,最终的方法为:

public static final State save(HttpServletRequest request, Map<String, Object> conf) {
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
            MultipartFile multipartFile = multipartRequest.getFile(conf.get("fieldName").toString());
            String savePath = (String)conf.get("savePath");
            String originFileName = multipartFile.getOriginalFilename();
            String suffix = FileType.getSuffixByFilename(originFileName);
            originFileName = originFileName.substring(0, originFileName.length() - suffix.length());
            savePath = savePath + suffix;
            long maxSize = ((Long)conf.get("maxSize")).longValue();
            if (!validType(suffix, (String[])((String[])conf.get("allowFiles")))) {
                return new BaseState(false, 8);
            } else {
                savePath = PathFormat.parse(savePath, originFileName);
                String[] savePathBySplit_temp = savePath.split("/");
                String temp = "";
                String fileName = savePathBySplit_temp[savePathBySplit_temp.length - 1];

                for(int i = 1; i < savePathBySplit_temp.length - 1; ++i) {
                    if (i != savePathBySplit_temp.length - 2) {
                        temp = temp + savePathBySplit_temp[i] + "/";
                    } else {
                        temp = temp + savePathBySplit_temp[i];
                    }
                }

                String pathTemp = request.getSession().getServletContext().getRealPath(temp);
                System.out.println(pathTemp + "," + fileName);
                System.out.println((new File(pathTemp)).exists());
                File targetFile = new File(pathTemp);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }

                System.out.println((new File(pathTemp)).exists());
                State storageState = StorageManager.saveFileByInputStream(multipartFile.getInputStream(), pathTemp + "/" + fileName, maxSize);
                if (storageState.isSuccess()) {
                    storageState.putInfo("url", PathFormat.format(savePath));
                    storageState.putInfo("type", suffix);
                    storageState.putInfo("original", originFileName + suffix);
                }

                return storageState;
            }
        } catch (Exception var15) {
            var15.printStackTrace();
            System.out.println(var15.getMessage());
            return new BaseState(false, 4);
        }
    }

注意:如果项目的根路径有带上项目的名称,比如我的是:http://localhost:8080/jblog/,需要在config.json中设置”imageUrlPrefix”: “/jblog”, /* 图片访问路径前缀 */。

如果有其它问题,可以参考http://blog.csdn.net/fansunion/article/details/41420443

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值