Struts2.0 和JQuery實現AjaX的調用功能

   开发过程忽略:

 重点注意点解释以下:

(1)将Action相应的格式设置为流的格式.

(2)将响应的信息以JSon格式保存在ActionContext.

(3)使用JQuery调用.

重点代码如下:

Action采用注解方式:

/**
 * 发送的短信控制器
 *
 * @author longgangbai
 *
 */
@Result(name = "send", type = ResultTypeSpace.STREAM, params = { "inputName",
  "resultStream" })
public class HistorySmsAction extends BaseAction<HistorySms, HistorySmsService>

 

 

/**
  * 发送短信
  *
  * @return
  */
 public String send() {
  if (sms != null) {
   StringBuffer result = new StringBuffer();
   int resultCode = SMSUtils.sendShortMessage(sms.getMobileNo(), sms
     .getContent());
   if (resultCode == SUCCESS_CODE) {
    sms.setResult(OperateType.SUCCESS);
   } else {
    sms.setResult(OperateType.FAILURE);
   }
   String info = SMSUtils.getResult(resultCode);
   sms.setOrderId(vacationOrderNo);
   result.append("{info:\"").append(info).append("\"}");
   ActionContext ctx = ActionContext.getContext();
   try {
    ctx.put("resultStream", new ByteArrayInputStream(result
      .toString().getBytes("UTF-8")));
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   }
   defaultEntityService.save(sms);
  }
  return SEND;
 }

 

页面调用:

$(document).ready(function()
{
  $('#send_ajax').click(function(){
      var params=$('#inputFrom').serialize(); //序列化表单的值
     $.post(
     '${ctx}/history-sms!send.dhtml',

       params,

     function(data)
     {
       alert("短信提示:" "+data.info);
     },'json');
  });
}
);

你可能感兴趣的:(jquery,Ajax,json)