[/WEB-INF/src/web/ajax/AjaxAction.java]



package web.ajax;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.*;
import com.opensymphony.xwork2.ActionSupport;

public class AjaxAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {

        private HttpServletRequest request;
        private HttpServletResponse response;
    private Logger logger = Logger.getLogger(this.getClass());

   public String autoComplete() throws Exception {

            try{

                    logger.info("autoComplete start");



String[] wordArr = {"acrobat“,                                                "adobe“,                                                "bmw",
                                                "brave“,                                                "cbs“,                                                "cnn",
                                                "heaven“,                                                "html“,                                                "kbs",
                                                "key“,                                                "net“,                                                "news",
                                                "queen“,                                                "quick“,                                                "soup",
                                                "super“,                                                "ufo“,                                                "usb",
                                                "work“,                                                "wow“,                                                "zebra",
                                                "zenith"};
                    StringBuffer xmlStr = new StringBuffer("");
                    boolean isTitleExist = false;

                    //실제업무에서는 비지니스 레이어에서 XML String을 구현한다.
                    //String xmlStr = ajax.getAutoCompleteTitleList(boardVO);

                    xmlStr.append("<?xml version=\"1.0\" encoding=\"EUC-KR\"?>");
                    xmlStr.append("<words>");
for(int i = 0;  i < wordArr.length ; i++){

                            if(wordArr[i].indexOf(request.getParameter("word")) != -1){

                                    xmlStr.append("<word>");
                                    xmlStr.append(wordArr[i]);
                                    xmlStr.append("</word>");
                                    isTitleExist = true;
                            }

                    }


xmlStr.append("</words>");
                    //logger.info("request.getParameter(\"title\") : " + request.getParameter("title"));
                    logger.info("xmlStr.toString() : " + xmlStr.toString());
                    if(isTitleExist){
                            response.setContentType("text/xml");
                        response.setHeader("Cache-Control", "no-cache");
                        response.getWriter().write(xmlStr.toString());
                    }else{
                            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                    }
                    logger.info("forward :" + request.getParameter("forward"));
                    logger.info("autoComplete end");
                    return null;
            }catch(Exception e){
                        logger.info("Error : " + e.toString());
                        e.printStackTrace();
                        throw e;
                }    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
        public void setServletResponse(HttpServletResponse response) {
                this.response = response;
        }
}

 

 

 

 

 

 

+ Recent posts