Struts2 integrates CKEditor and CKFinder for upload

Source: Internet
Author: User

The integration process with Struts2 is really easy. however, after implementation, the image cannot be uploaded. After the upload, "the file does not exist" is displayed, and some information is found online. After summary, this function is finally implemented. The cause of this problem is the struts2 interceptor, in the web. xml, you can see clearly: [html] <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping> configuration, struts2 intercepts all URLs starting. in CKEditor configuration, CKEditor is implemented by using servlet, which also works by intercepting URL. Some servlet configurations of CKeditor are as follows: [html] <servlet-mapping> <servlet-name> ConnectorServlet </servlet-name> <url-pattern>/ckfinder/core/connector/java/connector. java </url-pattern> </ser Vlet-mapping> now it is clear that the struts2 interceptor intercepts all URLs starting, the CKEd servlet interceptor intercepts all requests with "/ckfinder/core/connector/java/connector. URL starting with java. at that time, when CKEditor uploads images, the URL contains "/" (indicating the root directory of the website), which is intercepted by the struts2 interceptor first, in this way, the uploaded URL request will not be forwarded to the CKEditor servlet, and all the uploaded images will not be successful. if the problem is found, the problem is solved. The simplest way is to change the struts2 Interceptor "/*" "#. action or "*. do ", you can change it to [html] <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern> *. action </url-pattern> </filter-map Ping> the second method is to define a CKeditor filter to inherit the filter FilterDispatcher of Struts2. The complete code is [java] package cn.edu. csu. software. filters; 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. filterDispatcher; @ Suppre SsWarnings ("deprecation") public class FCKFilter extends FilterDispatcher {public void doFilter (ServletRequest req, res, FilterChain chain) throws IOException, response {HttpServletRequest request = (HttpServletRequest) req; string URI = request. getRequestURI (); String [] uriArray = URI. split ("/ckfinder/core/connector/java/*/"); int arrayLen = uriArray. length; if (arrayLen> = 2) {chain. doFilter (req, res);} else {super. doFilter (req, res, chain) ;}} the method above overwrites the doFilter method in FilterDispatcher of struts2. The execution principle is: Get the complete http url address, then, determine whether the URL contains the CKEditor servlet interception rule "/ckfinder/core/connector/java/*/". If it contains, struts2 will not block the URL request and directly forward it, the servlet of the friend CKEditor is processed. If it is not included, the ze interceptor intercepts this URL. After struts2 processes the custom filter, it must be processed on the web. modify the interceptor class of struts2 in xml. You cannot use the default interceptor class FilterDispatcher of struts2. However, please rest assured that the custom filter FCKFilter inherits all the methods in FilterDispatcher, so the project is still running as usual. Www.2cto.com <! -- Struts2 configuration file --> [html] <filter> <filter-name> struts2 </filter-name> <! -- <Filter-class> org. apache. struts2.dispatcher. filterDispatcher </filter-class> --> <filter-class> cn. itcast. utils. FCKFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.