- 内置 IOC、AOP、ORM、DAO、MVC 等特性
- 基于 Servlet 3.0 规范
- 使用 Java 注解取代 XML 配置
- 客户端可使用 HTML 或 JSP 作为视图模板
- 服务端可发布 REST 服务(使用 REST 插件)
- 客户端通过 AJAX 获取服务端数据并进行界面渲染
- 面向基于 Web 的中小规模的应用程序
- 新手能在较短时间内入门
- 核心具有良好的定制性且插件易于扩展
整个工程的目录结构如下:
smart-sample/ ┗ src/ ┗ main/ ┗ java/ ┗ resources/ ┗ webapp/ ┗ pom.xml 在 java 目录下,创建以下包名目录结构:
org/ ┗ smart4j/ ┗ sample/ ┗ action/ ┗ entity/ ┗ service/ 可见,基础包名为:org.smart4j.sample,下面的配置中会用到它。
编辑 pom.xml 文件,添加 smart-framework 依赖:
<dependency> <groupId>org.smart4j</groupId> <artifactId>smart-framework</artifactId> <version>[版本号]</version> </dependency>提示:需要指定具体的版本号。若使用相关 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 提示:需根据实际情况修改以上配置。
packageorg.smart4j.sample.entity; importorg.smart4j.framework.orm.annotation.Entity; @EntitypublicclassUser{privatelongid; privateStringusername; privateStringpassword; // getter/setter }Service 接口
packageorg.smart4j.sample.service; importjava.util.List; importjava.util.Map; importorg.smart4j.sample.entity.User; publicinterfaceUserService{List<User> findUserList(); UserfindUser(longid); booleansaveUser(Map<String, Object> fieldMap); booleanupdateUser(longid, Map<String, Object> fieldMap); booleandeleteUser(longid)}Service 实现
packageorg.smart4j.sample.service.impl; importjava.util.List; importjava.util.Map; importorg.smart4j.framework.orm.DataSet; importorg.smart4j.framework.tx.annotation.Service; importorg.smart4j.framework.tx.annotation.Transaction; importorg.smart4j.sample.entity.User; importorg.smart4j.sample.service.UserService; @ServicepublicclassUserServiceImplimplementsUserService{@OverridepublicList<User> findUserList(){returnDataSet.selectList(User.class)} @OverridepublicUserfindUser(longid){returnDataSet.select(User.class, "id = ?", id)} @Override@TransactionpublicbooleansaveUser(Map<String, Object> fieldMap){returnDataSet.insert(User.class, fieldMap)} @Override@TransactionpublicbooleanupdateUser(longid, Map<String, Object> fieldMap){returnDataSet.update(User.class, fieldMap, "id = ?", id)} @Override@TransactionpublicbooleandeleteUser(longid){returnDataSet.delete(User.class, "id = ?", id)} }packageorg.smart4j.sample.action; importjava.util.List; importjava.util.Map; importorg.smart4j.framework.ioc.annotation.Inject; importorg.smart4j.framework.mvc.DataContext; importorg.smart4j.framework.mvc.annotation.Action; importorg.smart4j.framework.mvc.annotation.Request; importorg.smart4j.framework.mvc.bean.Params; importorg.smart4j.framework.mvc.bean.Result; importorg.smart4j.framework.mvc.bean.View; importorg.smart4j.sample.entity.User; importorg.smart4j.sample.service.UserService; @ActionpublicclassUserAction{@InjectprivateUserServiceuserService; @Request.Get("/users") publicViewindex(){List<User> userList = userService.findUserList(); DataContext.Request.put("userList", userList); returnnewView("user.jsp")} @Request.Get("/user") publicViewcreate(){returnnewView("user_create.jsp")} @Request.Post("/user") publicResultsave(Paramsparams){Map<String, Object> fieldMap = params.getFieldMap(); booleanresult = userService.saveUser(fieldMap); returnnewResult(result)} @Request.Get("/user/{id}") publicViewedit(longid){Useruser = userService.findUser(id); DataContext.Request.put("user", user); returnnewView("user_edit.jsp")} @Request.Put("/user/{id}") publicResultupdate(longid, Paramsparams){Map<String, Object> fieldMap = params.getFieldMap(); booleanresult = userService.updateUser(id, fieldMap); returnnewResult(result)} @Request.Delete("/user/{id}") publicResultdelete(longid){booleanresult = userService.deleteUser(id); returnnewResult(result)} }在 Action 中使用了 JSP 作为视图展现技术,需要编写以下 JSP 文件:
- user.jsp
- user_list.jsp
- user_create.jsp
- user_edit.jsp
提示:更多相关细节,请参考 Smart Sample 示例。
TODO
- 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
注意:插件依赖于框架,不能独立使用。
- smart-plugin-security -- 基于 Apache Shiro 的安全控制插件
- smart-plugin-cache -- 基于注解的 Cache 插件
- smart-plugin-i18n -- 通用的 I18N 插件
- smart-plugin-mail -- 基于 Apache Commons Email 的邮件收发插件
- smart-plugin-template -- 基于 Apache Velocity 的模板引擎插件
- smart-plugin-job -- 基于 Quartz 的作业调度插件
- smart-plugin-soap -- 基于 Apache CXF 的 SOAP Web Service 插件
- smart-plugin-rest -- 基于 Apache CXF 的 REST Web Service 插件
- smart-plugin-hessian -- 基于 Hessian 的 RMI 插件
- smart-plugin-xmlrpc -- 基于 Apache XML-RPC 的 XML-RPC 插件
- smart-plugin-search -- 基于 Apache Lucene 的搜索引擎插件
- smart-plugin-mybatis -- 基于 MyBatis 的数据持久层插件
- smart-plugin-args -- 强大的 Action 方法参数绑定的插件
- smart-plugin-c3p0 -- 基于 C3P0 的连接池插件
- smart-plugin-druid -- 基于 Druid 的连接池插件
注意:模块不依赖于框架,可以独立使用。
- smart-sso -- 基于 Jasig CAS 的 SSO 模块
- smart-cache -- 通用的 Cache 模块与基于内存的实现
- smart-cache-ehcache -- 基于 EhCache 的 Cache 模块
- smart-cache-redis -- 基于 Jedis 的 Cache 模块
- Smart 系列博文:http://my.oschina.net/huangyong/blog/158380
- Maven 那点事儿:http://my.oschina.net/huangyong/blog/194583
