1. 基于Jfinal启动,导入jfinal-undertow 此包基于NIO的高性能Web嵌入式服务器服务器启动;
maven中添加jfinal依赖:
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal-undertow</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.5</version>
</dependency>
配置所需文件:
package config;
import com.jfinal.config.*;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
public class Config extends JFinalConfig {
// 启动jfinal 函数
public static void main(String[] args) {
UndertowServer.start(Config.class, 80, true);
}
// 常量配置
public void configConstant(Constants me) {
me.setDevMode(true);
//限制上传100M
me.setMaxPostSize(104857600);
//TODO 因为很多人配置jdk环境问题,默认使用cgilb代理
me.setProxyFactory(new CglibProxyFactory());
}
// 路由配置
public void configRoute(Routes me) {
me.add(new AdminRouter());
me.add(new BiRouter());
me.add(new CrmRouter());
me.add(new OaRouter());
me.add(new WorkRouter());
}
public void configEngine(Engine me) {}
public void configPlugin(Plugins me) {}
public void configInterceptor(Interceptors me) {}
public void configHandler(Handlers me) {}
}
其中路由配置里面一个示例,例如CrmRouter如下:具体访问的Controller的url
import com.jfinal.config.Routes;
import com.kakarote.crm9.erp.crm.controller.*;
public class CrmRouter extends Routes {
@Override
public void config() {
addInterceptor(new CrmInterceptor());
add("/CrmProduct", CrmProductController.class);
add("/CrmProductCategory", CrmProductCategoryController.class);
add("/CrmLeads", CrmLeadsController.class);
add("/CrmCustomer", CrmCustomerController.class);
add("/CrmBusiness", CrmBusinessController.class);
add("/CrmContacts", CrmContactsController.class);
add("/CrmContract", CrmContractController.class);
add("/CrmReceivables", CrmReceivablesController.class);
add("/CrmRecord",CrmRecordController.class);
add("/Crm/ReceivablesPlan",CrmReceivablesPlanController.class);
add("/Crm/Instrument",InstrumentController.class);
add("/CrmBackLog",CrmBackLogController.class);
add("/CrmRobot",CrmRobotController.class);
add("/CrmAudioRobot",CrmAudioRobotController.class);
add("/CrmQuestionAnswering",CrmQuestionAnsweringCntroller.class);
add("/CrmSendEmail",CrmSendEmailController.class);
}
}
启动jfinal Application启动类
import com.jfinal.server.undertow.UndertowConfig;
import com.jfinal.server.undertow.UndertowServer;
import com.kakarote.crm9.common.config.JfinalConfig;
import com.kakarote.crm9.common.constant.BaseConstant;
public class Application {
public static void main(String[] args) {
UndertowConfig config=new UndertowConfig(JfinalConfig.class,"config/undertow.txt");
config.setResourcePath("src/main/webapp,"+ BaseConstant.UPLOAD_PATH);
config.setServerName(BaseConstant.NAME);
UndertowServer.create(config).start();
System.out.println("系统服务启动完成.......");
}
}
如果不想写一个单独的启动类就在本来的JfinalConfig里面写main方法直接启动,如下:
// 启动jfinal 函数
public static void main(String[] args) {
UndertowServer.start(JfinalConfig.class, 80, true);
}
直接右键启动 或者debug启动,启动成功显示如下:
2. Jfinal基于Tomcat启动
修改web.xml文件,项目 src/main/webapp/WEB-INF,在 web.xml 修改 内容如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>jfinal</filter-name>
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
<init-param>
<param-name>configClass</param-name>
<param-value>config.Config</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jfinal</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
maven pom文件中添加 servlet api 依赖包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
添加Tomcat7插件,pom中添加
<build>
<finalName>base</finalName>
<plugins>
<plugin>
<!--maven对应的tomcat插件,启动tomcat的时候使用-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
添加Tomcat7后 用tomcat启动的时候要记得 注释掉jfinal-undertow 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acsiont</groupId>
<artifactId>jfinal_tomcat</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>jfinal_tomcat Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- servlet 依赖->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- jfinal 依赖-->
<!-- <dependency>-->
<!-- <groupId>com.jfinal</groupId>-->
<!-- <artifactId>jfinal-undertow</artifactId>-->
<!-- <version>1.9</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>4.5</version>
</dependency>
</dependencies>
<build>
<finalName>base</finalName>
<plugins>
<plugin>
<!--maven对应的tomcat插件,启动tomcat的时候使用-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/</path>
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Tomcat启动的时候如果写了Application这种弄启动类的话也要注释掉:
package com.kakarote.crm9;
//import com.jfinal.server.undertow.UndertowConfig;
//import com.jfinal.server.undertow.UndertowServer;
//import com.kakarote.crm9.common.config.JfinalConfig;
//import com.kakarote.crm9.common.constant.BaseConstant;
//import com.kakarote.crm9.erp.crm.controller.WebSocketController;
public class Application {
public static void main(String[] args) {
// UndertowConfig config=new UndertowConfig(JfinalConfig.class,"config/undertow.txt");
// config.setResourcePath("src/main/webapp,"+ BaseConstant.UPLOAD_PATH);
// config.setServerName(BaseConstant.NAME);
// UndertowServer.create(config).start();
// UndertowServer.create(JfinalConfig.class)
// .configWeb(builder -> {
// // 配置WebSocket需使用ServerEndpoint注解
// builder.addWebSocketEndpoint(WebSocketController.class);
// })
// .start();
// System.out.println("系统服务启动完成.......");
}
}
没有单独的Application启动类的话就注释掉JfinalConfig里面的main启动方法:
Tomcat7 启动 --> 双击刚刚添加的tomcat插件
启动结果 如下图就成功啦
访问测试:
连接websocket,连接成功!