Path variables

An introduction to path variables in a Medusa context. The values of the path variable used here can be changed to other values, if you want to experiment.

Back to overview

Path variable on page load

Use ServerRequest in the setupAttributes() method to gain access to the entire incoming request metadata, including path variables.

someValue123 anotherValue432
JDK22.0.2 w/ Medusa 0.9.6-SNAPSHOT

Server

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;
import org.springframework.web.reactive.function.server.ServerRequest;

import static io.getmedusa.medusa.core.attributes.Attribute.$$;

@UIEventPage(path = "/detail/path/{sample1}/{sample2}", file = "/pages/path.html")
public class PathVariableController {

    public List<Attribute> setupAttributes(ServerRequest request) {
        return $$("sessionVar1", request.pathVariable("sample1"),
                  "sessionVar2", request.pathVariable("sample2"));
    }

}

Path variable on actions

From that point on, you could add them to the attributes so that they are part of the Session (without the need to render them visually). You can then use them anywhere, as you can always use the session. The Session object is always optionally available for any action method, if you add it to the beginning or end of your parameters: myMethod(Session session, ... ) or myMethod(..., Session session)

JDK22.0.2 w/ Medusa 0.9.6-SNAPSHOT

Client

<span th:text="${actionOutput}"></span>

<button m:click="runAction()">Get path variables on action from session</button>

Server

import io.getmedusa.medusa.core.annotation.UIEventPage;
import io.getmedusa.medusa.core.attributes.Attribute;
import io.getmedusa.medusa.core.session.Session;
import org.springframework.web.reactive.function.server.ServerRequest;

import static io.getmedusa.medusa.core.attributes.Attribute.$$;

@UIEventPage(path = "/detail/path/{sample1}/{sample2}", file = "/pages/sample/path.html")
public class PathVariableController {

    public List<Attribute> setupAttributes(ServerRequest request) {
        return $$(
                "sessionVar1", request.pathVariable("sample1"),
                "sessionVar2", request.pathVariable("sample2")
        );
    }

    public List<Attribute> runAction(Session session) {
        return $$("actionOutput",
                session.getAttribute("sessionVar1")
                + "/" +
                session.getAttribute("sessionVar2"));
    }

}