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