日期:2014-05-16  浏览次数:20370 次

Struts2+JSON+JQuery简单示例

一、所需jar包

commons-logging-1.1.1.jar

commons-collections-3.1.jar

commons-digester-2.0.jar

commons-beanutils-1.7.0.jar

xstream-1.2.2.jar

commons-io-2.0.1.jar

commons-lang-2.5.jar

javassist-3.11.0.GA.jar

ognl-3.0.1.jar

struts2-core-2.2.3.jar

xwork-core-2.2.3.jar

commons-fileupload-1.2.2.jar

freemarker-2.3.16.jar

?

struts2-json-plugin-2.2.3.jar

json-lib-2.2.3-jdk15.jar

dom4j-1.6.1.jar

ezmorph-1.0.6.jar

?

二、index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>  
<%@ taglib prefix="s" uri="/struts-tags"%>

<script type="text/javascript" src="js/jquery-1.4.2.js" ></script>
<script type="text/javascript">
<!--
	
	$(document).ready(function(){

		$("#registerButton").click(function(){

			var url = "json.action";
			var params = {"uid":$("#uid").attr("value")};
			
			$.getJSON(url,params,function callback(data){
				var user = eval("("+data+")");
				$("#result").each(function(){
					$(this).html("welcome ,"+user.uid);
				});
			});
			
			return false;
		})
	
	});
	
//-->
</script>
  
<html>  
  <head>  
    <title>JSON Page</title>  
  </head>  
    
  <body>  
  
    <s:form method="post" id="form">
    	 <table align="center">  
            <tr>  
                <td>  
                    <s:textfield label="USERNAME" name="uid" id="uid"/>  
                </td>
                <td>
                	<s:div id="result" cssStyle="color:#ff0000;"/> 
                </td>  
            </tr>  
            <tr>  
                <td colspan="2">  
                    <s:submit id="registerButton" value="JSON"/>  
                </td>  
            </tr>  
        </table>  
    </s:form> 
  </body>  
</html> 

?

三、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.i18n.encoding" value="UTF-8"/>

    <package name="json" extends="json-default">
    	<action name="json" class="com.s2ajax.exer.web.actions.JSONAction" method="jsonTest">
    		<result type="json">
    			<param name="root">result</param>
    		</result>
    	</action>
    </package>

</struts>

?

四、JSONAction

package com.s2ajax.exer.web.actions;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

public class JSONAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -7645203344502656194L;

	
	private String uid ;
	
	private String result;
	
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String jsonTest(){
		
		Map<Object,Object> map = new HashMap<Object,Object>();
		map.put("uid", getUid());
		
		JSONObject jsonObj = JSONObject.fromObject(map);
		
		result = jsonObj.toString();
		
		return SUCCESS;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	
}
?
1 楼 zhucelogin 2011-11-02  
不错,包括要导入的架包也挺正确,学习了,最近也在做jquery easy ui+struts2的