/**************************************************************
struts.xml
**************************************************************/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<include file="ajax.xml" />
</struts>
/**************************************************************
ajax.xml
**************************************************************/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ajax" namespace="/ajax" extends="struts-default">
<global-results>
<result name="Exception">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="Exception" />
</global-exception-mappings>
<action name="*" method="{1}" class="web.ajax.AjaxAction">
<interceptor-ref name="servlet-config" />
<interceptor-ref name="exception" />
<result name="searchTitleList_success">/ajax/AutoComplete.jsp</result>
</action>
<action name="defaultAction">
<result>/WrongAction.jsp</result>
</action>
</package>
</struts>
/**************************************************************
AjaxAction.java
**************************************************************/
package web.ajax;
import javax.servlet.ServletContext;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionSupport;
/**
* Base Action class for the Tutorial package.
*/
public class AjaxAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private Logger logger = Logger.getLogger(this.getClass());
/**
* test1
* @throws Exception
*/
public String autoComplete() throws Exception {
try{
logger.info("autoComplete start");
// request를 가져온다.
ServletContext servletContext = ServletActionContext.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
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;
}
}