3

I need to use a custom PageModel but I'm not sure how to do this. I'm on Tridion 2013, DXA Java.

I've tried changing the line in our code that says

import com.sdl.webapp.common.api.model.page.PageModelImpl;

@RegisteredViewModel(viewName = "HomePage", modelClass = PageModelImpl.class),

to replace PageModelImpl with my custom page model instead. But this results in a Internal Server Error.

Is it necessary for me to create a whole new module and a custom PageController? Or is there any way to use a custom model with the generic PageController?

  • Can you edit your question and provide a bit more details like what version of DXA you are using and if it is Java or .NET etc.? Please note the documentation of DXA also provides details on how to create your own module with your own Models, that is essentially what you want to do. – Bart Koopman Feb 9 '18 at 10:06
  • @BartKoopman Thanks! I appreciate the response. I'm coming in relatively new to a mature project, so I understand that I might be asking questions with obvious answers. I did see the manual had info about creating custom modules, but my real question is if I can use a custom model without rewriting everything else. I have edited to highlight that. – sunamc1 Feb 9 '18 at 14:32
5

Since you are using SDL Tridion 2013, I'm assuming your are also using DXA 1.6 (as that is the latest release supporting 2013).

In the documentation there is a chapter about developing new website elements, which also describes how to create custom models. A custom Page Model example you can find below:

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 Tridion 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;
    }
}

You would store these custom Models in a DXA Module, and the explanation on how to create a DXA Module on Java can also be found in the documentation here: https://docs.sdl.com/LiveContent/content/en-US/SDL%20DXA-v6/GUID-AA979315-2AFD-4B7F-8FC7-980E85855295

The next step will then also be to create a custom view for your new Page Model, again, you will find an explanation of that in the documentation. Basically all of what you are asking is covered in the docs, under the mentioned topic https://docs.sdl.com/LiveContent/content/en-US/SDL%20DXA-v6/GUID-41A36F76-1154-4D8A-B2C9-D62F9829A7F2

You don't need a custom controller just for a custom Page Model and View, customizing the controller is only needed when you need to change the default routing or handling of Pages. You could decide not to create your own DXA Module and modify the Page Model and Views in the DXA Core module, that will definitely work, but it is considered a better practice to leave the existing example code as is, and customize DXA via your own custom Modules (since they are also not impacted in case you ever wish to upgrade to a newer DXA release).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.