> For the complete documentation index, see [llms.txt](https://admincode.gitbook.io/admincode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://admincode.gitbook.io/admincode/basics/newpage/step-5-creating-the-controllers-summary-and-detail.md).

# Step 5: Creating the Controllers \[Summary and Detail]

The controller layer in a Java application acts as the entry point for handling incoming requests and coordinating the flow of data between the client (such as a web browser or API consumer) and the service layer. It receives requests, processes any necessary validation, and calls the appropriate methods in the service layer to execute business logic. Once the service layer completes its operations, the controller receives the result, which is then formatted and returned to the client, typically as JSON, XML, or another suitable format.&#x20;

By acting as a mediator, the controller ensures that the service layer remains decoupled from the external communication and keeps the application's architecture organized. It is also responsible for handling exceptions, managing response statuses, and facilitating security-related tasks like authentication and authorization. This separation of concerns helps maintain a clean, maintainable codebase and improves the scalability of the application.

The **Summary page** of an MVC application typically provides an overview of the system's state, displaying high-level data or status updates from the model in a concise and user-friendly format. It may include lists, summaries, or dashboards that give the user a quick understanding of key information.&#x20;

On the other hand, the **Detail page** focuses on presenting more granular, in-depth information. It allows users to interact with or explore specific data items, such as viewing the full details of a particular record, editing, or performing actions related to that data. Both pages rely on the controller to handle user interactions, ensuring smooth data flow between the model and the view while maintaining a clear, organized user experience.

**Detail Controller**

```java
package com.rce.controller.page;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.rce.uicomponent.core.Column;
import com.rce.uicomponent.core.Row;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.rce.controller.core.Form;
import com.rce.controller.core.FormParam;
import com.rce.controller.core.PageController;
import com.rce.controller.core.PageState;
import com.rce.dto.ArticleCommentDTO;
import com.rce.dto.ArticleDTO;
import com.rce.dto.AuthorDTO;
import com.rce.dto.BaseDTO;
import com.rce.dto.CitationDTO;
import com.rce.model.ArticleTypeEnum;
import com.rce.uicomponent.core.DropDown;
import com.rce.uicomponent.core.DropDownOption;
import com.rce.uicomponent.core.Input;
import com.rce.uicomponent.core.Table;
import com.rce.uicomponent.core.UIComponent;

import lombok.extern.slf4j.Slf4j;

import com.rce.uicomponent.UIInputType;
import com.rce.uicomponent.UILayoutForm.ComponentAlignment;
import com.rce.uicomponent.UILayoutForm.ComponentMode;
import com.rce.uicomponent.core.LayoutForm;

@Slf4j
@Controller
@RequestMapping("article")
public class ArticleController extends PageController {

	@PostMapping("/{id}")
	@Override
	protected ModelAndView postPage(ModelMap model, @PathVariable Long id) throws Exception {       	
		return pageLoad(id, PageState.Read, model);
	}

	@GetMapping("/{id}") 
	@Override
	protected ModelAndView getPage(ModelMap model, @PathVariable Long id) throws Exception {  
		log.info("entering article ...");
		return pageLoad(id, PageState.Read, model);
	}

	@PostMapping("/edit/{id}")
	@Override
	protected ModelAndView postPageEdit(ModelMap model, @PathVariable Long id) throws Exception {       	
		return pageLoad(id, PageState.Edit, model);
	}

	@GetMapping("/edit/{id}")    
	@Override
	protected ModelAndView getPageEdit(ModelMap model, @PathVariable Long id) throws Exception {       
		return pageLoad(id, PageState.Edit, model);
	}

	@PostMapping("/new")
	@Override
	protected ModelAndView postPageNew(ModelMap model) throws Exception {       	
		return pageLoad(0L, PageState.New, model);
	}

	@GetMapping("/new")
	@Override
	protected ModelAndView getPageNew(ModelMap model) throws Exception {       	
		return pageLoad(0L, PageState.New, model);
	}

	public ModelAndView pageLoad(Long id, PageState state, ModelMap model) throws Exception {
		return super.pageLoad(id, ArticleDTO.class, state, model, "/pages/article");
	}
	
    public LayoutForm getLayoutForm(Object o) throws Exception {
    	LayoutForm layoutForm = new LayoutForm();
    	
    	ArticleDTO article = (ArticleDTO)o;
		Input articleId = UIInput.draw("Id", article.getId(), UIInputType.Text);
		Input articleTitle = UIInput.draw("title", article.getTitle(), UIInputType.Text);
		Input articleCategory = UIInput.draw("category", article.getCategory(), UIInputType.Text);
		Input articleSummary = UIInput.draw("summary", article.getSummary(), UIInputType.TextArea);	
		
		Table author = Table.Builder.newInstance()
				.setTableDescription("author")
				.setName("authors")
				.oneToOneBuilder()
					.setIsOneToOne(true)	
					.setDisplayValue(article.getAuthor() != null ? article.getAuthor().getName() : "")
					.setValueType("name")
					.done()
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonIsSingleSelectTable(true)
					.setAddButtonUILayoutTitle("Select Author")
					.done()
				.setDatalistType(AuthorDTO.class)
				.setTableId("author")
				.build();
		author = UIDataTable.draw(author);
		
		List<DropDownOption> types = new ArrayList<>();
		DropDownOption type = new DropDownOption();
		type.setDescription("Politics");
		type.setValue(ArticleTypeEnum.POLITICS.name());
		types.add(type);
		type = new DropDownOption();
		type.setDescription("Civil");
		type.setValue(ArticleTypeEnum.CIVIL.name());
		types.add(type);
		type = new DropDownOption();
		type.setDescription("Economy");
		type.setValue(ArticleTypeEnum.ECONOMY.name());
		types.add(type);
		DropDown articleType = UIDropDown.draw("type", types, 
				article.getType() != null ? 
				article.getType().name() : "");	
		
		// Multi dropdown with lookup table and DTO
		DropDown articleTypes = UIMultiDropDown.draw("types", 
				article.getTypes() != null ? 
						article.getTypes().stream()
						.map(v -> v.getType().name())
						.collect(Collectors.toList())
						: List.of(), "type", types);
		
		// Multi dropdown with plain List<String>
		List<DropDownOption> typeStringList = new ArrayList<>();
		type = new DropDownOption();
		type.setDescription("Politics");
		type.setValue(ArticleTypeEnum.POLITICS.name());
		typeStringList.add(type);
		type = new DropDownOption();
		type.setDescription("Civil");
		type.setValue(ArticleTypeEnum.CIVIL.name());
		typeStringList.add(type);

		Table citations = Table.Builder.newInstance()
				.setTableDescription("citations")
				.setName("citations")
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonIsSingleSelectTable(false)
					.setAddButtonUILayoutTitle("Select Citations")
					.done()
				.setDatalist(article.getCitations())
				.setDatalistType(CitationDTO.class)
				.setTableId("citations")
				.build();
		citations = UIDataTable.draw(citations);

		Table comments = Table.Builder.newInstance()
				.setTableDescription("comments")
				.setName("comments")
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonActionUrl("article/confirmAddNewComment")
					.setAddButtonGetUILayoutUrl("article/getAddNewCommentLayout")
					.setAddButtonUILayoutTitle("New Comment")
					.done()
				.setDatalist(article.getComments())
				.setDatalistType(ArticleCommentDTO.class)
				.setTableId("comments")
				.build();
		comments = UIDataTable.draw(comments);

		Map<String, UIComponent> fieldsRow1Col1 = new LinkedHashMap<String, UIComponent>();
		fieldsRow1Col1.put(articleId.getName(), articleId);
		fieldsRow1Col1.put(articleTitle.getName(), articleTitle);
		fieldsRow1Col1.put(articleCategory.getName(), articleCategory);
		fieldsRow1Col1.put(articleSummary.getName(), articleSummary);

		
		Map<String, UIComponent> fieldsRow1Col2 = new LinkedHashMap<String, UIComponent>();
		fieldsRow1Col2.put(articleType.getName(), articleType);
		fieldsRow1Col2.put(articleTypes.getName(), articleTypes);
		fieldsRow1Col2.put("author", author);
	
		Map<String, UIComponent> fieldsRow2Col1 = new LinkedHashMap<String, UIComponent>();
		fieldsRow2Col1.put("citations", citations);
		fieldsRow2Col1.put("comments", comments);
		
		List<Row> rows = layoutForm.getRows();
		Column col1 = new Column(fieldsRow1Col1);
		Column col2 = new Column(fieldsRow1Col2);
		Row row1 = new Row();
		row1.setColumns(List.of(col1, col2));
		rows.add(row1);
		Column col3 = new Column(fieldsRow2Col1);
		Row row2 = new Row();
		row2.setColumns(List.of(col3));
		rows.add(row2);
		layoutForm.setRows(rows);
		
    	return layoutForm;	
	}

	@Override
	public Map<String, UIComponent> getFields(Object o) throws Exception {
		ArticleDTO article = (ArticleDTO)o;
		Input articleId = UIInput.draw("Id", article.getId(), UIInputType.Text);
		Input articleTitle = UIInput.draw("title", article.getTitle(), UIInputType.Text);
		Input articleCategory = UIInput.draw("category", article.getCategory(), UIInputType.Text);
		Input articleSummary = UIInput.draw("summary", article.getSummary(), UIInputType.TextArea);	
		
		Table author = Table.Builder.newInstance()
				.setTableDescription("author")
				.setName("authors")
				.oneToOneBuilder()
					.setIsOneToOne(true)	
					.setDisplayValue(article.getAuthor() != null ? article.getAuthor().getName() : "")
					.setValueType("name")
					.done()
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonIsSingleSelectTable(true)
					.setAddButtonUILayoutTitle("Select Author")
					.done()
				.setDatalistType(AuthorDTO.class)
				.setTableId("author")
				.build();
		author = UIDataTable.draw(author);
		
		List<DropDownOption> types = new ArrayList<>();
		DropDownOption type = new DropDownOption();
		type.setDescription("Politics");
		type.setValue(ArticleTypeEnum.POLITICS.name());
		types.add(type);
		type = new DropDownOption();
		type.setDescription("Civil");
		type.setValue(ArticleTypeEnum.CIVIL.name());
		types.add(type);
		type = new DropDownOption();
		type.setDescription("Economy");
		type.setValue(ArticleTypeEnum.ECONOMY.name());
		types.add(type);
		DropDown articleType = UIDropDown.draw("type", types, 
				article.getType() != null ? 
				article.getType().name() : "");	
		
		// Multi dropdown with lookup table and DTO
		DropDown articleTypes = UIMultiDropDown.draw("types", 
				article.getTypes() != null ? 
						article.getTypes().stream()
						.map(v -> v.getType().name())
						.collect(Collectors.toList())
						: List.of(), "type", types);
		
		// Multi dropdown with plain List<String>
		List<DropDownOption> typeStringList = new ArrayList<>();
		type = new DropDownOption();
		type.setDescription("Politics");
		type.setValue(ArticleTypeEnum.POLITICS.name());
		typeStringList.add(type);
		type = new DropDownOption();
		type.setDescription("Civil");
		type.setValue(ArticleTypeEnum.CIVIL.name());
		typeStringList.add(type);

		Table citations = Table.Builder.newInstance()
				.setTableDescription("citations")
				.setName("citations")
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonIsSingleSelectTable(false)
					.setAddButtonUILayoutTitle("Select Citations")
					.done()
				.setDatalist(article.getCitations())
				.setDatalistType(CitationDTO.class)
				.setTableId("citations")
				.build();
		citations = UIDataTable.draw(citations);

		Table comments = Table.Builder.newInstance()
				.setTableDescription("comments")
				.setName("comments")
				.setIsEditable(true)
				.addEditBehavior()
					.setAddButtonActionUrl("article/confirmAddNewComment")
					.setAddButtonGetUILayoutUrl("article/getAddNewCommentLayout")
					.setAddButtonUILayoutTitle("New Comment")
					.done()
				.setDatalist(article.getComments())
				.setDatalistType(ArticleCommentDTO.class)
				.setTableId("comments")
				.build();
		comments = UIDataTable.draw(comments);

		Map<String, UIComponent> fields = new LinkedHashMap<String, UIComponent>();
		fields.put(articleId.getName(), articleId);
		fields.put(articleTitle.getName(), articleTitle);
		fields.put(articleCategory.getName(), articleCategory);
		fields.put(articleSummary.getName(), articleSummary);
		fields.put(articleType.getName(), articleType);
		fields.put(articleTypes.getName(), articleTypes);
		fields.put("author", author);
		fields.put("citations", citations);
		fields.put("comments", comments);
	
		return fields;
	}

	@RequestMapping(value="/getAddNewCommentLayout", produces = MediaType.TEXT_HTML_VALUE)
	@ResponseBody
	protected String getAddNewCommentLayout(@RequestParam Map<String,String> params) throws Exception {   	
		Input commentTextBox = UIInput.draw("comment", "", UIInputType.Text);
		List<UIComponent> commentAddButtonFields = new ArrayList<UIComponent>();
		commentAddButtonFields.add(commentTextBox);
		LayoutForm addCommentLayout = UILayoutForm.draw(commentAddButtonFields, ComponentMode.EDIT, ComponentAlignment.VERTICAL);
		
		return addCommentLayout.getHtml();
	}

	@RequestMapping(value="/confirmAddNewComment", produces = MediaType.TEXT_HTML_VALUE)
	@ResponseBody
	protected String addNewComment(@RequestParam Map<String,String> params, @FormParam Form form) throws Exception {   	
		
		String commentValue = params.get("comment");

		Table table = getTableFromMemoryByParams(params);

		BaseDTO dirtyDTO = form.getDirtyDto();

		ArticleDTO article = (ArticleDTO)dirtyDTO;
		if (article.getComments() == null) {
			article.setComments(new ArrayList<ArticleCommentDTO>());
		}

		ArticleCommentDTO newComment = new ArticleCommentDTO();
		newComment.setComment(commentValue);
		article.getComments().add(newComment);

		article.setComments(article.getComments());

		table.setDatalist(article.getComments());
		
		form.setDirtyDto(dirtyDTO);
		DataHolder.addDataToMap(form.getHash(), form);
		
		return redrawTableAndSave(table);
	}

	@PostMapping("/update")
	protected ModelAndView saveUpdate(@FormParam Form form, ModelMap model) throws Exception { 
		ArticleDTO articleDTO = (ArticleDTO) form.getDto();
		ArticleDTO dirtyArticle = (ArticleDTO) form.getDirtyDto();
		articleDTO.setTitle(dirtyArticle.getTitle());
		articleDTO.setAuthor(dirtyArticle.getAuthor());
		articleDTO.setSummary(dirtyArticle.getSummary());
		articleDTO.setCategory(dirtyArticle.getCategory());
		articleDTO.setCitations(dirtyArticle.getCitations());		
		articleDTO.setComments(dirtyArticle.getComments());
		if (dirtyArticle.getType() != null) {
			articleDTO.setType(dirtyArticle.getType());
		}
		if (dirtyArticle.getTypes() != null && !dirtyArticle.getTypes().isEmpty()) {
			articleDTO.setTypes(dirtyArticle.getTypes());
		}

		return super.saveUpdateEntity(articleDTO, model, "article");
	}

	@PostMapping("/create")
	protected ModelAndView saveNew(@FormParam Form form, ModelMap model) throws Exception { 
		ArticleDTO articleDTO = (ArticleDTO) form.getDto();
		ArticleDTO dirtyArticle = (ArticleDTO) form.getDirtyDto();
		articleDTO.setTitle(dirtyArticle.getTitle());
		articleDTO.setAuthor(dirtyArticle.getAuthor());
		articleDTO.setSummary(dirtyArticle.getSummary());
		articleDTO.setCategory(dirtyArticle.getCategory());
		articleDTO.setCitations(dirtyArticle.getCitations());
		articleDTO.setComments(dirtyArticle.getComments());
		articleDTO.setComments(dirtyArticle.getComments());
		articleDTO.setType(dirtyArticle.getType());
		articleDTO.setTypes(dirtyArticle.getTypes());

		return super.saveNewEntity(articleDTO, model, "article");
	}

	@PostMapping("/delete")
	protected ModelAndView delete(@FormParam Form form, ModelMap model) throws Exception {   	
		ArticleDTO articleDTO = (ArticleDTO) form.getDto();
		return super.deleteEntity(articleDTO, model, "articles");
	}

}
```

**Summary Controller**

```java
package com.rce.controller.summary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.*;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rce.controller.core.SummaryController;
import com.rce.dto.ArticleDTO;
import com.rce.uicomponent.core.Input;
import com.rce.uicomponent.UIInputType;
import com.rce.uicomponent.core.Action;
import com.rce.uicomponent.core.ColumnDefinition;
import com.rce.uicomponent.core.ColumnDefinitionType;
import com.rce.uicomponent.core.DbFieldType;
import com.rce.uicomponent.core.Table;

@Controller
public class ArticlesController extends SummaryController {

	@RequestMapping(value="/articles",method=RequestMethod.POST)
	public ModelAndView getPageForm(@RequestParam Map<String,String> searchFilterParams, ModelMap model) throws Exception {       	
		return pageLoad(searchFilterParams, model);
	}

	@RequestMapping(value="/articles",method=RequestMethod.GET)    
	public ModelAndView getPageLoad(@RequestParam Map<String,String> searchFilterParams, ModelMap model) throws Exception {       
		return pageLoad(searchFilterParams, model);
	}

	@Override
	protected ModelAndView getPageForm(ModelMap model, String processId) throws Exception {
		return null;
	}

	public ModelAndView pageLoad(@RequestParam Map<String,String> searchFilterParams, ModelMap model) throws Exception {
		List<Action> actionList = new ArrayList<Action>();
		Action action = new Action();
		action.setAction("executeProcess('article/new')");
		action.setDescription("New Article");
		action.setExecuteFromJavascript(true);
		actionList.add(action);
		return super.pageLoad(searchFilterParams, model, "Search Articles", "articles/search", actionList, 
				"/summary/articles", null);
	}

	@Override
	protected Table search(Map<String,String> searchParams) throws Exception {

		String sqlQuery = "select articles.Id, "
				+ "   title, category, "
				+ "   authors.name as author, "
				+ "   users.username as createdbyuser,"
				+ "   users.id as userId"
				+ " from articles "
				+ " inner join authors on authors.id=articles.author"
				+ " inner join users on users.id=articles.createdby"
				+ " where articles.isdeleted = 0 ";

		Map<String, ColumnDefinition> columnDefinitions = new HashMap<String, ColumnDefinition>();
		ColumnDefinition articleIdDef = new ColumnDefinition();
		articleIdDef.setType(ColumnDefinitionType.CLICKABLE_LINK);
		articleIdDef.setColumnDbName("Id");
		articleIdDef.setActionLink("index?page=article/?id");
		columnDefinitions.put(articleIdDef.getColumnDbName(), articleIdDef);

		Table table = Table.Builder.newInstance()
				.setTableId("articlesTable")
				.setName("articlesTable")
				.setSqlQuery(sqlQuery)
				.setColumnDefinitions(columnDefinitions)
				.setInputFilters(getInputFilters())
				.build();

		table = UITable.draw(table, searchParams);
		return table;
	}

	@Override
	protected Map<String,Input> getInputFilters() {
		Input articleId = UIInput.draw("Id", UIInputType.Text);
		articleId.setDbField("articles.id");
		articleId.setDbPrivateKey(true);
		Input articleTitle = UIInput.draw("articleTitle", UIInputType.Text);
		articleTitle.setDbField("articles.title");
		articleTitle.setDbFieldType(DbFieldType.Text);
		Input createdDateFrom = UIInput.draw("createdDate", UIInputType.DateTime);
		createdDateFrom.setDbField("articles.createdDate");

		Map<String,Input> inputFilters = new LinkedHashMap<String,Input>();
		inputFilters.put(articleId.getName(), articleId);
		inputFilters.put(articleTitle.getName(), articleTitle);
		inputFilters.put(createdDateFrom.getName(), createdDateFrom);
		return inputFilters;
	}

	@RequestMapping(value = "/articles/search", produces = MediaType.TEXT_HTML_VALUE)
	@ResponseBody
	public String searchControl(HttpSession session, @RequestParam Map<String,String> allParams) throws Exception {
		return super.searchControl(session, allParams, ArticleDTO.class);
	}

	@Override
	protected Table search(String id) throws Exception {
		return null;
	}
}
```

###
