Category Archives: Struts2

The knowledge about Struts2

How to assign json object in Struts2

Have to say struts-json-plugin have do a lot of jobs for us. Here is the configuration for the backend.

struts.xml

I’ll skip the action part. The main part is the result tag.

<result type=”json”>

<!– This is the output json object, which should be one of the property of the action. Dont forget to new this object. –>
<param name=”root”>dataMap</param>

<param name=”excludeNullProperties”>true</param>

<param name=”includeProperties”>   userList.*</param>

<param name=”excludeProperties”>
SUCCESS
</param>
</result>

 

Action part

private Map dataMap;

//getter and setter

public String execute() {

dataMap.clear();
User user = new User();
user.setId(“123″);
user.setName(“JSONActionStruts2″);
user.setPassword(“123″);
user.setSay(“Hello world !”);
dataMap.put(“user”, user);

dataMap.put(“success”, true);

return SUCCESS;
}

 

Done!

How to know if a List’s size in Struts2 tag

In my last project, I forgot to judge the list’s size before loop it. So I got an Exception. Then I googled the following solutions.

1. To know if it’s empty:

<s:if test="%{productList.isEmpty()}">
    //if the list is empty
</s:if>
<s:else>
    <s:iterator value="productList">
      //if not empty, then do the loop
    </s:iterator>
</s:else>

2. To know its size

<s:if test="%{productList.size() > 0}">
    //do the loop
</s:if>