Search code examples
javaxpagesxpages-ssjs

passing arraylist with JsonJavaObject into method from SSJS


On an XPage I have in a viewScope an arraylist with json objects which I want to submit to a managed bean:

function createPersons(){
    var json:java.util.ArrayList = viewScope.get("persons");
    personBean.createPersons(json);
}

here is an example of the viewscope content:

[0] 
area    Compliance
mandatory   true
newArea false
person  Patrick Kwinten/Web
prio    2
[1] 
area    Credits
mandatory   true
newArea false
person  Patrick Kwinten/Web
prio    2
[2] 
area    Food
mandatory   true
newArea true
person  Patrick Kwinten/Web
prio    2

Now I would like to define in my java class the method:

public void createPersons(ArrayList json){ utils.printToConsole(this.getClass().getSimpleName().toString() + " - createPersons() json"); utils.printToConsole("size json = " + json.size()); //todo; }

but I get the error message:

Description Resource Path Location Type JsonJavaObject cannot be resolved to a type personBean.java

Any advice anyone?


Solution

  • I would recommend to use a custom Java object in your view scope instead of relying on the Json serialization. You create a Java class, roughly like this:

        public class AreaInfo {
            private String area;
            private boolean mandatory;
            private boolean newArea;
            private String person;
            private int prio;
    
            public String getArea() {
                return area;
            }
            public void setArea(String area) {
                this.area = area;
            }
            public boolean isMandatory() {
                return mandatory;
            }
            public void setMandatory(boolean mandatory) {
                this.mandatory = mandatory;
            }
            public boolean isNewArea() {
                return newArea;
            }
            public void setNewArea(boolean newArea) {
                this.newArea = newArea;
            }
            public String getPerson() {
                return person;
            }
            public void setPerson(String person) {
                this.person = person;
            }
            public int getPrio() {
                return prio;
            }
            public void setPrio(int prio) {
                this.prio = prio;
            }
    
    
        }
    

    Use that one in your ArrayList in the viewscope. You could also consider having a class around the list if you want to fetch e.g. by Area name etc. Makes it much easier to deal with.

    Let me know if that works for you