struts2使用ajax返回的字符串和ajax返回的json

2017-10-18 18:54:56来源:CSDN作者:qq_35301916人点击

分享

使用Ajax返回的字符串

案例:校验用户名是否存在,用的是struts2验证

创建jsp向服务器发送ajax请求

<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>MyJSP 'ajax.jsp' starting page</title>

    <scripttype="text/javascript"src="jquery-1.8.3.js"></script>

  </head>

       <scripttype="text/javascript">

               $(function(){

                        $("input").blur(function(){

                                  $.ajax({

                                           url:"ajax.action",

                                           data:{"uname":$("input").val()},

                                           type:"post",

                                           dataType:"text",

                                           success:function(data){

                                                   if(data=="0"){

                                                              $("span").html("");

                                                    }else{

                                                              $("span").html("用户名已存在");

                                                    }

                                           },

                                           error:function(){

                                                    alert("出错");

                                           }

                                  });

                        });

               });

      </script>

  <body>

     <inputtype="text"name="uname"/><span></span>

  </body>

</html>

 

 

 

创建action,写一个返回InputStream的方法,该方法把需要返回的字符串转换成流的方式返回

public class AjaxTestAction extends ActionSupport{

         privateString uname;

         publicInputStream getMessage()throwsUnsupportedEncodingException{

                   String msg="";

                   //判断用户名是否存在

                   if("asd".equals(uname)){

                            msg="1";

                   }else{

                            msg="0";

                   }

                   InputStream is=new ByteArrayInputStream(msg.getBytes("utf-8"));

                   returnis;

         }

         //省略get/set

}

配置struts.xml

<action name="ajax" class="base.AjaxTestAction">

                                     <resulttype="stream">

                                               <param name="inputName">message</param>

                                               <!-- 该流封装的数据,做成一个网页格式 -->

                                               <param name="contentType">text/html</param>

                                     </result>

                            </action>

测试.

以上主要返回的是一个字符串,这种单字符串数据在开发中会大量使用,如仅仅修改密码,修改性别,修改状态等信息。

如果要返回一个对象,或者要返回一个集合到页面中,也可以使用上面的方式完成,如:返回一个UserDTO,或者返回一个List<UserDTO>.实现的方式,就是把对象和集合自己拼接成一个JSON格式的字符串,UserDTO--àjson Object; List<UserDTO>--àjsonArray.

使用Ajax返回的json

1.        导入JSON的插件

2.        写一个普通的Action类,需要封装的数据声明属性和get方式。

3.        在struts.xml文件中,包要继承json-default

4.        在action的配置中,配置结果类型为json


<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">

<html>

  <head>

    <title>My JSP 'ajax.jsp' starting page</title>

    <scripttype="text/javascript"src="jquery-1.8.3.js"></script>

  </head>

       <scripttype="text/javascript">

               $(function(){

                        $("input").click(function(){

                                  $.ajax({

                                          url:"uajax.action",

                                             type:"get",

                                             dataType:"text",

                                             success:function(data){

                                                      alert(data);

                                             },

                                             error:function(e){

                                                      alert(e.status);//404  500 200

                                             }

                                  });

                        });

               });

      </script>

  <body>

     <inputtype="button"value="点击"/>

  </body>

</html>

 

 

 

 

第三步编写action

public class AjaxUserAction extends ActionSupport{

         privateUser u;

         publicString getUser(){

                   User u1=new User();

                   u1.setUname("张三");

                   u1.setUserid(123);

                   u1.setPhone("111");

                   u=u1;

                   return"success";

         }

         publicUser getU() {

                   returnu;

         }

         publicvoid setU(User u) {

                   this.u = u;

         }

}

第四步 配置struts.xml

<package name="new" extends="json-default">

                            <actionname="uajax"class="base.AjaxUserAction"method="getUser">

                                     <resulttype="json">

                                               <param name="root">u</param>

                                     </result>

                            </action>

                   </package>



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台