百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Smart Framework一款轻量级 Java Web 框架

ccwgpt 2024-10-28 14:59 31 浏览 0 评论

简介

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

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

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

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

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

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

入门

1. 创建一个 Maven Web 工程

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

smart-sample/
  ┗ src/
    ┗ main/
      ┗ java/
      ┗ resources/
      ┗ webapp/
  ┗ pom.xml

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

org/
  ┗ smart4j/
    ┗ sample/
      ┗ action/
      ┗ entity/
      ┗ service/

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

2. 配置 Maven 依赖

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

<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 类

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 接口

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 实现

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 类

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

提示:更多相关细节,请参考 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 模块

相关推荐

用Steam启动Epic游戏会更快吗?(epic怎么用steam启动)

Epic商店很香,但也有不少抱怨,其中一条是启动游戏太慢。那么,如果让Steam启动Epic游戏,会不会速度更快?众所周知,Steam可以启动非Steam游戏,方法是在客户端左下方点击“添加游戏”,然...

Docker看这一篇入门就够了(dockerl)

安装DockerLinux:$curl-fsSLhttps://get.docker.com-oget-docker.sh$sudoshget-docker.sh注意:如果安装了旧版...

AYUI 炫丽PC开发UI框架2016年6月15日对外免费开发使用 [1]

2016年6月15日,我AY对外发布AYUI(WPF4.0开发)的UI框架,开发时候,你可以无任何影响的去开发PC电脑上的软件exe程序。AYUI兼容XP操作系统,在Win7/8/8.1/10上都顺利...

别再说C#/C++套壳方案多了!Tauri这“借壳生蛋”你可能没看懂!

浏览器套壳方案,C#和C++有更多,你说的没错,从数量和历史积淀来看,C#和C++确实有不少方式来套壳浏览器,让Web内容在桌面应用里跑起来。但咱们得把这套壳二字掰扯清楚,因为这里面学问可大了!不同的...

OneCode 核心概念解析——Page(页面)

在接触到OneCode最先接触到的就是,Page页面,在低代码引擎中,页面(Page)设计的灵活性是平衡“快速开发”与“复杂需求适配”的关键。以下从架构设计、组件系统、配置能力等维度,解析确...

React是最后的前端框架吗,为什么这么说的?

油管上有一位叫Theo的博主说,React是终极前端框架,为什么这么说呢?让我们来看看其逻辑:这个标题看起来像假的,对吧?React之后明明有无数新框架诞生,凭什么说它是最后一个?我说的“最后一个”不...

面试辅导(二):2025前端面试密码:用3个底层逻辑征服技术官

面试官放下简历,手指在桌上敲了三下:"你上次解决的技术难题,现在回头看有什么不足?"眼前的候选人瞬间僵住——这是上周真实发生在蚂蚁金服终面的场景。2025年的前端战场早已不是框架熟练...

前端新星崛起!Astro框架能否终结React的霸主地位?

引言:当"背着背包的全能选手"遇上"轻装上阵的短跑冠军"如果你是一名前端开发者,2024年的框架之争绝对让你眼花缭乱——一边是React这位"背着全家桶的全能选...

基于函数计算的 BFF 架构(基于函数计算的 bff 架构是什么)

什么是BFFBFF全称是BackendsForFrontends(服务于前端的后端),起源于2015年SamNewman一篇博客文章《Pattern:BackendsFor...

谷歌 Prompt Engineering 白皮书:2025年 AI 提示词工程的 10 个技巧

在AI技术飞速发展的当下,如何更高效地与大语言模型(LLM)沟通,以获取更准确、更有价值的输出,成为了一个备受关注的问题。谷歌最新发布的《PromptEngineering》白皮书,为这一问题提供了...

光的艺术:灯具创意设计(灯光艺术作品展示)

本文转自|艺术与设计微信号|artdesign_org_cn“光”是文明的起源,是思维的开端,同样也是人类睁眼的开始。每个人在出生一刻,便接受了光的照耀和洗礼。远古时候,人们将光奉为神明,用火来...

MoE模型已成新风口,AI基础设施竞速升级

机器之心报道编辑:Panda因为基准测试成绩与实际表现相差较大,近期开源的Llama4系列模型正陷入争议的漩涡之中,但有一点却毫无疑问:MoE(混合专家)定然是未来AI大模型的主流范式之一。...

Meta Spatial SDK重大改进:重塑Horizon OS应用开发格局

由文心大模型生成的文章摘要Meta持续深耕SpatialSDK技术生态,提供开自去年9月正式推出以来,Meta持续深耕其SpatialSDK技术生态,通过一系列重大迭代与功能增强,不断革新H...

&quot;上云&quot;到底是个啥?用&quot;租房&quot;给你讲明白IaaS/PaaS/SaaS的区别

半夜三点被机房报警电话惊醒,顶着黑眼圈排查服务器故障——这是十年前互联网公司运维的日常。而现在,程序员小王正敷着面膜刷剧,因为公司的系统全"搬"到了云上。"部署到云上"...

php宝塔搭建部署thinkphp机械设备响应式企业网站php源码

大家好啊,欢迎来到web测评。本期给大家带来一套php开发的机械设备响应式企业网站php源码,上次是谁要的系统项目啊,帮你找到了,还说不会搭建,让我帮忙录制一期教程,趁着今天有空,简单的录制测试了一下...

取消回复欢迎 发表评论: