Struts2.5 use Ajax to pass json data to the JSP instance, struts2.5json

Source: Internet
Author: User

Struts2.5 use Ajax to pass json data to the JSP instance, struts2.5json

AJAX + JSON = "JSP"

AJAX

AJAX is a technology that updates some webpages without the need to reload the entire webpage.

By performing a small amount of data exchange with the server in the background, AJAX can implement asynchronous updates on webpages. This means that you can update a part of a webpage without reloading the entire webpage.

If you need to update the content of a traditional web page (without AJAX), you must reload the entire web page.

JSON

JSON (JavaScript Object Notation, JS Object tag) is a lightweight data exchange format. Based on a subset of ECMAScript (w3c js specification), It stores and represents data in a text format completely independent of programming languages. The concise and clear hierarchy makes JSON an ideal data exchange language. Easy to read and write, easy to parse and generate machines, and effectively improve network transmission efficiency.

Use Ajax to pass json data to JSP

Step 1:Create a json data in your Action

Public class AjaxAction extends ActionSupport {/*****/private static final long serialVersionUID = 1L; private String result; public String getResult () {return result ;} public void setResult (String result) {this. result = result;} public String getjson () {JSONObject json = new JSONObject (); json. put ("name", "zhangsan"); json. put ("password", "123456"); result = json. toString (); return SUCCESS ;}

Step 2:Compile the content in the struts. xml file

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.5 // EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <! -- You do not need to restart the server to change the name in action in developer mode. --> <constant name = "struts. devMode" value = "true"> </constant> <! -- Only the suffix (. do), the value can be modified by yourself --> <constant name = "struts. action. extension "value =" do "> </constant> <! -- Enable dynamic method call --> <constant name = "struts. enable. dynamicMethodInvocation "value =" true "/> <package name =" default "extends =" json-default "namespace =" "> <action name =" ajax "class =" com. action. ajaxAction "method =" getjson "> <result name =" success ">/ajaxjson. jsp </result> <result type = "json"> <! -- The attribute to be serialized by Struts2 is specified here, this property must have the corresponding getter method in action --> <param name = "root"> result </param> </result> </action> </package> </ struts>

For actions that use Struts2 to serialize objects to JSON, the package must inherit from json-default. Note that the unique result here does not specify the name attribute. Parm setting name = "root" indicates local refresh. The root directory page is returned.

Step 3:First, let's first understand the principles of Ajax: this is done with jQuery, and we need to introduce js

Function testCheck (num) {$. ajax ({type: "post", // submission Method url: "TestCheckServlet", // submission address async: true, // asynchronous request dataType: "html ", // return type data: {"num": num}, // pass the previous value success: function (data, textStatus) {// Method for successful execution var json = eval ("(" + data + ")"); // convert the transmitted data to json, you can also transmit the json data to alert (json. name) ;}, error: function () {// method of failed execution alert ("error ");}})}

There are still many methods for passing JSON values. You need to mine them a little bit.

The above Struts2.5 example of Using Ajax to pass json data to JSP is all the content shared by Alibaba Cloud. I hope it can be used as a reference and support for the customer's house.

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.