/
Flexible Method Signatures for UI Framework Controller and Action Methods

Flexible Method Signatures for UI Framework Controller and Action Methods

The UI framework has conventions for page and fragment controller classes and allows you to give flexible method signatures for page controller, fragment controller, and fragment action methods.

Page Controller method

  • page controller classes must be in the org.openmrs.module.yourmodule.page.controller package

  • the class name must be the page name (with its first letter uppercased) + "PageController"

  • depending on the request method, a controller methods named get(), post(), etc will be called. If the method doesn't exist and a controller() method does, that will be called (for all request methods)

  • for example the controller method for the "editEncounter" page is org.openmrs.module.uiexample.page.controller.EditEncounterPageController.controller()

  • in rare cases you may want to override these settings by manually configuring your controller and view providers instead of using the standard setup.

Parameters

Method parameters will be auto-set by type for these classes:

  • org.openmrs.ui.framework.page.PageContext

  • org.openmrs.ui.framework.page.PageModel (this is preferred for clarity, although Model and Map have the same effect)

  • org.openmrs.ui.framework.Model (this will be a PageModel)

  • java.util.Map (this will be a PageModel)

  • org.openmrs.ui.framework.page.PageRequest

  • javax.servlet.http.HttpServletRequest

  • javax.servlet.http.HttpServletResponse

  • javax.servlet.http.HttpSession

  • org.openmrs.ui.framework.session.Session

  • org.springframework.context.ApplicationContext

  • org.openmrs.ui.framework.UiUtils

@RequestParam annotation

You may put the @org.springframework.web.bind.annotation.RequestParam annotation (from Spring MVC) on any parameter and the UI Framework will take the specified parameter from the HTTP request, and use Spring's ConversionService to convert it to the specified argument type. For example:

public void controller(@RequestParam("patientId") Patient patient) { ... }

@MethodParam annotation

The @org.openmrs.ui.framework.annotation.MethodParam annotation allows you to encapsulate complex logic in a method (similar to Spring's @ModelAttribute). The value of the MethodParam annotation should be the method name to call, and that method may have a flexible signature in the same way as the controller. For example:

public void controller(@MethodParam("buildPatient") Patient patient) { ... } public Patient buildPatient(@RequestParam ...) { ... ; return patient }

@CookieValue annotation

The @org.springframework.web.bind.annotation.CookieValue annotation will look for a value in a cookie in the HttpServletRequest whose name is the value of the annotation, and use Spring's type conversion to convert it to the specified argument type. For example: