可以换span,具体作用问美工吧。
接受ajax请求的action。
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
*
Title:AjaxAction
*/
public class AjaxAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
PrintWriter out = response.getWriter();
out.print(1);//ajax取得都是字符的输出。如果数据量大的话,还可以用xml来发送和接受
return null;
}
}
struts-config.xml
----------------------------
如果是servlet
web.xml
AjaxServlet
servlet.AjaxServlet
AjaxServlet
/AjaxServlet .servlet
AjaxServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AjaxServlet extends HttpServlet {
Logger log = Logger.getLogger(this.getClass());
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
response.setContentType("text/xml; charset=GBK");
PrintWriter out = response.getWriter();
out.print(2);
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
//Process the HTTP Put request
public void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//Process the HTTP Delete request
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
-------------------------------------
如果是webwork
xwork.xml
AjaxAction.java
import java.io.PrintWriter;
public class AjexAlbumAction extends ActionSupport implements Action{
public void ajax()throws IOException {
PrintWriter pw = ServletActionContext.getResponse().getWriter();
ServletActionContext.getResponse().setContentType("text/html;charset=GBK");
pw.print(1);
pw.close();
}
}
本例子在winXPsp2、JB9、Eclipse3,j2sdk1.4.1 、Tomcat5、Tomcat4.1都能运行