1

I have below requirement in my project using Java DXA 1.7, I am new to DXA. Can someone provide me which Component Template and Page Template should I use for my requirement?

Also how can I achieve my JSON output in my custom controller?

Schema Name: Custom Content

Fields in Schema:

  • title - Text Field
  • contentBody - RichText Box
  • footerContent - Rich Text Box

When I request through http://mydomain.com/CustomController/MyPageName

My Desired Output should be as below JSON format:

{
    "title":"Some Title",
    "contentBody":"Some RichText Content",
    "footerContent: "Again Some RichText Content"
}
  • So, if you request some Page, you want to return JSON representing the content of a Component on that Page? What if there are multiple Components on the Page? – Rick Pannekoek May 23 '18 at 6:21
  • Rick - I need the content in an array object. – Arulmozhi May 23 '18 at 11:25
1

Your requirement is somewhat similar to this: How to render plain JSON (without any additional tag) data in DXA 1.5 java?

However, what you want seems to be more in line with what DXA is designed for. Nevertheless, you will need to use a custom Page Controller which returns the JSON you want.

The underlying data can be obtained from the Page Model: pageModel.getRegions().get(“Main”).getEntities(). This will return a set of Entity Models of a type which you defined (and registered).

You could use a regular Strongly Typed Entity Model, which uses individual properties for each field. Alternatively, you can use a special semantic mapping where all CM fields are mapped to a single property of type Map<String,String>. If you serialize such property to JSON, it will yield the output you’re looking for.

The DXA Framework comes with a predefined Entity Model type that uses this “all CM fields to single property” mapping: class Configuration. You can use this class instead of defining your own Entity Model.

  • Rick - Can I get example code for the custom page controller? – Arulmozhi May 30 '18 at 5:03

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.