• struts2+ajax+jquery


    struts2+ajax+jquery

    ajax交互

    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    
                pageEncoding="UTF-8"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <title>Insert title here</title>
    
    <script type="text/javascript" src="js/jquery-1.12.3.js"></script>
    
    <script type="text/javascript">
    
                $(function() {
    
                            $("input:eq(0)").click(function() {
    
                                        $.ajax({
    
                                                    url : "ajax/ajax.action",
    
                                                    data : "word='text交互'",
    
                                                    type : "post",
    
                                                    dataType : "text",
    
                                                    success : function(data) {
    
                                                                alert(data); 
    
                                                    },
    
    error : function(XMLHttpRequest, textStatus, errorThrowing) {
    
                                                                alert(textStaus);
    
                                                    }
    
                                        });
    
                            });
    
     
    
                            $("input:eq(1)").click(
    
                            function() {
    
                                        $.ajax({
    
                                        url : "json/json.action",
    
                                        data : {
    
                                                    "name" : "小明"
    
                                                    },
    
                                                    type : "post",
    
                                                    dataType : "text",
    
                                                    success : function(data) {
    
                                                    alert(data);
    
     
    
                                        $("textarea").html(data);
    
                            //如果返回值类型是text,则需要手动转换为js的json对象                                                           
    
                                        var obj = eval("(" + data + ")");
    
                                                                                        alert(obj.name+obj.age+obj.tel);
    
                            var users = obj.users;
    
                            for (var int = 0; int < users.length; int++) {
    
                            alert(user[i].username + "------------------"                                                                                    + users[i].password);
    
                                                                                        }
    
     
    
                                                                            },
    
    error : function(XMLHttpRequest, textStatus,errorThrowing) {
    
                                        alert(textStatus);
    
                                                                            }
    
                                                                });
    
                                                    });
    
                });
    
    </script>
    
     
    
    </head>
    
    <body>
    
                <input type="button" value="点击" />
    
                <br>
    
                <input type="button" value="json交互" />
    
                <br>
    
                <textarea rows="" cols=""></textarea>
    
    </body>
    
    </html>

    action:

    AjaxAction.java

    public class AjaxAction extends ActionSupport {
    
     
    
                  private String word;
    
     
    
                  public String getWord() {
    
                                return word;
    
                  }
    
     
    
                  public void setWord(String word) {
    
                                this.word = word;
    
                  }
    
     
    
                  public InputStream getContentStream() throws Exception {
    
                                return new ByteArrayInputStream("你好事假".getBytes("utf-8"));
    
                  }
    
     
    
                  @Override
    
                  public String execute() throws Exception {
    
                                System.out.println("AjaxAction.execute()" + word);
    
                                return SUCCESS;
    
                  }
    
     
    
    }

    JsonAction.java

    public class AjaxAction extends ActionSupport {
    
     
    
                  private String word;
    
     
    
                  public String getWord() {
    
                                return word;
    
                  }
    
     
    
                  public void setWord(String word) {
    
                                this.word = word;
    
                  }
    
     
    
                  public InputStream getContentStream() throws Exception {
    
                                return new ByteArrayInputStream("你好事假".getBytes("utf-8"));
    
                  }
    
     
    
                  @Override
    
                  public String execute() throws Exception {
    
                                System.out.println("AjaxAction.execute()" + word);
    
                                return SUCCESS;
    
                  }
    
     
    
    }

    JsonAction.java

    public class AjaxAction extends ActionSupport {
    
     
    
               private String word;
    
     
    
               public String getWord() {
    
                          return word;
    
               }
    
     
    
               public void setWord(String word) {
    
                          this.word = word;
    
               }
    
     
    
               public InputStream getContentStream() throws Exception {
    
                          return new ByteArrayInputStream("你好事假".getBytes("utf-8"));
    
               }
    
     
    
               @Override
    
               public String execute() throws Exception {
    
                          System.out.println("AjaxAction.execute()" + word);
    
                          return SUCCESS;
    
               }
    
     
    
    }

    stryts.xml

    <package name="ajaxp" namespace="/ajax" extends="struts-default">
    
                                <action name="ajax" class="com.clc.Action.AjaxAction">
    
                                              <result name="success" type="stream">
    
                                                            <!-- 当struts2解析这个结果的时候
    
                                                            也就是Action执行完了
    
                                                            需要使用一个输入流读取内容,然后再写入到网络的客户端 inputName用来指定从哪里读取
    
                                                                          将contentStream的首字母大写,加上get前缀,
    
                                                                          得到一个方法名getContentStream 会调用action中的这个方法得到一个输入流
    
                                                                          要求在Action中有一个getContentStream名字的方法 sInputStream类型的返回值类型 -->
    
                                                            <param name="inputName">contentStream</param>
    
                                                            <param name="contentCharSet">utf-8</param>
    
     
    
                                              </result>
    
                                </action>
    
                  </package>
    
                  <package name="jsonPk" namespace="/json" extends="json-default">
    
                                <action name="json" class="com.clc.Action.JsonAction">
    
     
    
                                              <result type="json">
    
                                                            <!-- 调用message的getter方法:getMessage()方法 -->
    
                                                            <!-- json的结果类型会将对象转换为json字符串,返回给客户端 -->
    
                                                            <param name="root">message</param>
    
                                              </result>
    
                                </action>
    
                  </package>
  • 相关阅读:
    探索ASP.NET MVC5系列之~~~3.视图篇(下)---包含常用表单和暴力解猜防御
    探索ASP.NET MVC5系列之~~~2.视图篇(上)---包含XSS防御和异步分部视图的处理
    SVN:Previous operation has not finished; run 'cleanup' if it was interrupted
    探索ASP.NET MVC5系列之~~~1.基础篇---必须知道的小技能
    MVC:The name 'Scripts' does not exist in the current context
    Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property
    Dapper扩展之~~~Dapper.Contrib
    AutoFac在项目中的应用
    群福利:百度云管家-本地SVIP
    【声明】前方不设坑位,不收费!~ 我为NET狂官方学习计划
  • 原文地址:https://www.cnblogs.com/chenglc/p/6923232.html
Copyright © 2020-2023  润新知