六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

struts2.1实现ajax通信

[复制链接]

升级  10%

66

主题

66

主题

66

主题

举人

Rank: 3Rank: 3

积分
230
 楼主| 发表于 2013-1-23 02:51:09 | 显示全部楼层 |阅读模式
首先去下载 http://code.google.com/p/jsonplugin/downloads/list 先
还有记得加埋strtus官方lib里面有关json的jar包
然后添加测试action
package com.json;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.googlecode.jsonplugin.annotations.JSON;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("unchecked")public class JSONExample extends ActionSupport implements  ServletRequestAware{private static final long serialVersionUID = 8465922425903754099L;private String tip;private Map map = new HashMap();private HttpServletRequest request;@JSON(name="testMap")public Map getMap() {return map;}public void setMap(Map map) {this.map = map;}@JSON(name="newName")public String getTip() {return tip;}public void setTip(String tip) {this.tip = tip;}@Overridepublic String execute() throws Exception {setTip( "系统中已有用户名,请重新选择一个!");this.map.put("ok", "dd");this.map.put("ss", "lal");System.out.println("************"+this.request.getParameter("name"));return SUCCESS;}@Overridepublic void setServletRequest(HttpServletRequest arg0) {this.request = arg0;}} 页面使用jqurey
<script type="text/javascript" src="jquery.js"></script><script type="text/javascript">$(document).ready( function() {$("a").click( function() {$.getJSON("JSONExample.action","name="+$("#asdf").value, function(json){  $("#ss").html(json.testMap.ss);  $("#ok").html(json.testMap.ok);  $("#newName").html(json.newName);}); });});</script><a href="#">dfdf</a><div id ="ss"></div><br><div id="newName"></div><br><div id ="ok"></div> struts.xml中加入
<constant name="struts.i18n.encoding" value="UTF-8"/><package name="com.json" extends="json-default"><action name="JSONExample" class="com.json.JSONExample"><result type="json" /></action></package> 然后试试
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表