rife2/rife2
关于
Full-stack, no-declaration, framework to quickly and effortlessly create web applications with modern Java.
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- +1
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文
Welcome
RIFE2 is a full-stack framework for building web applications in plain, type-safe Java.
Routing, templates, forms, persistence and tests are all ordinary Java code, with no XML, YAML or reflection magic. The compiler and your IDE understand your whole application, giving you completion, refactoring, navigation and type checking everywhere.
The whole framework ships as a single 2.5MB jar with zero dependencies, is an explicit Java module, and runs on Java 17 and later, tested on Java 17, 21, 25 and 26.
TIP: If you use IntelliJ IDEA as your IDE, consider installing the RIFE2 IDEA Plug-in.
It will greatly enhance your coding experience.
This is a quick tutorial, the full documentation contains a lot more information.
The RIFE2 Javadocs complement the documentation with many more details.
See RIFE2 in action on rife2.com, where fifteen examples each run live on the page and link to their own source.
Why RIFE2?
A frequently asked question is: "Why choose RIFE2 over other popular frameworks"?
The short answer is that RIFE2 is different, it's designed to create web applications quickly with small teams. It has challenged and will always challenge the status-quo. It's not meant to replace enterprise-level frameworks like Spring or JEE, though I've used it for enterprise applications. RIFE2 leverages the power of the Java platform for web applications that you'd usually write with scripting languages. Productivity and maintainability is key, and you'll find that you get 90% of the work done for 10% of the effort, and can still integrate with other Java libraries and frameworks where you need it.
RIFE2 has features that after 20 years still can't be found elsewhere:
web continuations, bidirectional template engine, bean-centric metadata system,
full-stack without dependencies, metadata-driven SQL builders, content
management framework, full localization support, resource abstraction, persisted
cron-like scheduler, continuations-based workflow engine.
Most of these features have stood the test of time and after 20 years still prove to be great choices for web application development. RIFE2 has learned from decades of experience and improves on these original features in many ways.
RIFE2 also has features that have been adopted by others, but that usually lack the convenience of the tight integration throughout a full-stack.
For instance: out-of-container tests can analyze the structure of the resulting templates without having to parse HTML, the authentication system is built from all the other pieces of the full-stack and seamlessly integrates into your web application, URLs are generated from the configuration you created without the risk of becoming stale, the logic-less templates are really purely content driven and can generate any text-based format (JSON, XML, HTML, SVG, SQL), ... and much more.
The features that RIFE2 1.10 adds follow the same pattern. Server-sent events are bridged into the generic query manager and the workflow engine, so saving a bean anywhere in the application updates the list in every connected browser, and an event can carry a template that is resolved against each receiving connection. CSRF protection reuses the route tags that already generate your form inputs, so an existing form carries the token without a template change. There's htmx support in the web engine, declarative database migrations that can be reversed and previewed, JSON parsing and generation that converts beans and records, and ahead-of-time instrumentation, so continuations work without the java agent, also in a GraalVM native image.
Virtual threads by default
On Java 21 and later, RIFE2's embedded server runs every request on its own virtual thread, with no configuration. You write ordinary blocking code: a database query, an HTTP call or a file read parks only that thread instead of tying up a pooled request thread. There is no reactive API and no async plumbing to learn, and blocking code can scale to many concurrent I/O-bound requests because virtual threads are cheap to create and park. When deployed to an external servlet container, RIFE2 leaves request threading to the container. Either way, it fits the rest of RIFE2, which offers the same sequential model: continuations pause and resume a flow across requests, and the workflow engine coordinates concurrent tasks as straight-line code that waits on events.
Built for AI-assisted development
RIFE2's design suits AI coding assistants. Everything is plain, type-safe Java: no XML or YAML, no required annotations, and no reflection magic, so the compiler and the agent both read your application's structure straight from the source. Nothing is hidden: routes are fields, elements are classes, and templates are files, all discoverable by reading the code. The framework is a single 2.5MB jar with zero dependencies, built by bld in plain Java, so there is less machinery to reverse engineer, and bld can expose your build commands to agents over the Model Context Protocol. Out-of-container tests exercise the whole request cycle in memory for a fast compile-and-test loop, and an llms.txt at the rife2.com site root gives agents an index of the framework's documentation. The guide for coding agents explains how a RIFE2 application is put together and lists the pitfalls that are easy to miss.
RIFE2 is the red pill, ready to show you how deep the rabbit hole can go, if you're up for it!
Features
Web : engine with continuations, clean-URL path mapping, out-of-container testing, form building, dynamic validation, server-sent events with replay, htmx integration, customizable authentication, CSRF protection.
Runtime and deployment : embedded Jetty or Tomcat, virtual threads by default, Jakarta EE 10/11 war deployment, GraalVM native images with continuations, explicit Java module, hierarchical configuration, persisted scheduler, dependency injection, workflow engine with continuations.
Data : database query builders, generic query manager, declarative migrations, bean-centric metadata and constraints, JSON with bean conversion, content management.
Templates and content : bidirectional logic-less templates, localization, RSS, Atom and JSON feeds, WebJars, resource abstraction.
Tooling : pure Java build system (bld), IntelliJ IDEA plugin, Gradle plugin.
Quickstart
Create a project
Nothing needs to be installed, this one-liner scaffolds a ready-to-run RIFE2 application:
bash -c "$(curl -fsSL https://rife2.com/create.sh)"
Or on Windows:
irm https://rife2.com/create.ps1 | iex
Then cd into the new application and run it:
./bld compile run
Or on Windows:
bld.bat compile run
The generated project builds with bld, but that's only one option: RIFE2 projects can just as well be built with Gradle or Maven.
Hello World Example
This is how you get started with a Hello World site.
public class HelloWorld extends Site {
public void setup() {
get("/hello", c -> c.print("Hello World"));
}
public static void main(String[] args) {
new Server().start(new HelloWorld());
}
}
The main method spins up the integrated embedded Jetty server, so that you can
immediately start coding. The same HelloWorld class can be added as a
parameter value to your web.xml, requiring absolutely no changes to your code
between development and production.
Out-of-container testing is a first-class citizen in RIFE2, directly interacting
with your Site class to simulate full request-response interactions,
without having to spin up a servlet container.
This is how you could test the example above with JUnit:
class HelloTest {
@Test void verifyHelloWorld() {
var m = new MockConversation(new HelloWorld());
assertEquals("Hello World", m.doRequest("/hello").getText());
}
}
Type-safe Links and URLs
One of the most brittle aspects of web application development is typing links and URLs as text literals, without anything guaranteeing they remain correct when your routes change or when you deploy your application in different web application contexts. RIFE2's routing API allows all your application links to be generated correctly without any effort on your behalf.
Let's add a new route that contains an HTML link towards the previous Hello World route.
You can see that routes don't have to be created inside the setup() method,
but can also be created as part of your Site's construction, allowing the
routes to be stored in fields.
public class HelloLink extends Site {
Route hello = get("/hello", c -> c.print("Hello World"));
Route link = get("/link", c-> c.print("<a href='" + c.urlFor(hello) + "'>Hello</a>"));
public static void main(String[] args) {
new Server().start(new HelloLink());
}
}
We can now test this as such:
class HelloTest {
@Test void verifyHelloLink() {
var m = new MockConversation(new HelloLink());
assertEquals("Hello World", m.doRequest("/link")
.getParsedHtml().getLinkWithText("Hello")
.follow().getText());
}
}
Bidirectional Logic-Less Templates
The main impetus that had me create RIFE2, was RIFE's unique template engine.
RIFE2's templates contain two main concepts:
- values - that can be filled in with content and data
- blocks - that will be stripped away and that provide content snippets
Your Java code will compose the final layout by assigning and appending blocks,
and by putting data into values. Let's rewrite the HelloLink example above with a template.
In this example, no template manipulation is done in Java yet.
Instead, it introduces the {{v route:hello/}} value tag, which will
automatically be replaced with the URL of the route that is available with that
field name in your active Site.
public class HelloTemplate extends Site {
Route hello = get("/hello", c -> c.print("Hello World"));
Route link = get("/link", c-> c.print(c.template("HelloTemplate")));
public static void main(String[] args) {
new Server().start(new HelloTemplate());
}
}
With HelloTemplate.html being:
<!DOCTYPE html>
<html lang="en">
<body>
<a href="{{v route:hello/}}">Hello</a>
</body>
</html>
Note that RIFE2 internally transforms your templates into Java classes by generating optimized bytecode.
This happens on-the-fly during development. For production, templates can be pre-compiled, making them incredibly fast.
Template Manipulation
Let's change the example some more and create a single route that can respond to
both get and post requests.
- the
getrequest will display a form with a single button to click. - the
postrequest will receive the form's submission and displayHello World.
public class HelloForm extends Site {
Route hello = route("/hello", c -> {
var t = c.template("HelloForm");
switch (c.method()) {
case GET -> t.setBlock("content", "form");
case POST -> t.setBlock("content", "text");
}
c.print(t);
});
public static void main(String[] args) {
new Server().start(new HelloForm());
}
}
With HelloForm.html being:
<!DOCTYPE html>
<html lang="en">
<body>
<!--v content/-->
<!--b form-->
<form action="{{v route:action:hello/}}" method="post" name="hello">
<!--v route:inputs:hello/-->
<input type="submit" name="Submit">
</form>
<!--/b-->
<!--b text--><p id="greeting">Hello World</p><!--/b-->
</body>
</html>
NOTE: that the
route:value tag from the above has been split intoroute:action:androute:inputs:, generating hidden HTML form inputs for parameters instead of query string parameters.
You can see that the template contains all the pieces to create both pages:
- the value named
content - the block named
form - the block named
text
In Java, we simply assign either block to the value, depending on what we want to display.
Another benefit is that RIFE2's template tags can be HTML comments, making them completely invisible. This allows you to work on your HTML design as usual and preview the template file with a regular browser.
Finally, let's include a test for this functionality:
class HelloTest {
@Test void verifyHelloForm() {
var m = new MockConversation(new HelloForm());
var r = m.doRequest("/hello").getParsedHtml()
.getFormWithName("hello").submit();
assertEquals("Hello World", r.getParsedHtml()
.getDocument().body()
.getElementById("greeting").text());
}
}
Just the top of the rabbit hole
Thanks for reading until the end!
This was merely a quick introduction to whet your appetite, RIFE2 comes with a comprehensive and easy to read manual with many examples and pragmatic explanations. You can also see RIFE2 in action on rife2.com, where each live example links to its own source.
If you have any questions, suggestions, ideas or just want to chat, feel free to post on the forums or to join us on Discord.
Read more in the full documentation and RIFE2 Javadocs.
About RIFE2
RIFE2 builds on the foundations of the original RIFE framework that was popular from 2002 to 2010. It preserves most of the original features and adds new ones, for a fraction of the footprint and with even greater developer productivity. RIFE2 is created and maintained by Geert Bevin, veteran Java coder and one of the first Java Champions.
探索相关项目
加载中…