jquery ajax +struts1.3

页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<html> 
<head> 
<script type="text/javascript" src="jquery-1.4.1.js"></script> 
<script type="text/javascript"> 
$(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());
});
}
</script> 
<body> 
<a href="#" >helloworld</a> 
</body> 

</html>

 

 

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/hello" type="com.yourcompany.struts.action.HelloAction" />

  </action-mappings>

  
</struts-config>

 

你可能感兴趣的:(apache,jquery,Ajax,struts,MyEclipse)