简体   繁体   中英

Two methods in same action in struts2?

I am trying to create an action with two methods (JSON action). I am calling them from JSP files. If I try to call an action value as 'medias' in my code, it simply run both the methods every time.

@Action(value="medias", results = {@Result(name="success",type="json")})
public String getMedias(){
     System.out.println("IN METHOD CALL medias"); 
    return SUCCESS;
}

@Action(value="allMediaTypes", results = {@Result(name="success",type="json")})
public String getAllMediaTypes(){
           System.out.println("IN METHOD CALL allMediaTypes"); 
       return SUCCESS;
}

Both method runs simultaneously, no matter which method is getting called from jsp, it runs both the methods.

Don't prefix your method names with get - doing this has implications.

It's a good idea to name them the same as your action names for consistency, ie:

public String medias() {
    ...
}

public String allMediaTypes() {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM