随笔 - 55  文章 - 187  trackbacks - 0
<2016年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(12)

随笔分类

随笔档案

groovy

搜索

  •  

最新评论

阅读排行榜

评论排行榜

使用这个组合,感觉还是很方便灵活的。

1、将struts2的json插件加入web工程的lib,jsonplugin的下载地址:http://code.google.com/p/jsonplugin/downloads/list

2、struts.xml添加专为ajax使用的package
<package name="ajax" extends="json-default">
        
<action name="ajaxRequest"
            class
="org.david.struts2.HelloWorld">
            
<result type="json"></result>
        
</action>
    
</package>

3、helloworld.jsp
           <SCRIPT type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
        
<SCRIPT type="text/javascript">
            
function clickButton()
            
{    
                
var url = 'ajaxRequest.action';
                
var params = {
                        name:$('#name').attr('value')
                }
;
                jQuery.post(url, params, callbackFun, 'json');
            }

            
function callbackFun(data)
            
{
                alert(data.result);//对应HelloWorld类的message属性
                    //获取数据后渲染页面
            }

        
</SCRIPT>



       
<input id="name" type="text">
        
<input type="button" value="ok" onclick="javascript:clickButton();">

4、HelloWorld.java
package org.david.struts2;

public class HelloWorld {

    
private String name;
    
private String result;

    
// ajax请求参数赋值
    public void setName(String name) {
        
this.name = name;
    }


    
// ajax返回结果
    public String getResult() {
        
return result;
    }


    
public String execute() {
        
this.result = "Hello! " + this.name + ".";
        
return "success";
    }


}
posted on 2008-09-07 23:07 大卫 阅读(41942) 评论(17)  编辑  收藏 所属分类: JavaJavaScriptweb

FeedBack:
# re: Struts2 + jQuery 实现ajax 2009-04-11 10:37 starnc
jQuery.post(url, params, callbackFun, 'json');
关于这条语句,我查jquery的api,只有三个参数啊,没有‘json’,这个参数,请解释一下,谢谢  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2009-08-31 21:49 ss
是反回值  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2009-10-30 13:48 123
老大。你的struts2标签呢.  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-01-08 15:13 f
为什么<package name="ajax" extends="json-default">
的ACTION找不到啊  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-01-11 11:19 jazzCat
请问,返回值data肯定是个HelloWorld对象么?
能不能返回自定义的对象呢?  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-01-11 11:20 jazzCat
请问,返回值data肯定是个HelloWorld对象么?
能不能返回自定义的对象呢?或返回一个List?  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-01-14 10:00 #tao
请问callbackFun(data)中的data 是什么啊,我用你的方法可以进入到action里面,可是后台出错了,能否大虾这点下
java.lang.NoSuchMethodError: com.opensymphony.xwork2.ActionContext.get(Ljava/lang/Object;)Ljava/lang/Object;
at com.googlecode.jsonplugin.JSONResult.execute(JSONResult.java:157)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)

  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-01-15 14:46 gjhohj
@#tao
struts 2.0 用json 0.32
struts 2.1 用 json 0.33-34  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2010-04-15 14:09 cc
json在struts2中的配置只用下plugin吗。jon-lib.jar这个包要不要一起加进去
还有一个json.js这个要要加
  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2010-08-18 11:23 叶新华
楼主 说的很清晰. 赞一个  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2011-07-08 13:27 jj
干净明了
  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2011-07-25 11:17 guanlin218
为什么我配置了。一直提示我ajaxRequest.action 这个找不到。  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2012-06-07 18:10 abc
@starnc
是不是随便哪里copy一段来的????  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2012-06-07 18:11 abc
.....  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2012-06-07 18:11 abc
@abc
adsaf  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax 2012-08-25 11:46 妞妞
不用struts的标签也是可以的
  回复  更多评论
  
# re: Struts2 + jQuery 实现ajax[未登录] 2016-01-16 20:29 1
@cc
1  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: