1. 程式人生 > >ueditor與ssh整合之上傳圖片篇

ueditor與ssh整合之上傳圖片篇

1. 加入外掛

<script type="text/javascript" src="scripts/ueditor/ueditor.config.js"></script>

<script type="text/javascript" src="scripts/ueditor/ueditor.all.min.js"></script>

2.建立容器

<script id="container" name="content" type="text/plain">
    </script>

3.例項化容器

var ue = UE.getEditor('container',{
initialFrameHeight:600, 
})

4.重寫struts2的攔截器

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class MyStrutsFilter extends StrutsPrepareAndExecuteFilter {
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		String url = request.getRequestURI();         
		System.out.println(url);         
		if (url.contains("/blog/scripts/ueditor/jsp/")) {             
			System.out.println("使用自定義過濾器");             
			chain.doFilter(req, res);         
		}else{             
			System.out.println("使用預設過濾器");             
			super.doFilter(req, res, chain);         
		} 
	}
}

5.web.xml裡

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
替換成自己寫的攔截器

 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>com.blog.until.MyStrutsFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

5.修改ueditor/ueditor.config.js檔案裡

window.UEDITOR_HOME_URL = "/blog/scripts/ueditor/"

6.修改ueditor/jsp/config.json檔案

"imageUrlPrefix": "/blog", /*圖片字首(專案名) */
"imagePathFormat": "/img/{yyyy}{mm}{dd}/{time}{rand:6}",(圖片儲存地址)