天天看点

webwork 整合 json webwork 使用 json 亲自测试过

  找了很多资料和自己的测试,终于能把webwork和json结合一起使用。不过webwork一定要用2.2.7这版本,其他过低的版本都不支持json。如果哦想在其他版本使用json可以自己开发些工具,webwork 2.2.7对json的支持不是很完美。下面就说下如何使用:

     这个是检验用户名是否重复例子。

     action 中上下文多一个返回JSONObject的数据类型

//页面传过来的登录名

 private String loginName;

//注意命名一定要是jsonObject

 public JSONObject jsonObject;

 public String isBeging() throws Exception {

  jsonObject=new JSONObject();

  jsonObject.put("message", new UserService().isBeging(loginName)); 

  return "success";

 }

 public JSONObject getJsonObject() {

  return jsonObject;

 }

 public void setJsonObject(JSONObject jsonObject) {

  this.jsonObject = jsonObject;

 }

 public String getLoginName() {

  return loginName;

 }

 public void setLoginName(String loginName) {

  this.loginName = loginName;

 }

     xwofk.xml配置信息

 <package name="myjson" extends="webwork-default">

  <action name="login" class="action.LoginAction"

   method="isBeging">

   <result name="success" type="json"></result>

  </action>

 </package>

上面只是其中的一种配置

<action name="getActiveCustomers" class="...">

webwork 整合 json webwork 使用 json 亲自测试过

     <result name="success" type="json">

     <!--这两个参数就是JSONResult里的两个字段,其实jsonObjectProperty应该就是返回JSONObject字段的名字。--> 

     <!--jsonObjectProperty大家还是不要动的好,会出错的。下面一个及是返回的类型,不用改。--> 

webwork 整合 json webwork 使用 json 亲自测试过

         <param name="jsonObjectProperty">activeCustomer</param>

webwork 整合 json webwork 使用 json 亲自测试过

         <param name="contentType">application/json</param>

webwork 整合 json webwork 使用 json 亲自测试过

     </result>

webwork 整合 json webwork 使用 json 亲自测试过

 </action>

     jsp页面

<%@ page language="java" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

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

<html>

  <head>

    <base href="<%=basePath%>" target="_blank" rel="external nofollow" >

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

 <meta http-equiv="pragma" content="no-cache">

 <meta http-equiv="cache-control" content="no-cache">

 <meta http-equiv="expires" content="0">   

 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

 <meta http-equiv="description" content="This is my page">

 <!--

 <link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >

 -->

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

 <script type="text/javascript">

  function login(){

   jQuery.post("login.action",

    {"loginName":$("#loginName").val()},

    function (json){

     if(json.message){

      $("#message").css("color","#FF0000");

      $("#message").text("被占用,请更换");

     }else{

      $("#message").css("color","#008000");

      $("#message").text("恭喜你,可以使用");

     }

    },

    "json");

  }

 </script>

  </head>

  <body >

    用户名:<input type="text" id="loginName" οnchange="login()"/> <font id="message"></font><br>

    <input id="login" value="注册" type="button" readonly="readonly"/>

  </body>

</html>