Interaction of JSON data between Ajax and action--including list data transfer

Source: Internet
Author: User

Be afraid to forget later, as a note, the explanation is contained in the code. Note: The Jquery Ajax used here 1. Pass the basic data type

Useraction

Package action;

Import java.io.IOException;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Com.opensymphony.xwork2.ActionSupport;

The public class Useraction extends actionsupport{
    /**
        The value of the attribute here, consistent with the request parameters of the foreground Ajax, parameters are requested by the structs default interceptor To intercept and pass in through the set method.
    /private String value;//the properties of the background are consistent with the data names that Ajax sends, and
    the value returned by the set get private string url;//has a getting method--for Ajax acquisition Public
    String GetValue () {return
        value;
    }
    public void SetValue (String value) {
        this.value = value;
    }
    Public String test1 () {
        url= "test1";
        System.out.println (value);
        return SUCCESS;
    }

Structs2.xml
The so-called JSON is a key-value form of numerical format, a lightweight data interchange format.

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
    <package name=" Actions "namespace="/"extends=" Json-default ">
    <action name= "user" class= "action. Useraction "method=" test1 ">
        <!--<result name=" Success ">/1.jsp</result>-->
        <!-- In Stucts2, type= "JSON" is used to make AJAX requests, so there is no jump page at all, and JSON is a data format that turns all the variables in the action into JSON to return the page-->
        <result Name= "Success" type= "JSON" >  

         </result>
    </action>
    </package>
</struts >    

JSP: Code-

<%@ page language= "java" import= "java.util.*" 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" >  

Results:
Background received data:

Foreground display background returned data
2. Passing entity Objects

Basic as above:

public class Orderaction extends Actionsupport {
    Private order = New Order ();

    Public execute () {
        //Here the order for content assignment
        //order.setordername () ...;
        Return success
    }
}

The XML file returns the class and method that is consistent with the basic type above, indicating the action, and returns the JSON type.

Ajax:

$.ajax ({
                     type: "Get",
                     URL: "User.action",
                     data: {value: "MSG"},//form is Key-value
                     dataType: "JSON",
                     success:function (data) {
                  //through access to the order entity, and then access to the inside property value
                        alert (data.order.orderName);

                          },
                     error: function (ERR) {
                                alert (err);
                        }

                     });
             
3. Visit List

Front desk:
List is read through jquery loop

$.ajax ({
                     type: "Get",
                     URL: "Getproducts.action",
                     data: {msg: "Fashion Menswear"},
                     DataType: "JSON",
                     success : function (data) {
                     //alert ("return succeeded");
                        Here for Data.list  data is an
object//data.list for loops, index is the subscript for the loop, and content is the object of each loop.              $.each (data.list,function (index,content) {
                     alert (content.name);                     
                            });
                          ,
                     error:function ( ERR) {alert ("Erro");}});
                     

The XML file is the same as Example 1.

Background:

Private list<products> list;//is used to return the List array,

set method .... Get

method ....
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.