All posts by silentred

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!

Use FtpClient to create directory if it doesn’t exist.

Apache Common Net package is really easy to use.

Recently, I was using the FtpClient to upload local images to the ftp server.  I’d like to post my uploader here.

package com.oceania.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import com.oceania.modle.FtpServer;
import com.oceania.modle.Img;
public class Uploader {
static FTPClient ftpClient;
private static final Logger logger = Logger.getLogger(Uploader.class);
public static boolean uploadImg(Img img, FtpServer ftp){
boolean returnValue = false;
ftpClient = new FTPClient();
try {
int reply;
if( ftp.getPort() == 0){
           ftpClient.connect(ftp.getHost());
}else{
ftpClient.connect(ftp.getHost(),ftp.getPort());
}
ftpClient.login(ftp.getUser(), ftp.getPassword());
reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
                logger.info(“FTP SERVER REFUSED CONNECTION.”);
                return returnValue;
            }
            FileInputStream fis =  new FileInputStream(img.getLocalPath());
            if ( !ftpClient.changeWorkingDirectory(ftp.getDir())) {
            ftpClient.makeDirectory(ftp.getDir());
            ftpClient.changeWorkingDirectory(ftp.getDir());
            }
            ftpClient.storeFile(img.getName(), fis);
            fis.close();
            ftpClient.logout();
            returnValue = true;
    } catch (SocketException e) {
           e.printStackTrace();
    } catch (IOException e) {
           e.printStackTrace();
     }finally {
           if (ftpClient.isConnected()) {
               try {
                ftpClient.disconnect();
               } catch (IOException ioe) {
               }
           }
       }
return returnValue;
}
}

 

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>