前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >轻量级MVC服务器actframework

轻量级MVC服务器actframework

作者头像
阿超
发布2024-08-23 19:47:59
290
发布2024-08-23 19:47:59
举报
文章被收录于专栏:快乐阿超

一个人追求的目标越高,他的才能就发展得越快,对社会就越有益,我确信这也是一个真理。——玛克西姆·高尔基

链接:

GitHub - actframework/actframework: An easy to use Java MVC server stack

Install

Add act-starter-parent into your pom.xml file

代码语言:javascript
复制
<parent>
  <groupId>org.actframework</groupId>
  <artifactId>act-starter-parent</artifactId>
  <version>1.9.2.0</version>
</parent>

Or use maven archetype to start a new project:

代码语言:javascript
复制
mvn archetype:generate -B \
    -DgroupId=com.mycom.helloworld \
    -DartifactId=helloworld \
    -DarchetypeGroupId=org.actframework \
    -DarchetypeArtifactId=archetype-quickstart \
    -DarchetypeVersion=1.9.2.0

tips don’t forget replace the groupId, artifactId and appName in the above script, or you can use interactive mode to generate your project:

代码语言:javascript
复制
mvn archetype:generate -DarchetypeGroupId=org.actframework -DarchetypeArtifactId=archetype-quickstart -DarchetypeVersion=1.9.2.0

Note There are more ActFramework application archetypes for use. Please get them here.

Features

A full stack MVC framework

  • Actframework is NOT a servlet framework. Act app does not run in a servlet container. Instead it run as an independent Java application and it starts in seconds

Unbeatable development experience w/ great performance

  • Never restart your app when you are developing. Act’s dev mode provides hot reloading feature makes it the dream of every Java web app developer. Check out this 3 mins video and feel it!
  • According to TechEmpower Framework Benchmark Act beats most full stack Java web framework on the market. In some cases Act can be over 10 times faster than Springboot

Fully JSR330 Dependency Injection support

  • ActFramework’s DI support is built on top of Genie, a lightweight yet fast JSR330 implementation.
  • Benefit from Act’s powerful class scan feature, it does not require the user to create injector from modules (as the usually way you use Guice). Declare your module and your binding is automatically registered

Superb SPA/Mobile app support

Uncompromising Security

Annotation aware but not annotation stack

Annotation is one of the tool ActFramework used to increase expressiveness. However we do not appreciate crazy annotation stacked code. Instead we make the code to express the intention in a natural way and save the use of annotation whenever possible.

For example, for the following SpringMVC code:

代码语言:javascript
复制
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List listUsersInvoices(
  @PathVariable("userId") int user,
  @RequestParam(value = "date", required = false) Date dateOrNull) {
    ...
}

The corresponding ActFramework app code is:

代码语言:javascript
复制
@GetAction("/user/{user}/invoices")
public List listUsersInvoices(int user, Date date) {
  ...
}

Multi-environment configuration

ActFramework supports the concept of profile which allows you to organize your configurations in different environment (defined by profile) easily. Take a look at the following configurations from one of our real project:

代码语言:javascript
复制
resources
  ├── conf
  │   ├── common
  │   │   ├── app.properties
  │   │   ├── db.properties
  │   │   ├── mail.properties
  │   │   ├── payment.properties
  │   │   └── social.properties
  │   ├── local-no-ui
  │   │   ├── app.properties
  │   │   ├── db.properties
  │   │   └── port.properties
  │   ├── local-sit
  │   │   └── app.properties
  │   ├── local-ui
  │   │   ├── app.properties
  │   │   └── db.properties
  │   ├── sit
  │   │   ├── app.properties
  │   │   └── db.properties
  │   └── uat
  ...

Suppose on your UAT server, you start the application with JVM option -Dprofile=uat, ActFramework will load the configuration in the following sequence:

  1. Read all .properties files in the /resources/conf/common dir
  2. Read all .properties files in the /resources/conf/uat dir

This way ActFramework use the configuration items defined in uat profile to overwrite the same items defined in common profile. The common items that are not overwritten still effective.

Simple yet powerful database support

Powerful view architecture with multiple render engine support

An unbelievable automate testing framework that never presented in any other MVC frameworks

Commonly used tools

Sample code

A HelloWorld app

代码语言:javascript
复制
package demo.helloworld;

import act.Act;
import act.Version;
import org.osgl.mvc.annotation.GetAction;

public class HelloWorldApp {

    @GetAction
    public String sayHelloTo(@DefaultValue("World") String who) {
        return "Hello " + who + "!";
    }

    public static void main(String[] args) throws Exception {
        Act.start();
    }

}

See this 7 mins video on how to create HelloWorld in Eclipse from scratch. or for users without youtube access

A full RESTful service

代码语言:javascript
复制
package demo.rest;

import act.controller.Controller;
import act.db.morphia.MorphiaAdaptiveRecord;
import act.db.morphia.MorphiaDao;
import org.mongodb.morphia.annotations.Entity;
import org.osgl.mvc.annotation.*;

import java.util.Map;

import static act.controller.Controller.Util.notFoundIfNull;

@Entity("user")
public class User extends MorphiaAdaptiveRecord<User> {

    @UrlContext("user")
    public static class Service extends MorphiaDao<User> {

        @PostAction
        public User create(User user) {
            return save(user);
        }

        @GetAction
        public Iterable<User> list() {
            return findAll();
        }

        @GetAction("{id}")
        public User show(@DbBind("id") User user) {
            return user;
        }

        @PutAction("{id}")
        public User update(@DbBind("id") @NotNull User user, Map<String, Object> data) {
            user.mergeValues(data);
            return save(user);
        }

        @DeleteAction("{id}")
        public void delete(String id) {
            deleteById(id);
        }
    }

    public static void main(String[] args) throws Exception {
        Act.start();
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Install
  • Features
  • Sample code
    • A HelloWorld app
      • A full RESTful service
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档