Struts Ajax Json

一、包

struts2与json的依赖包:struts2-json-plugin-2.2.3.jar

 

二、过程:

①引入json依赖包

②编写action类

③配置struts.xml

④编写页面

⑤测试

 

三、demo

1、Action 类

package com.zxt.action;
import com.opensymphony.xwork2.ActionSupport;
/**
 *
 * @Title: JsonAction.java
 * @Package com.zxt.action
 * @Description:struts2 + ajax + json用例
 * @author zxt
 * @date 2011-12-6 上午10:38:51
 * @version V1.0
 */
public class JsonAction extends ActionSupport {
    /**
     *
     */
    private static final long serialVersionUID = 7443363719737618408L;
    /**
     * 姓名
     */
    private String name;
    /**
     * 身高
     */
    private String inch;
    /**
     * ajax返回结果,也可是其他类型的,这里以String类型为例
     */
    private String result;
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        if ("张三".equals(name)) {
            result = "身份验证通过,身高为" + inch;
        } else
            result = "不是张三!";
        return SUCCESS;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getInch() {
        return inch;
    }
    public void setInch(String inch) {
        this.inch = inch;
    }
    /**
     *
     * @Title: getResult
     * @Description:json调取结果
     * @param @return
     * @return String
     * @throws
     */
    public String getResult() {
        return result;
    }
}

 

 

2、Struts 配置

<package name="ajax" extends="json-default">
    <action name="jsonAjax" class="com.zxt.action.JsonAction">
        <!-- 将返回类型设置为json -->
        <result type="json"></result>
    </action>
</package>

 

3、JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    <% String path=r equest.getContextPath(); String basePath=r equest.getScheme()+ "://"+request.getServerName()+ ":"+request.getServerPort()+path+ "/"; %>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>

        <head>
            <base href="<%=basePath%>">
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>测试</title>
            <script type="text/javascript" src="include/js/jquery-1.4.2.min.js"></script>
            <script type="text/javascript">
                $(function () {
                    $("#tj").click(function () {
                        //提交的参数,name和inch是和struts action中对应的接收变量
                        var params = {
                            name: $("#xm").val(),
                            inch: $("#sg").val()
                        };
                        $.ajax({
                            type: "POST",
                            url: "jsonAjax.action",
                            data: params,
                            dataType: "text", //ajax返回值设置为text(json格式也可用它返回,可打印出结果,也可设置成json)
                            success: function (json) {
                                var obj = $.parseJSON(json); //使用这个方法解析json
                                var state_value = obj.result; //result是和action中定义的result变量的get方法对应的
                                alert(state_value);
                            },
                            error: function (json) {
                                alert("json=" + json);
                                return false;
                            }
                        });
                    });
                });
            </script>
        </head>

        <body>
            <span>姓名:</span>
            <input id="xm" type="text">
            <br/>
            <span>身高:</span>
            <input id="sg" type="text">
            <br/>
            <input type="button" value="提交" id="tj">
        </body>

        </html>

 

4、Jquery 解析

 var data = {
     "list": [{
         "id": 1,
         "content": "测试信息1111"
     }, {
         "id": 2,
         "content": "测试信息2222"
     }]
 }
 $.each(data.list, function (i, item) {
     alert(item.id);
     alert(item.content);
 });

 

 

 

参考

[1] 天天向上.struts2 + ajax + json的结合使用,实例讲解.http://hi.baidu.com/zhaotao_king/item/d356c1c5a84bb4c0994aa074

[2] yin_jw.Struts2+json+jQuery(用户名验证).http://yin-jw.iteye.com/blog/1095885

[3] ini_always.struts2整合json出现no result type defined for type 'json'的解决方法.http://www.cnblogs.com/ini_always/archive/2011/10/15/2213404.html

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(struts)