感谢波总的提醒。不用重复造轮子
基于maven项目构建的。
首先引入依赖。
创建一个继承自 JFinalConfig的类
由于web.xml中之前有其他全局元素,所以继承了 WebBuilder 做扩展
继承 UndertowServer
class MyUndertowServer extends UndertowServer {
Consumer<MyWebBuilder> webBuilderConsumer = e -> {
e.addInitParam("webAppRootKey", "AppleAPP");
e.addInitParam("contextConfigLocation", "classpath:spring/ApplicationContext.xml");
e.addInitParam("log4jConfigLocation", "classpath:log4j.properties");
e.addListener("org.springframework.web.util.Log4jConfigListener");
e.addListener("org.springframework.web.context.ContextLoaderListener");
e.addFilter("encodingFilter", "org.springframework.web.filter.CharacterEncodingFilter")
.addFilterInitParam("encodingFilter", "encoding", "utf-8")
.addFilterUrlMapping("encodingFilter", "/*");
e.addFilter("DruidWebStatFilter", "com.alibaba.druid.support.http.WebStatFilter")
.addFilterInitParam("DruidWebStatFilter", "exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*")
.addFilterUrlMapping("DruidWebStatFilter", "/*");
e.addFilter("LoginFilter", "com.renrencai.util.LoginFilter")
.addFilterUrlMapping("LoginFilter", "*.do");
e.addServlet("DruidStatView", "com.alibaba.druid.support.http.StatViewServlet")
.addServletMapping("DruidStatView", "/druid/*");
e.addServlet("springMvc", "org.springframework.web.servlet.DispatcherServlet")
.addServletInitParam("springMvc", "contextConfigLocation", "classpath:spring/ApplicationContext-mvc.xml")
.setServletLoadOnStartup("springMvc", 1)
.addServletMapping("springMvc", "/*");
};
protected Consumer<MyWebBuilder> builderConsumer = webBuilderConsumer;
public MyUndertowServer(Class<? extends JFinalConfig> t) {
super(new UndertowConfig(t));
}
public MyUndertowServer(UndertowConfig undertowConfig) {
super(undertowConfig);
}
protected void init() {
super.builder = Undertow.builder();
super.configUndertow();
configWeb();
}
protected void configWeb() {
MyWebBuilder wb = new MyWebBuilder(this);
builderConsumer.accept(wb);
}
}
最后启动项目:
public class AdminMain {
public static void main(String[] args) {
try {
MyUndertowServer server = new MyUndertowServer(MyJFinalConfig.class);
server.config(config -> config.setSessionTimeout(30));
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
可自行添加 undertow.txt undertow-pro.txt 作配置
undertow.devMode=true undertow.port=8090 undertow.host=0.0.0.0 undertow.contextPath=/ undertow.gzip.enable=true undertow.gzip.level=5 undertow.gzip.minLength=1024 undertow.ioThreads=4