搭建轻量级java web框架
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建轻量级java web框架相关的知识,希望对你有一定的参考价值。
MVC这种常用的设计模式可以将应用程序解耦。
若用Servlet来充当C,会产生大量的Servlet。作为一个轻量级框架,我们的目标减少Servlet的数量,对某一业务主题使用统一的Controller,依赖注入Service,结果放入Request或者Response。
本章目标是打造如下的Controller代码:
/** * 处理客户管理的相关请求 */ @Controller public class CustomerController { @Inject private CustomerService customerService; /** * 进入客户列表界面 */ @Action("get:/customerListIndex") public View customerListIndex(Param param) { List<Customer> customerList = customerService.getCustomerList(); return new View("customerList.jsp").addModel("customerList", customerList); } /** * 显示客户基本信息 */ @Action("get:/customerDetailIndex") public View show(Param param) { long id = param.getLong("id"); Customer customer = customerService.getCustomer(id); return new View("customerDetail.jsp").addModel("customer", customer); } /** * 创建客户界面 */ @Action("get:/customerInsertIndex") public View customerInsertIndex(Param param) { return new View("customerCreateIndex.jsp"); } /** * 创建客户 */ @Action("get:/customerInsert") public Data customerInsert(Param param){ Map<String, Object> fieldMap = param.getMap(); boolean result = customerService.insertCustomer(fieldMap); return new Data(result); } /** * 编辑客户界面 */ @Action("get:/customerEditIndex") public View customerEditIndex(Param param){ long id = param.getLong("id"); Customer customer = customerService.getCustomer(id); return new View("customerEditIndex.jsp").addModel("customer",customer); } /** * 编辑客户 */ @Action("pub:/customerEdit") public Data customerEdit(Param param) { long id = param.getLong("id"); Map<String,, Object> fieldMap = param.getMap(); boolean result = customerService.updateCustomer(id, fieldMap); return new Data(result); } /** * 删除客户 */ @Action("delete:/customerDelete") public Data customerDelete(Param param) { long id = param.getLong("id"); boolean result = customerService.deleteCustomer(id); return new Data(result); } }
其中,View对应jsp,Data对应Json数据。
3.2 搭建开发环境
3.2.1 创建框架项目
创建普通的java项目,添加pom.xml文件。敲入三坐标:
<?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>smartFramework</groupId> <artifactId>smartFramework</artifactId> <version>1.0.0</version> </project>
添加Servlet、JSP、JSTL依赖。
<dependencies> <!-- Servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- JSP --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- JSTL --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> </dependencies>
引入SLF4J和Log4J两个依赖,其中SLF4J为Log4J的接口。
<!-- SLF4J --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>
引入mysql与java驱动依赖。
<!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.33</version> <scope>runtime</scope> </dependency>
引入Json序列化依赖。
<!-- Jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.2</version> </dependency>
常用工具类依赖。
<!-- Apache Commons Lang --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!-- Apache Commons Collections --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.0</version> </dependency>
JDBC类库,DbUtils。
<!-- Apache Commons DbUtils --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.6</version> </dependency>
数据库连接池依赖。
<!-- Apache Commons DBCP --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.0.1</version> </dependency>
3.2.2 创建示例项目
以上是关于搭建轻量级java web框架的主要内容,如果未能解决你的问题,请参考以下文章