1. 程式人生 > >struts2使用ajax返回的字串和ajax返回的json

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

使用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>

<script type="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">

<result type="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>

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

</head>

     <script type="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>

  <input type="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">

<action name="uajax" class="base.AjaxUserAction" method="getUser">

<result type="json">

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

</result>

</action>

</package>