Query params

An introduction to url parameters in a Medusa context. The values of the query parameter used here can be changed to other values, if you want to experiment. You can already remove them.

Back to overview

Query param on page load

Use ServerRequest in the setupAttributes() method to gain access to the entire incoming request metadata, including query parameters. Keep in mind that these are Optionals, so you should return a fallback value in case no value is provided via .else().

123someValue 543anotherValue
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/query", file = "/pages/query-param.html")
public class QueryParamController {

    public List<Attribute> setupAttributes(ServerRequest request) {
        return $$("sessionVar1", request.queryParam("sample1").orElse("No 'sample1' param provided?"),
                  "sessionVar2", request.queryParam("sample2").orElse("No 'sample2' param provided?"));
    }

}

Query param on actions

You could also add these 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/query", file = "/pages/query-param.html")
public class QueryParamController {

    public List<Attribute> setupAttributes(ServerRequest request) {
        return $$(
                "sessionVar1", request.queryParam("sample1").orElse("No 'sample1' param provided?"),
                "sessionVar2", request.queryParam("sample2").orElse("No 'sample2' param provided?"),
        );
    }

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

}