Skip to content

Actions

yassinezerguine08 edited this page Oct 23, 2026 · 1 revision

Actions

An action is something the page can ask the Java side to do. It is a public method annotated with @Action, and the page reaches it by name at /action/<name>. The examples on this page come from Oriel's own ActionHandlerTest, which binds a small Ledger that can be locked and unlocked:

public class Ledger {
	private final List<String> unlocked = new ArrayList<>();

	@Action("lock-ledger")
	public void onClickLockLedger() {
		unlocked.clear();
	}

	@Action("unlock-ledger")
	public Status onClickUnlockLedger(final Form form) {
		unlocked.add(form.get("passphrase"));
		return new Status(form.get("passphrase"));
	}
}

The page asks for them with htmx:

<button type="button" hx-post="/action/lock-ledger" hx-swap="none">Lock</button>

<form hx-post="/action/unlock-ledger" hx-target="#ledger-status" hx-swap="outerHTML">
	<input type="password" name="passphrase">
	<button type="submit">Unlock</button>
</form>

What a method can take

  • Nothing, like lock-ledger.
  • A Form, like unlock-ledger. It holds the values the request carried, decoded from application/x-www-form-urlencoded.

Form.get(name) returns the value, or null when the field was not sent. Form.has(name) is true when the field was sent and is not empty. A field sent twice keeps its last value, and a pair without = or without a name is ignored.

Values usually come from the form's fields, or from hx-vals:

<button type="button" hx-post="/action/unlock-ledger" hx-vals='{"passphrase": "open sesame!"}'
	hx-target="#ledger-status" hx-swap="outerHTML">Unlock</button>

What a method can return, and what the page gets back

The method… The page gets In ActionHandlerTest
returns a View 200 with the view rendered as HTML, for htmx to swap in unlock-ledger
returns nothing (void) or null 204, which htmx takes as leaving the page as it is lock-ledger
throws 500; the reason is logged at error level and the page only learns that something failed break-ledger
returns something that is not a view 500, the same way misreport-ledger
does not exist 404: an unknown action is refused rather than answered with the whole page shred-ledger
is asked for by a request without the session 403; see The server and its security any of them, asked anonymously

Returning nothing suits an action whose effect does not show, or one that finds nothing has changed.

Expected failures and unexpected ones

Throwing is for what nobody expected. An everyday mistake, such as a file in the wrong format, should be told to the person in a view instead:

@Action("open")
public NoteView onOpenChosen(final Form form) {
	final Path file = Path.of(form.get("path"));

	try {
		notes.load(file);
	} catch (IOException e) {
		log.warn("{} could not be opened: {}", file, e.getMessage());
		return new NoteView("Could not open " + file, true);
	}

	return new NoteView("Opened " + file, false);
}

For a 500, /oriel/failure.js puts a short notice in the page, so that a failed click does not look like an ignored one:

  • The text comes from data-failure-message on <body>. Say there where the person can find the log.
  • The notice goes into the element marked data-failure-host; of several, the last one on the page. Without any, it goes at the end of <body>.
  • The notice is marked data-failure, so the application styles it with [data-failure] { … }.

Names must be unique

Two methods claiming the same name make Oriel refuse to start, with an IllegalStateException naming both methods. Which one the page reached would otherwise depend on the order they happened to be found in. ActionsTest shows it with two classes that both claim lock.

With CDI or without

Without a container, bind the actions of the objects you hand over:

final Actions actions = Actions.of(new Ledger());

With CDI, Oriel finds every @Action while the container starts and calls it on the bean the container manages:

final SeContainer container = SeContainerInitializer.newInstance().initialize();
final Actions actions = Actions.discovered(container.getBeanManager().getExtension(ActionExtension.class));

For that:

  • Add a CDI implementation, such as Weld SE (org.jboss.weld.se:weld-se-core).
  • Give the application its own src/main/resources/META-INF/beans.xml.
  • Make the controllers beans, for example @ApplicationScoped.

oriel-server registers its ActionExtension with the container by itself, through META-INF/services.

Either way, the result goes to Routes.create(…); see Getting started.

Clone this wiki locally