最近再弄富文本框,选择了UEditor,原因是:界面漂亮,百度开源的
然而,

开启整合之路(怎么做)

1.下载插件

下载只有将插件放在Webapp下,如图

2、修改

导入之后我们就需要修改一些参数已满足我们的需求。如图

我们要修改图片的保存路径,因为插件默认是保存在项目部署的路径下,每次重新部署图片都会消失,所以我们需要将图片单独保存到一个图片服务器下,展示图片的controller如下

@RequestMapping(value = "/showImage")
public void showImage(HttpServletRequest request, HttpServletResponse response,String filePath){
String rootPath=request.getSession().getServletContext().getRealPath("/");
String absolutePath=rootPath+filePath;
//截取文件后缀名
String suffix=absolutePath.substring(absolutePath.lastIndexOf(".")+1);
String responseType = "image/jpeg";
if ("png".equals(suffix)) {
responseType = "image/png";
} else if ("jpg".equals(suffix) || "jpeg".equals(suffix)) {
responseType = "image/jpeg";
} else if ("gif".equals(suffix)) {
responseType = "image/gif";
}
response.setContentType(responseType);
FileInputStream inputStream=null;
OutputStream os=null;
try {
int count;
inputStream=new FileInputStream(absolutePath);
os=response.getOutputStream();
byte[] buffer=new byte[1024*1024];
while ((count=inputStream.read(buffer))!=-1){
os.write(buffer,0,count);
} }catch (IOException ex){
logger.error("图片找不到{}",ex);
}finally {
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os!=null){
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

如果不想改的话我们这样就弄好了,由于我的项目用的springMVC,如果你在web.xml那边的配置如图

多图上传时有可能会碰到弹出框不能显示的问题
因为弹出框的静态资源是image.html,这个image.html没有对应的controller的话,被DispatcherServlet拦截之后,就会找不到controller,然后就会抛出404的错误,目前暂时的处理办法是,修个image.html的后缀为htm,使其不被拦截。

3.使用

前台使用

<script type="text/javascript" src="../static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="../static/ueditor/ueditor.all.js"></script> <input type="hidden" class="span12 required" name="info" id="info" value='${mallProduct.info}' />
<script id="editor" type="text/plain" style="width:96%;height:300px;" class="required"></script> var ue = UE.getEditor('editor');
ue.ready(function() {
ue.setContent($("#info").val());
});

相关的代码结构以及原理

UEditor的上传功能是在controller.jsp中实现的

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.baidu.ueditor.ActionEnter" %>
<%@ page trimDirectiveWhitespaces="true" %>
<% request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html"); String rootPath = application.getRealPath( "/" ); out.write( new ActionEnter( request, rootPath ).exec() ); %>

入口在ueditor.config.js中。
=========================================================================

完美的分割线

如果你觉得用controller.jsp作为上传服务不妥的话,我们也可以修改将上传代码放入Controller中
代码如下:

package com.lyz.controller;

import com.baidu.ueditor.ActionEnter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter; /**
* Created by xiangwei on 2017/7/16.
*/
@Controller
@RequestMapping("/ueditor")
public class UeditorController {
private Logger logger= LoggerFactory.getLogger(UeditorController.class);
@RequestMapping(value = "/dispatch")
public void config(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/json");
String rootPath = request.getSession().getServletContext().getRealPath("/");
response.setHeader("Content-Type" , "text/html");
try {
String exec = new ActionEnter(request, rootPath).exec();
PrintWriter writer = response.getWriter();
writer.write(exec);
writer.flush();
writer.close();
} catch (IOException e) {
logger.error("图片上传失败!");
e.printStackTrace();
} } }

然后修改ueditor.config.js文件中的

再修改ConfigManager类的getConfigPath()方法,将路径指定到config.json的路径。

如:

private String getConfigPath () {
// return this.parentPath + File.separator + ConfigManager.configFileName;
return this.rootPath+File.separator+"static/ueditor/"+"jsp"+File.separator+ConfigManager.configFileName;
}

UEditor使用总结(与SpringMVC整合)的更多相关文章

  1. springMVC -- 整合UEditor(富文本编辑器)

    工作中需要用到UEditor编辑文本,在与springMVC进行整合时,出现了一些问题,结果导致,在进行图片上传时出现如下提示: 上网查询了很多相关资料,此处简要记录下,防止以后遇到类似问题. 一种方 ...

  2. (转)Dubbo与Zookeeper、SpringMVC整合和使用

    原文地址: https://my.oschina.net/zhengweishan/blog/693163 Dubbo与Zookeeper.SpringMVC整合和使用 osc码云托管地址:http: ...

  3. SSM整合(三):Spring4与Mybatis3与SpringMVC整合

    源码下载 SSMDemo 上一节整合了Mybatis3与Spring4,接下来整合SpringMVC! 说明:整合SpringMVC必须是在web项目中,所以前期,新建的就是web项目! 本节全部采用 ...

  4. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  5. springmvc整合fastjson

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...

  7. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  8. Springmvc整合tiles框架简单入门示例(maven)

    Springmvc整合tiles框架简单入门示例(maven) 本教程基于Springmvc,spring mvc和maven怎么弄就不具体说了,这边就只简单说tiles框架的整合. 先贴上源码(免积 ...

  9. SpringMVC整合Tiles框架

    SpringMVC整合Tiles框架 Tiles组件 tiles-iconfig.xml Tiles是一个JSP布局框架. Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离 ...

随机推荐

  1. C# textbox中输入时加限制条件 // C#Winform下限制TextBox只能输入数字 // 才疏学浅(TextBox 小数点不能在首位+只能输入数字)

    textbox中输入时加限制条件 分类: C# winform2008-08-26 08:30 306人阅读 评论(0) 收藏 举报 textbox正则表达式object 1.用正则表达式! 2.使用 ...

  2. 【POJ 3074】 Sudoku

    [题目链接] http://poj.org/problem?id=3074 [算法] 将数独问题转化为精确覆盖问题,用Dancing Links求解 转化方法如下 : 我们知道,在一个数独中 : 1. ...

  3. Java获取NTP网络时间

    最近项目中涉及到一个时间验证的问题,需要根据当前时间来验证业务数据是否过期.所以直接写代码如下: new java.util.Date().getTime();          结果测试的时候出现了 ...

  4. Servlet初始化与异步支持

    Shared libraries(共享库) / runtimes pluggability(运行时插件能力) 1.Servlet容器启动会扫描,当前应用里面每一个jar包的 ServletContai ...

  5. python 11:range(起始索引,终止索引,步数)(默认情况下步数为1,生成从起始索引,每次增加(终止索引-起始索引)/步数,到终止索引前的数字串)

    squares = [] for value in range(1,11): #第三参数默认为1,生成从1开始,每次增加1步数,到11前的10为止的数字串 square = value ** 2 sq ...

  6. python--7、面向对象

    什么是面向对象 对象,即抽象的一类事物中的某个具体的个体.这个世界中存在的一切皆为对象,不存在的也能创建出来. 较之面向过程的区别: 编程的复杂度远高于面向过程,不了解面向对象而立即上手基于它设计程序 ...

  7. Linux通信之同步阻塞模式

    [参考]韦东山 教学笔记 1. 原子操作原子操作指的是在执行过程中不会被别的代码路径所中断的操作.常用原子操作函数举例:atomic_t v = ATOMIC_INIT(0); //定义原子变量v并初 ...

  8. 用List表示多重性

    练习目标-在类中使用List作为模拟集合操作: 在本练习中,将用List实现银行与客户间的多重关系. 任务 对银行来说,可添加Bank类. Bank 对象跟踪自身与其客户间的关系.用Customer对 ...

  9. DataTable如何去除重复的行

    两种方法1 数据库直接去除重复select distinct * from 表名去除了重复行distinct 2 对 DataTable直接进行操作DataTable dt=db.GetDt(&quo ...

  10. 安卓代码迁移:Make.exe: *** [***.o]Error 1

    描述:NDK开发中显示,windows环境下NDK开发 解决办法:查找系统环境变量,找到关于Cygwin的环境变量或其他无效的环境变量删除处理.