Struts2 Action being called twice if result type is json
Asked Answered
S

1

8

I have an Action class with 4 action methods. All four action action methods use a json result.

Via logging statements and debugging, I have verified that if I call action method 1, action method 2 and 3 are also called. But not 4. Finally, action method 1 is called again and the json result is generated

If I change the result type of Action method 1 to the default dispatcher with a jsp location, only action method 1 is called. (this is the behavior I want with the json result) Hope that makes sense.

Anyone have any ideas? This question was asked here https://stackoverflow.com/questions/3767698/struts2-if-result-type-json-and-method-defined-then-all-methods-get-invoked

But there was no answer. Please let me know if you need more information.

@ResultPath("/WEB-INF/jsp/dta/")
public class GroupEntityAction extends BaseAction {
    /**
     * 
     */
    private static final long serialVersionUID = 6750675222824235086L;
    private static Logger log = Logger.getLogger(GroupEntityAction.class);

    private List<EntityBusiness> theUnusedEntityBusinessList;
    private String assignedEntities[];
    private long groupId;
    private long businessId;
    private String parentMe;
    private long rptYear;
    private String ssoId;
    private String isSubmitted;

    private String delimGoLiveEmails;
    private List<String> theEmailList;

    @Action(value = "ajaxGetAvailableEntityList",
            results = { @Result(name = "success", type = "json") }
            ,
            interceptorRefs = { @InterceptorRef("dtaStack"),
                    @InterceptorRef(value = "dtaStack", params = { "appInterceptor.allowedRoles", "ADMIN" }) }
            )
            public String getEntityListsByBusiness() throws Exception {

        if (rptYear == 0) {
            return SUCCESS;
        }

        LookupService theSvc = new LookupService();

        if (businessId != 0) {
            setTheUnusedEntityBusinessList(theSvc.getAvailableEntityListBizExceptIds(rptYear, businessId, ssoId, assignedEntities));
        } else {
            setTheUnusedEntityBusinessList(theSvc.getAvailableEntityListParentMeExceptIds(rptYear, parentMe, ssoId, assignedEntities));

        }

        log.debug(theUnusedEntityBusinessList.size());

        return SUCCESS;
    }

    @Action(value = "ajaxToggleGroupBusinessSubmitted",
            results = { @Result(name = "success", type = "json") }
            ,
            interceptorRefs = { @InterceptorRef("dtaStack") }
            )
            public String toggleGroupBusinessReview() {

        try {
            new ProformaService().toggleIsSubmitted(getCurrentUser().getSsoId(), groupId, rptYear, businessId);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return ERROR;
        }
        return SUCCESS;
    }

    @Action(value = "ajaxGetGoLiveEmailList",
            results = { @Result(type = "json") }
            ,
            interceptorRefs = { @InterceptorRef("dtaStack"),
                    @InterceptorRef(value = "dtaStack", params = { "appInterceptor.allowedRoles", "ADMIN" }) }
            )
            public String getGoLiveEmailList() {

        try {
            List<TaxUser> theUserList = new SecurityService().getAll();

            List<String> theEmailList = new ArrayList<String>();
            for (TaxUser theUser : theUserList) {

                if ((!theUser.getRoles().contains("ADMIN")) && (theUser.getIsActive().equalsIgnoreCase("Y"))) {
                    if (!theEmailList.contains(theUser.getEmail())) {
                        theEmailList.add(theUser.getEmail());
                    }
                }
            }

            setDelimGoLiveEmails(StringUtils.join(theEmailList.toArray(), "|"));
            setTheEmailList(theEmailList);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return ERROR;
        }

        return SUCCESS;
    }

    @Action(value = "ajaxGetChaserEmailList",
            results = { @Result(name = "success", type = "json") }
            ,
            interceptorRefs = { @InterceptorRef("dtaStack"),
                    @InterceptorRef(value = "dtaStack", params = { "appInterceptor.allowedRoles", "ADMIN" }) }
            )
            public String getChaserEmailList() {

        try {

            List<String> theEmailList = new LookupService().getChaserEmailList();

            setDelimGoLiveEmails(StringUtils.join(theEmailList.toArray(), "|"));
            setTheEmailList(theEmailList);

        } catch (SQLException e) {
            log.error(e.getMessage());
            return ERROR;
        }
        return SUCCESS;
    }

    public void setTheUnusedEntityBusinessList(
            List<EntityBusiness> theUnusedEntityBusinessList) {
        this.theUnusedEntityBusinessList = theUnusedEntityBusinessList;
    }

    public List<EntityBusiness> getTheUnusedEntityBusinessList() {
        return theUnusedEntityBusinessList;
    }

    public void setAssignedEntities(String assignedEntities[]) {
        this.assignedEntities = assignedEntities;
    }

    public String[] getAssignedEntities() {
        return assignedEntities;
    }

    public void setGroupId(long groupId) {
        this.groupId = groupId;
    }

    public long getGroupId() {
        return groupId;
    }

    public void setBusinessId(long businessId) {
        this.businessId = businessId;
    }

    public long getBusinessId() {
        return businessId;
    }

    public void setParentMe(String parentMe) {
        this.parentMe = parentMe;
    }

    public String getParentMe() {
        return parentMe;
    }

    public void setRptYear(long rptYear) {
        this.rptYear = rptYear;
    }

    public long getRptYear() {
        return rptYear;
    }

    public void setSsoId(String ssoId) {
        this.ssoId = ssoId;
    }

    public String getSsoId() {
        return ssoId;
    }

    public void setIsSubmitted(String isSubmitted) {
        this.isSubmitted = isSubmitted;
    }

    public String getIsSubmitted() {
        return isSubmitted;
    }

    public void setDelimGoLiveEmails(String delimGoLiveEmails) {
        this.delimGoLiveEmails = delimGoLiveEmails;
    }

    public String getDelimGoLiveEmails() {
        return delimGoLiveEmails;
    }

    public void setTheEmailList(List<String> theEmailList) {
        this.theEmailList = theEmailList;
    }

    public List<String> getTheEmailList() {
        return theEmailList;
    }
}

In this action class, I attempting to call ajaxGetGoLiveEmailList, and what I get is ajaxGetGoLiveEmailList called first, and then ajaxGetChaserEmailList, and then ajaxGetAvailableEntityList, and then ajaxGetGoLiveEmailList gets called again. ajaxToggleGroupBusinessSubmitted is skipped.

If I change the result annotation of ajaxGetGoLiveEmailList to

results={@Result(location="something.jsp")

, only ajaxGetGoLiveEmailList get called.

When I look at the config browser, all the action mapping are configured correctly, pointing to the correct method calls.

Sharpfreeze answered 19/5, 2011 at 16:20 Comment(1)
Please post your action code or a test case that exhibits the problem.Morvin
C
19

JSON plugin may be calling all your methods that start with "get" in an attempt to serialize them for output. Try renaming your methods to something else.

Carcinomatosis answered 19/5, 2011 at 20:24 Comment(3)
I have the same problem. Change function name to "loadxxx". It works now. (:Shorts
Wow! Thanks, nmc. I had the same weird problem. This solution solved it.Shutz
Thanks a lot nmc. I still couldn't believe it. This fixed the problem.Emblazonment

© 2022 - 2024 — McMap. All rights reserved.