分享-struts2的AJAX怎么实现

在struts2 的ajax里,实现比较简单。
从页面开始吧
1.写好页面,页面里写好实现ajax 的函数,我使用jQuery 的$.ajax({……….}) 的方式实现,待会儿会贴出代码
2.创建好处理ajax请求 的Action 类
3.在struts2 的struts.xml 核心配置文件中配置访问此Action 的Action元素
4.如果一切正常就不需要调试了
脚本↓↓↓

function submit(){
        var content=$("#content");
        if(content.val().length>0&&content.val().length<=200){ // jquery 里面的值是val()
            alert(content.val());
            var infoId=$("#infoId").val();  alert(infoId);
            var data={content:content.val(),infoId:infoId};
            $.ajax({
                url:"replyInformation",
                data:data,
                type:"post",
                dataType:"json",
                success:function(result){
                    $("#norm").after("发表于:"+result.replyTime+"<br/>"+result.content+"<br/><br/>");
                }
            })
        }else{
            alert("请填写回复内容");
        }
        return ;
    }

页面元素↓↓↓

<input type="hidden" id="infoId" value="<s:property value="infoId"/>" />
<h1><s:property value="info.title"/></h1>
发表于:<s:property value="info.reportTime"/> <br/><br/>
<s:property value="info.content"/> <br/><br/>

<h3 id="norm">读者回应<br/><br/></h3> 

<s:iterator value="replyList" id="reply">
    发表于:<s:property value="replyTime"/> <br/>
    <s:property value="content"/>  <br/><br/>
</s:iterator>

快速回复(字数200以内):  <br/>
<textarea id="content" rows="" cols=""></textarea>  <br/>
<input type="button" value="提交" onclick="submit()" /><a href="index.jsp">返回首页</a>

处理请求的Action↓↓↓

private String content;
private Integer infoId;

public String replyInformation(){
        reply=replyService.save(content,infoId);//保存回复内容
        Information information=informationService.updateReplyCountAndLastPostTime(infoId);//修改资讯last time and count
        return "success";
    }

配置struts2 的核心文件↓↓↓

  <action name="replyInformation" class="cn.jbit.test.action.InformationAction" method="replyInformation">
      <result type="json" >
         <param name="root">reply</param>
      </result>
  </action>

然后最后就是jar 包的问题了
需要struts2-json-plugin-2.2.1.jar
jQuery类库:jquery-1.8.3.min.js

如有遗漏欢迎指出^_^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值