struts1 + jquery + ajax学习

使用四种回传数据:

1.直接返回生成的HTML代码。

2.返回JS代码。

3.返回XML格式的文本字符串。

4.返回JSON格式的字符串。


返回JSON格式的字符串使用到了JSON包。

使用JSON包需要几个附加包。如下:

Json-lib requires (at least) the following dependencies in your classpath:

  • jakarta commons-lang 2.5
  • jakarta commons-beanutils 1.8.0
  • jakarta commons-collections 3.2.1
  • jakarta commons-logging 1.1.1
  • ezmorph 1.0.6

代码如下:

JSP页面:ButtonCreator.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>



	
		

		My JSP 'ButtonCreator.jsp' starting page

		
		
		
	

	
		
		
		
		
		
button产生区域
JS文件:myJs.js

$(document).ready(function(){
	$("#button1").click(function(){
		$.get("buttonCreatorAAC.do",{type:"type1"},function(result){
			$("#buttonContainer").html(result);
		});
	});
	$("#button2").click(function(){
		$.get("buttonCreatorAAC.do",{type:"type2"},function(result){
			eval(result);
			var btnNames = getButtonsInf();
			var innerHtml = "";
			for(var i = 0; i < btnNames.length; i++){
				innerHtml += "";
			}
			$("#buttonContainer").html(innerHtml);
		});
	});
	$("#button3").click(function(){
		$.ajax({
			url:"buttonCreatorAAC.do",
			data:{type:"type3"},
			dataType:"XML",
			success:function(result){
				var innerHtml = "";
				$(result).find("course").each(function(id){
					var btnName = $(this).children("name").text();
					innerHtml += "";
				});
				$("#buttonContainer").html(innerHtml);
			}
		});
	});
	$("#button4").click(function(){
		$.get("buttonCreatorAAC.do",{type:"type4"},function(result){
			var jsonObj = eval("(" + result +")");
			var innerHtml = "";
			for(var i = 0; i < jsonObj.length; i++){
				innerHtml +="";
			}
			$("#buttonContainer").html(innerHtml);
			
		});
	});
});

strut1 action类

/*
 * creator ^_^
 * 
 */
package com.esoft.demo.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ButtonCreatorAACAction extends Action {
	String[] btnNames = {"C语言","JAVA语言","C#语言","C++语言","Ruby语言","火星语"};
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String type = request.getParameter("type");
		StringBuffer sb = new StringBuffer();
		if(type.equals("type1")){
			//生成HTML的方式来生成按钮
			sb.append("");
		
		}else if(type.equals("type2")){
		//通过JS数组的方式
			sb.append("function getButtonsInf(){")
				.append("var btnNames = new Array();");
				for(int i = 0; i < btnNames.length; i++){
					sb.append("btnNames.push('" + btnNames[i] + "');");
				}
				sb.append("return btnNames");
				sb.append("}");
		}else if(type.equals("type3")){
		//XML的方式生成按钮
			sb.append("");
			sb.append("");
			for(int i = 0; i < btnNames.length; i++){
				sb.append("")
					.append("")
					.append(btnNames[i])
					.append("")
					.append("");
			}
			sb.append("");
		}else{
		//JSON的方式生成按钮
			JSONArray jsonArray = JSONArray.fromObject(btnNames);
			sb.append(jsonArray.toString());
		}
		out.write(sb.toString());
		out.flush();
		out.close();
		return null;
	}
}
代码: http://download.csdn.net/detail/duliming1987/4510806



你可能感兴趣的:(struts,web前端,JavaScript)

") .append("") .append("") .append("") .append("") .append("") .append("") .append("