本菜鸡是一名弱弱的测试工程师,最近完成了一个支付相关的项目,项目工作中,需要建立一个模拟支付宝的网关,主要是接收请求并返回数据。作为一名没有丝毫开发经验的菜鸡,初期入门相当费劲,主要还是思维上的转变。由于本人技能水平有限,本篇文章只介绍如何启动服务,并接收请求,返回数据。希望可以给想要入门的童鞋一些帮助。
一、首先建立一个maven工程
二、web.xml文件内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <filter> 7 <filter-name>jfinal</filter-name> 8 <filter-class>com.jfinal.core.JFinalFilter</filter-class> 9 <init-param> 10 <param-name>configClass</param-name> 11 <param-value>demo.DemoConfig</param-value> 12 </init-param> 13 </filter> 14 <filter-mapping> 15 <filter-name>jfinal</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 </web-app>
三、在main中创建DemoConfig.java
继承JFinalConfig类,并实现其中的5个抽象方法。
1 package com.test.common; 2 3 import com.jfinal.config.Constants; 4 import com.jfinal.config.*; 5 6 /** 7 * Created by wangx on 2015/8/23 0023. 8 */ 9 public class DemoConfig extends JFinalConfig { 10 public void configConstant(Constants me){ 11 12 } 13 14 public void configRoute(Routes me){ 15 16 } 17 18 public void configPlugin(Plugins me){ 19 20 } 21 22 public void configInterceptor(Interceptors me){ 23 24 } 25 26 public void configHandler(Handlers me){ 27 28 } 29 }
四、创建一个controller类,接收并返回数据。这里类名为DemoController.java并继承Controller
1 package com.test.controller; 2 3 import com.jfinal.core.Controller; 4 5 /** 6 * Created by wangx on 2015/8/23 0023. 7 */ 8 public class DemoController extends Controller{ 9 public void index(){ 10 renderText("学无止境!"); 11 } 12 }
五、检查一下web.xml中的param-value值,应该与DemoConfig文件的路径一致,包括包名和类名。以下截图也包括了我的工程结构,可以更直观的理解各个类的关系。
六、在DemoConfig中增加路由和main函数,注意端口号不要与其他程序重复。这里设置为80,是http默认端口号
1 package com.test.common; 2 3 import com.jfinal.config.Constants; 4 import com.jfinal.config.*; 5 import com.jfinal.core.JFinal; 6 import com.test.controller.DemoController; 7 8 /** 9 * Created by wangx on 2015/8/23 0023. 10 */ 11 public class DemoConfig extends JFinalConfig { 12 public void configConstant(Constants me){ 13 me.setDevMode(true); 14 } 15 16 public void configRoute(Routes me){ 17 me.add("/", DemoController.class); 18 } 19 20 public void configPlugin(Plugins me){} 21 22 public void configInterceptor(Interceptors me){} 23 24 public void configHandler(Handlers me){} 25 26 public static void main(String[] args) throws Exception { 27 JFinal.start("src/main/webapp", 80, "/", 5); 28 } 29 }
七、运行main函数,控制台print如下内容,则启动成功。这时我们打开网页,访问自己PC的ip
八、返回我们制定的内容
九、设置端口和url,更改DemoConfig中的两个参数,在DemoController中新增一个方法。帮助大家理解。
DemoConfig.java
1 package com.test.common; 2 3 import com.jfinal.config.Constants; 4 import com.jfinal.config.*; 5 import com.jfinal.core.JFinal; 6 import com.test.controller.DemoController; 7 8 /** 9 * Created by wangx on 2015/8/23 0023. 10 */ 11 public class DemoConfig extends JFinalConfig { 12 public void configConstant(Constants me){ 13 me.setDevMode(true); 14 } 15 16 public void configRoute(Routes me){ 17 me.add("/test", DemoController.class); 18 } 19 20 public void configPlugin(Plugins me){} 21 22 public void configInterceptor(Interceptors me){} 23 24 public void configHandler(Handlers me){} 25 26 public static void main(String[] args) throws Exception { 27 JFinal.start("src/main/webapp", 8080, "/", 5); 28 } 29 }
DemoController.java
1 package com.test.controller; 2 3 import com.jfinal.core.Controller; 4 5 /** 6 * Created by wangx on 2015/8/23 0023. 7 */ 8 public class DemoController extends Controller{ 9 public void index(){ 10 renderText("学无止境!"); 11 } 12 13 public void demo(){ 14 renderText("吓死宝宝了!"); 15 } 16 }
访问url:http://localhost:8080/test/demo
到此,可以启动服务,并返回数据。
十、pom文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>jFinalDemo</artifactId> 9 <version>1.0</version> 10 <packaging>war</packaging> 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <maven.compiler.source>1.6</maven.compiler.source> 15 <maven.compiler.target>1.6</maven.compiler.target> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>com.jfinal</groupId> 21 <artifactId>jfinal</artifactId> 22 <version>2.0</version> 23 </dependency> 24 <dependency> 25 <groupId>com.jfinal</groupId> 26 <artifactId>cos</artifactId> 27 <version>26Dec2008</version> 28 </dependency> 29 <dependency> 30 <groupId>com.jfinal</groupId> 31 <artifactId>jetty-server</artifactId> 32 <version>8.1.8</version> 33 </dependency> 34 </dependencies> 35 36 <build> 37 <plugins> 38 <plugin> 39 <groupId>org.apache.maven.plugins</groupId> 40 <artifactId>maven-resources-plugin</artifactId> 41 <configuration> 42 <encoding>${project.build.sourceEncoding}</encoding> 43 </configuration> 44 </plugin> 45 <plugin> 46 <groupId>org.apache.maven.plugins</groupId> 47 <artifactId>maven-compiler-plugin</artifactId> 48 <configuration> 49 <source>${maven.compiler.source}</source> 50 <target>${maven.compiler.target}</target> 51 <encoding>${project.build.sourceEncoding}</encoding> 52 </configuration> 53 </plugin> 54 <plugin> 55 <groupId>org.apache.maven.plugins</groupId> 56 <artifactId>maven-deploy-plugin</artifactId> 57 <configuration> 58 <skip>true</skip> 59 </configuration> 60 </plugin> 61 </plugins> 62 </build> 63 64 </project>