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 overviewUse ServerRequest in the setupAttributes() method to gain access to the entire incoming request metadata, including path variables.
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"));
}
}
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)
<span th:text="${actionOutput}"></span>
<button m:click="runAction()">Get path variables on action from session</button>
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"));
}
}