Creating custom models and controllers

Once you have the basic Java setup prepared, you can start creating models, controllers, and add any custom logic. You can create a subclass of any of the existing models, or create a new model.

About this task

Each model should extend the AbstractEntityModel, PageModel, or RegionModel.

Procedure

Edit module package section of your spring-context file:
  • Create a custom Entity Model (example):
    package com.sdl.dxa.modules.test.model;
    
    import com.sdl.webapp.common.api.mapping.annotations.SemanticEntity;
    import com.sdl.webapp.common.api.mapping.annotations.SemanticProperty;
    import com.sdl.webapp.common.api.model.entity.AbstractEntityModel;
    
    import static com.sdl.webapp.common.api.mapping.config.SemanticVocabulary.SDL_CORE;
    
    /**
     * Custom Model – The Component uses schema ‘CustomModel’
     */
    @SemanticEntity(entityName = "CustomModel", vocabulary = SDL_CORE, prefix = "m")
    public class CustomModel extends AbstractEntityModel {
    
        @SemanticProperty("m:myCustomFieldValue")
        private String myCustomFieldValue;
    
        public String getMyCustomFieldValue() {
            return this.myCustomFieldValue;
        }
    }
  • Create a custom Page Model (example):
    package com.sdl.dxa.modules.test.model;
    
    import com.sdl.webapp.common.api.mapping.annotations.SemanticEntity;
    import com.sdl.webapp.common.api.mapping.annotations.SemanticProperty;
    import com.sdl.webapp.common.api.model.page.PageModelImpl;
    
    import static com.sdl.webapp.common.api.mapping.config.SemanticVocabulary.SDL_CORE;
    
    /**
     * Custom Page Model – The SDL Web Page uses the CustomPageMetadata schema
     */
    @SemanticEntity(entityName = "CustomPageMetadata", vocabulary = SDL_CORE, prefix = "m")
    public class CustomPageModelImpl extends PageModelImpl {
    
        @SemanticProperty("m:headline")
        private String headline;
    
        public String getHeadline(){
            return this.headline;
        }
    }
  • Create a controller class (example):
    package com.sdl.webapp.addon.controller;
    
    import com.sdl.webapp.common.api.WebRequestContext;
    import com.sdl.webapp.common.api.content.ContentProviderException;
    import com.sdl.webapp.common.api.localization.Localization;
    import com.sdl.webapp.common.api.model.MvcData;
    import com.sdl.webapp.common.controller.BaseController;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * TestController
     *
     * @author 
     */
    @Controller
    @RequestMapping("/system/mvc/Example/ExampleController")
    public class TestController extends BaseController {
    
        @Autowired
        private WebRequestContext webRequestContext;
    
        @RequestMapping(method = RequestMethod.GET, value = "DoStuff/{regionName}/{entityId}")
        public String handleGetEntity(HttpServletRequest request, @PathVariable String regionName, @PathVariable String entityId) throws ContentProviderException {
    
    
            return "Hello world";
        }
    
    }

What to do next

Once your custom module structure is created, you can start adding views to it.