应用集成提供一种简单的集成到现有系统的方案,一般情况下如非特殊需求不建议采用应用集成的方式,最好独立部署。独立部署不但拥有更好的灵活性而且对现有系统零侵入。
1. Servlet集成
请参考web.xml注册finderweb提供的DispatchServlet
2. Spring-MVC集成
/** * springmvc 集成 * Controller方式 */ @Controller public class FinderController implements InitializingBean { @Autowired private ServletContext servletContext; private ActionDispatcher dispatcher; private static final String LOGIN_URL = "/finder?action=finder.login"; @Override public void afterPropertiesSet() throws Exception { /** * 使用 Servlet3.0 的文件上传 */ this.servletContext.setAttribute("javax.servlet.multipart.support", true); this.dispatcher = ActionDispatcherFactory.create(this.servletContext); } /** * 如果不使用Filter方式, 可以将Filter注掉, 并将该类的 Controller 注解放开 * 但是如果使用Controller方式, Finder的集群功能将失效 * * 其他集成方式请在web.xml中配置 com.finder.web.servlet.DispatchServlet 的方式。 * 并且要将servlet配置到最前面,确保在其他mvc框架之前执行。 */ @RequestMapping(value = "/finder") public void execute(HttpServletRequest request, HttpServletResponse response) throws Exception { if(com.skin.finder.filter.SessionFilter.check(request, response, LOGIN_URL)) { this.dispatcher.service(request, response); } } }
在web.xml中注册com.skin.finder.filter.HtmlSupportFilter
<filter>
<filter-name>HtmlSupportFilter</filter-name>
<filter-class>com.skin.finder.filter.HtmlSupportFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HtmlSupportFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3. SpringBoot集成
1. 将finder-web-x.x.x.jar放到磁盘的任意目录。
2. 将demo.zip中的代码复制到你的项目中,新建一个Controller, 在Controller中调用FinderSupport.service(request, response)。
3. 重写一个filter并继承HtmlSupportFilter。
// springboot 集成必须将finder-web-2.5.0.jar 作为一个独立的jar文件加载, 而不能打包在YourProject.jar中。
// java -Dloader.path=webapp/WEB-INF/lib,webapp/WEB-INF/classes -jar bin\YourProject.jar
/**
* springboot springmvc 集成
* Filter方式
*/
public class FinderFilter extends com.skin.finder.filter.DispatchFilter {
/**
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
/**
* 使用servlet3.0的multipart上传
* 如果 springboot 配置项 spring.http.multipart.enabled=true, 并且容器支持servlet3.0, springboot就会配置MultipartConfig
* 这将导致容器会解析 http body, finder将无法再读取 http body, 从而使finder的文件上传无法获取到上传文件。
* 通过此配置项指定finder使用容器提供的上传支持。
*
* 如果要使用finder自己的上传组件:
* 1. spring.http.multipart.enabled=false
* 2. javax.servlet.multipart.support=false 或者不设置此属性
*/
ServletContext servletContext = filterConfig.getServletContext();
servletContext.setAttribute("javax.servlet.multipart.support", true);
super.init(filterConfig);
}
}
/**
* springboot 注册 Filter
*/
@Configuration
public class FilterRegister {
@Bean
public FilterRegistrationBean registrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new FinderFilter());
filterRegistrationBean.addUrlPatterns("/finder");
filterRegistrationBean.setOrder(Integer.MIN_VALUE);
filterRegistrationBean.setEnabled(true);
return filterRegistrationBean;
}
}