README.md 9.3 KB
Newer Older
黄勇 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Smart Framework

## 简介

### 1. 它是一款轻量级 Java Web 框架

- 内置 IOC、AOP、ORM、DAO、MVC 等特性
- 基于 Servlet 3.0 规范
- 使用 Java 注解取代 XML 配置

### 2. 它使应用充分做到“前后端分离”

- 客户端可使用 HTML 或 JSP 作为视图模板
- 服务端可发布 REST 服务(使用 REST 插件)
- 客户端通过 AJAX 获取服务端数据并进行界面渲染

### 3. 它可提高应用程序的开发效率

- 面向基于 Web 的中小规模的应用程序
- 新手能在较短时间内入门
- 核心具有良好的定制性且插件易于扩展

![架构](http://i.imgur.com/NrH99Zu.png)

## 入门

### 1. 创建一个 Maven Web 工程

整个工程的目录结构如下:

```
smart-sample/
黄勇 已提交
33 34 35 36 37 38
  ┗ src/
    ┗ main/
      ┗ java/
      ┗ resources/
      ┗ webapp/
  ┗ pom.xml
黄勇 已提交
39 40 41 42 43 44
```

`java` 目录下,创建以下包名目录结构:

```
org/
黄勇 已提交
45 46 47 48 49
  ┗ smart4j/
    ┗ sample/
      ┗ action/
      ┗ entity/
      ┗ service/
黄勇 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
```

可见,基础包名为:org.smart4j.sample,下面的配置中会用到它。

### 2. 配置 Maven 依赖

编辑 `pom.xml` 文件,添加 `smart-framework` 依赖:

```xml
<dependency>
    <groupId>org.smart4j</groupId>
    <artifactId>smart-framework</artifactId>
    <version>[版本号]</version>
</dependency>
```

> 提示:需要指定具体的版本号。若使用相关 Smart 插件,则需分别配置。

### 3. 编写 Smart 配置

`resources` 目录下,创建一个名为 `smart.properties` 的文件,内容如下:

```
smart.framework.app.base_package=org.smart4j.sample
smart.framework.app.home_page=/users

smart.framework.jdbc.driver=com.mysql.jdbc.Driver
smart.framework.jdbc.url=jdbc:mysql://localhost:3306/smart-sample
smart.framework.jdbc.username=root
smart.framework.jdbc.password=root
```

> 提示:需根据实际情况修改以上配置。

### 4. 编写 Entity 类

```java
package org.smart4j.sample.entity;

import org.smart4j.framework.orm.annotation.Entity;

@Entity
public class User {

    private long id;

    private String username;

    private String password;

    // getter/setter
}
```

### 5. 编写 Service 接口及其实现

Service 接口

```java
package org.smart4j.sample.service;

import java.util.List;
import java.util.Map;
import org.smart4j.sample.entity.User;

public interface UserService {

    List<User> findUserList();

    User findUser(long id);

    boolean saveUser(Map<String, Object> fieldMap);

    boolean updateUser(long id, Map<String, Object> fieldMap);

    boolean deleteUser(long id);
}
```

Service 实现

```java
package org.smart4j.sample.service.impl;

import java.util.List;
import java.util.Map;
import org.smart4j.framework.orm.DataSet;
import org.smart4j.framework.tx.annotation.Service;
import org.smart4j.framework.tx.annotation.Transaction;
import org.smart4j.sample.entity.User;
import org.smart4j.sample.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Override
    public List<User> findUserList() {
        return DataSet.selectList(User.class);
    }

    @Override
    public User findUser(long id) {
        return DataSet.select(User.class, "id = ?", id);
    }

    @Override
    @Transaction
    public boolean saveUser(Map<String, Object> fieldMap) {
        return DataSet.insert(User.class, fieldMap);
    }

    @Override
    @Transaction
    public boolean updateUser(long id, Map<String, Object> fieldMap) {
        return DataSet.update(User.class, fieldMap, "id = ?", id);
    }

    @Override
    @Transaction
    public boolean deleteUser(long id) {
        return DataSet.delete(User.class, "id = ?", id);
    }
}
```

### 5. 编写 Action 类

```java
package org.smart4j.sample.action;

import java.util.List;
import java.util.Map;
import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.mvc.DataContext;
import org.smart4j.framework.mvc.annotation.Action;
import org.smart4j.framework.mvc.annotation.Request;
import org.smart4j.framework.mvc.bean.Params;
import org.smart4j.framework.mvc.bean.Result;
import org.smart4j.framework.mvc.bean.View;
import org.smart4j.sample.entity.User;
import org.smart4j.sample.service.UserService;

@Action
public class UserAction {

    @Inject
    private UserService userService;

    @Request.Get("/users")
    public View index() {
        List<User> userList = userService.findUserList();
        DataContext.Request.put("userList", userList);
        return new View("user.jsp");
    }

    @Request.Get("/user")
    public View create() {
        return new View("user_create.jsp");
    }

    @Request.Post("/user")
    public Result save(Params params) {
        Map<String, Object> fieldMap = params.getFieldMap();
        boolean result = userService.saveUser(fieldMap);
        return new Result(result);
    }

    @Request.Get("/user/{id}")
    public View edit(long id) {
        User user = userService.findUser(id);
        DataContext.Request.put("user", user);
        return new View("user_edit.jsp");
    }

    @Request.Put("/user/{id}")
    public Result update(long id, Params params) {
        Map<String, Object> fieldMap = params.getFieldMap();
        boolean result = userService.updateUser(id, fieldMap);
        return new Result(result);
    }

    @Request.Delete("/user/{id}")
    public Result delete(long id) {
        boolean result = userService.deleteUser(id);
        return new Result(result);
    }
}
```

### 6. 编写视图

在 Action 中使用了 JSP 作为视图展现技术,需要编写以下 JSP 文件:

- user.jsp
- user_list.jsp
- user_create.jsp
- user_edit.jsp

黄勇 已提交
248
> 提示:更多相关细节,请参考 [Smart Sample](http://git.oschina.net/huangyong/smart-sample) 示例。
黄勇 已提交
249

黄勇 已提交
250 251 252 253
## 提高

TODO

黄勇 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
## 示例

- Smart Sample:http://git.oschina.net/huangyong/smart-sample
- Smart Bootstrap:http://git.oschina.net/huangyong/smart-bootstrap
- Smart REST Server:http://git.oschina.net/huangyong/smart-rest-server
- Smart REST Client:http://git.oschina.net/huangyong/smart-rest-client

## 附录

### 相关插件

> 注意:插件依赖于框架,不能独立使用。

黄勇 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
- [smart-plugin-security](http://git.oschina.net/huangyong/smart-plugin-security) -- 基于 [Apache Shiro](http://shiro.apache.org/) 的安全控制插件
- [smart-plugin-cache](http://git.oschina.net/huangyong/smart-plugin-cache) -- 基于注解的 Cache 插件
- [smart-plugin-i18n](http://git.oschina.net/huangyong/smart-plugin-i18n) -- 通用的 I18N 插件
- [smart-plugin-mail](http://git.oschina.net/huangyong/smart-plugin-mail) -- 基于 [Apache Commons Email](http://commons.apache.org/proper/commons-email/) 的邮件收发插件
- [smart-plugin-template](http://git.oschina.net/huangyong/smart-plugin-template) -- 基于 [Apache Velocity](http://velocity.apache.org/) 的模板引擎插件
- [smart-plugin-job](http://git.oschina.net/huangyong/smart-plugin-job) -- 基于 [Quartz](http://www.quartz-scheduler.org/) 的作业调度插件
- [smart-plugin-soap](http://git.oschina.net/huangyong/smart-plugin-soap) -- 基于 [Apache CXF](http://cxf.apache.org/) 的 SOAP Web Service 插件
- [smart-plugin-rest](http://git.oschina.net/huangyong/smart-plugin-rest) -- 基于 [Apache CXF](http://cxf.apache.org/) 的 REST Web Service 插件
- [smart-plugin-hessian](http://git.oschina.net/huangyong/smart-plugin-hessian) -- 基于 [Hessian](http://hessian.caucho.com/) 的 RMI 插件
- [smart-plugin-xmlrpc](http://git.oschina.net/huangyong/smart-plugin-xmlrpc) -- 基于 [Apache XML-RPC](http://ws.apache.org/xmlrpc/) 的 XML-RPC 插件
- [smart-plugin-search](http://git.oschina.net/huangyong/smart-plugin-search) -- 基于 [Apache Lucene](http://lucene.apache.org/) 的搜索引擎插件
- [smart-plugin-mybatis](http://git.oschina.net/free/smart-plugin-mybatis) -- 基于 [MyBatis](http://mybatis.github.io/mybatis-3/) 的数据持久层插件
- [smart-plugin-args](http://git.oschina.net/free/smart-plugin-args) -- 强大的 Action 方法参数绑定的插件
- [smart-plugin-c3p0](http://git.oschina.net/huangyong/smart-plugin-c3p0) -- 基于 [C3P0](http://sourceforge.net/projects/c3p0/) 的连接池插件
- [smart-plugin-druid](http://git.oschina.net/huangyong/smart-plugin-druid) -- 基于 [Druid](https://github.com/alibaba/druid) 的连接池插件
黄勇 已提交
282 283 284 285 286

### 相关模块

> 注意:模块不依赖于框架,可以独立使用。

黄勇 已提交
287 288 289 290
- [smart-sso](http://git.oschina.net/huangyong/smart-sso) -- 基于 [Jasig CAS](http://www.jasig.org/cas) 的 SSO 模块
- [smart-cache](http://git.oschina.net/huangyong/smart-cache) -- 通用的 Cache 模块与基于内存的实现
- [smart-cache-ehcache](http://git.oschina.net/huangyong/smart-cache-ehcache) -- 基于 [EhCache](http://www.ehcache.org/) 的 Cache 模块
- [smart-cache-redis](http://git.oschina.net/lujianing/smart-cache-redis) -- 基于 [Jedis](https://github.com/xetorthio/jedis/) 的 Cache 模块
黄勇 已提交
291 292 293 294

### 参考资料

- Smart 系列博文:http://my.oschina.net/huangyong/blog/158380
黄勇 已提交
295
- Maven 那点事儿:http://my.oschina.net/huangyong/blog/194583