"); return "
" + html + ""; } } ``` ```java // Usage in controller: @Path("/docs") @Controller public class DocumentController { @Inject private Models models; @GET @Path("/readme") public String showReadme() { models.put("title", "Getting Started"); models.put("username", "Developer"); models.put("content", "This is the documentation."); return "readme.md"; // Will be processed by CustomMarkdownViewEngine } } ``` -------------------------------- ### Untitled No description -------------------------------- ### Define MVC Controllers with @Controller Annotation in Java Source: https://context7.com/jakartaee/mvc/llms.txt Marks JAX-RS resource methods or classes as MVC controllers. Enables returning view paths and accessing MVC features. This snippet demonstrates listing products and showing product details, injecting Models for data transfer. ```java import jakarta.mvc.Controller; import jakarta.mvc.Models; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.QueryParam; import jakarta.inject.Inject; @Path("/products") public class ProductController { @Inject private Models models; @GET @Path("/list") @Controller public String listProducts() { return "products/list.jsp"; } @GET @Path("/details") @Controller public String getProductDetails(@QueryParam("id") Long id) { // Add data to model models.put("productId", id); models.put("product", findProductById(id)); return "products/details.jsp"; } private Product findProductById(Long id) { // Database lookup logic return new Product(id, "Sample Product", 29.99); } static class Product { private Long id; private String name; private double price; public Product(Long id, String name, double price) { this.id = id; this.name = name; this.price = price; } // Getters omitted for brevity } } ``` -------------------------------- ### Using Models Interface to Pass Data from Controller to View in Java Source: https://context7.com/jakartaee/mvc/llms.txt Demonstrates how to use the `Models` interface in a Jakarta MVC controller to add data (simple values, complex objects, typed data) for consumption by view engines. It also shows how to retrieve data in a type-safe and non-typed manner, and access all data as a map. ```java import jakarta.mvc.Controller; import jakarta.mvc.Models; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.inject.Inject; import java.util.List; import java.util.ArrayList; @Path("/dashboard") @Controller public class DashboardController { @Inject private Models models; @GET public String showDashboard() { // Add simple values models.put("title", "Dashboard") .put("userName", "John Doe") .put("loginCount", 42); // Add complex objects List