页面:
$(document).ready
(
function()
{
$("a").click
(
function()
{
alert("Hello world!");
getHelloxml();
//getH();
}
);
}
);
//ajax调用方法一
function getHelloxml(){
$.ajax({
type: "POST",
url: "hello.do",
//dataType:"xml",
success: function(xml){
alert( "Data Saved: " +$(xml).text());
}
})
}
//ajax调用方法二
function getH(){
$.get("hello.do", function(data){
alert("Data Loaded: " + $(data).children().text());
});
}
action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.thoughtworks.xstream.XStream;
/**
* MyEclipse Struts
* Creation date: 02-22-2010
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class HelloAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
XStream xs = new XStream();
try {
//Message message=new Message();
//message.setRest("zhao zhiming ");
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(xs.toXML("你好"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
struts-config