In the Struts 2 support the use of JSON Ajax

2010-06-22  来源:本站原创  分类:Java  人气:200 

JSON plugin provides a service called json of ResultType, once for a specified a type of Action for the json of Result, then the Result of resources without mapping to any view. Will be downloaded to the jsonplugin-0.7.jar file to the Web application's WEB-INF path to complete the JSON plug-in installation.
JSON plugin provides a service called json of ResultType, once for a specified a type of Action for the json of Result, then the Result of resources without mapping to any view. Because JSON plug-in will be responsible for the state of Action message sequence data into JSON format and the data returned to the client page, JavaScript
Simply put, JSON plug-in allows us to asynchronous JavaScript call Action, and Action View of resources no longer need to use to display status information in the Action, but by the JSON plugin is responsible for Action in the status information returned to the calling page - In this way, you can complete Ajax interactions.
Struts2 provides a pluggable way to manage plug-in, install the JSON Struts2 plug and install plug-in and not much general difference as only need to plug Struts2 JAR file to the Web application's WEB-INF/lib path can.
JSON plugin installed by following these steps:
(1) landing http://code.google.com/p/jsonplugin/downloads/list site, download the JSON plugin Struts2 the latest version of the current latest version is 0.7, we can download the version of the JSON plugin.
(2) will be downloaded to the jsonplugin-0.7.jar file to the Web application's WEB-INF path to complete the JSON plug-in installation.
Achieve Actio logic <br /> assumptions wo, en input page includes three form fields, the three form fields for the three request parameters should be used to encapsulate the three Action request parameters. The name of three form fields were field1, field2 and field3

Action to deal with the request class code is as follows:

public class JSONExample
  {
  // Package request parameters of three properties
  private String field1;
  private transient String field2;
  private String field3;
  // Encapsulates the results of processing of properties
  private int[] ints = {10, 20};
  private Map map = new HashMap();
  private String customName = &quot;custom&quot;;
  // Three request parameters corresponding setter and  getter Method
  public String getField1()
  {
  return field1;
  }
  public void setField1(String field1)
  {
  this.field1 = field1;
  }
  // Here omitted the field1 and  field2 Two fields of setter and  getter Method
  ...
  // Encapsulates the results of processing the setter of the property and  getter Method
  public int[] getInts()
  {
  return ints;
  }
  public void setInts(int[] ints)
  {
  this.ints = ints;
  }
  public Map getMap()
  {
  return map;
  }
  public void setMap(Map map)
  {
  this.map = map;
  }
  // Use the comment syntax to change this attribute to serialize a property name after
  @JSON(name=&quot;newName&quot;)
  public String getCustomName()
  {
  return this.customName;
  }
  public String execute()
  {
  map.put(&quot;name&quot;, &quot;yeeku&quot;);
  return Action.SUCCESS;
  }
  }

In the above code, the use of the JSON annotation, comment, domain name is specified, name field specify the Action property to be serialized into JSON object property name. In addition, JSON annotation also supports the following several domains:

serialize: settings are serialized to the property deserialize: Set whether the property to deserialize.
format: Set the output for formatting, parsing the date format of form fields. For example, "yyyy-MM-dd'T'HH: mm: ss".
Configure the Action and configuration there is little difference between normal Action, Action should be configured for the type of json of Result. And this Result without any view of resource allocation.

Configuring the Action of struts.xml file code is as follows:




 





In the above configuration file there are two notable areas:

The first place is to configure struts.i18n.encoding constant, is no longer using GBK encoding, but UTF-8 encoding, because Ajax, POST requests are the way to UTF-8 encoded.

The second place is the configuration package, your package inherited json-default package, rather than inherit the default default package, this is because only under the package have json type Result.

相关文章
  • In the Struts 2 support the use of JSON Ajax 2010-06-22

    JSON plugin provides a service called json of ResultType, once for a specified a type of Action for the json of Result, then the Result of resources without mapping to any view. Will be downloaded to the jsonplugin-0.7.jar file to the Web application

  • Struts 2 in the use of JSON Ajax support 2009-08-20

    JSON plugin provides a json called the ResultType, once designated for a certain type of Action for the json of Result, without the Result of resources mapped to any view. JSON plugin because Action will be responsible for the status information in t

  • Struts 2 and AJAX 2009-05-16

    In today's - Web 2.0 concept of overwhelming Internet environment, easy AJAX integration for a successful WEB framework is indispensable. Therefore, Struts 2 one of the essential functions of a (Feature) is the "First-class AJAX support - Add interac

  • Conversion between JSON and JAVA (struts Ajax) 2010-04-24

    JSON (JavaScript Object Notation) is a lightweight data interchange format, which is based on a subset of JavaScript, often used with ajax, using ajax to load data. json ajax end of the configuration steps: 1. Guide package. Using json jar package of

  • Configuration transaction: struts with spring's two integrated approach 2010-12-12

    Uses two configuration files: applicationContext.xml and hibernate.cfg.xml 1. New web project 2. Add support for hibernate support, and spring (the order can be reversed) 3. Use the reflection tool support for database operations 4. Write dao interfa

  • Struts 2 and AJAX (Part III) 2010-12-19

    http://www.blogjava.net/max/archive/2007/08/16/137363.html BLOG has not been updated for a long time, the company's projects for some time before the busy, and I still friends with a publisher to write about the book thing, so have no time to complet

  • The ajax in struts 2009-08-16

    AJAX is an acronym for Asynchronous JavaScript and XML. Essentially, a JavaScript can make a HTTP request and update portions of a page directly, without going through a conventional POST or GET and refreshing the entire page. Better yet, a page can

  • JSON example (AJAX + STRUTS) 2009-10-23

    Some time ago to do a project using json, I take the time to write a struts + ajax + json examples. Ajax + json personal feel to a large extent reduces network and server IO, is a very good combination! 1: json the lib I'm using json-lib-2.1-jdk15.ja

  • JSON examples (AJAX + STRUTS) 2010-04-06

    JSON examples (AJAX + STRUTS) A project some time ago used the json, today I take the time to write a struts + ajax + json examples. Personal feeling ajax + json largely reduced network and server IO, is a very good combination! 1: json in the lib I'

  • PHP + ajax associating the drop-down selection menu, IE + Firefox browser support 2011-04-12

    PHP + ajax associating the drop-down selection menu, IE + Firefox browser support Product search and product releases have two or even multi-level classification, as well as search and background classification of the article published two times, can

  • struts,ajax出现乱码的解决方法 2013-12-24

    乱码问题好像跟我们中国程序员特别有缘,一直困扰着我们,从开始的JSP乱码问题,STRUTS乱码问题,到现在的AJAX乱码问题,无一不是搞得许多程序员焦头烂额的,整天骂XXX产品对中文支持不了,UTF-8无法使用中文啊什么的,其实这里面被骂的产品中其实99%以上是对中文支持非常好的,而出现乱码的原因只是因为自身对国际化支持以及文件编码等信息的认识不知造成的.要知道一个产品那么流行,怎么可能对中文支持不了呢,下面就开始一一帮大家解决这些问题. 1 .编码 -- 想要解决好中文问题,对编码肯定是不能一

  • Spring jar package Detailed 2009-02-28

    AspectJ directory are in the Spring framework to use AspectJ source code and test program files. AspectJ is the first java application framework provided by the AOP. dist directory is a Spring release package, regarding release package described belo

  • spring of the jar package Detailed 2009-03-02

    Excerpt from: http://blog.csdn.net/exceljava/archive/2007/10/05/1811925.aspx Download the spring package of documents and a variety of packages large projects are often at only some of which we must, if not sure what package when necessary, then take

  • SSH configuration file architecture 2009-05-20

    SSH (Struts1.3 + Spring2.0 + Hibernate3.1) architecture configuration document briefly Contents 1. 2 Runtime Environment 2. Configuration 2 2.1 New Project 2 2.2 Add Struts framework supports 3 (1) add the Struts package and the setting up of the pat

  • Detailed packet Spring Jar 2009-06-04

    Yes there is a complete spring.jar release contains a single jar package, spring.jar contains spring-mock.jar except in the content contained in all other contents of jar package, because only in the development environment will be used next spring-m

  • SSH package to develop the basis of structure-Spring Help 2009-08-08

    Understand the package structure of Spring release will help us reduce the size of packets, but also help to increase understanding of Spring framework. spring.jar is released module contains a complete package of a single jar. But does not include m

  • Spring2.0 compressed package directory Description 2010-04-13

    1, Spring2.0 compressed package directory Description aspectj directory in the Spring framework to use aspectj source code and test program files. Aspectj is the first java application framework for the provision of AOP. dist directory is a Spring re

  • spring jar package instructions 2010-04-13

    1, Spring2.0 compressed package directory Description aspectj directory in the Spring framework to use aspectj source code and test program files. Aspectj is the first java application framework for the provision of AOP. dist directory is a Spring re

  • Spring2.0 jar package 2010-04-24

    1, Spring2.0 compressed package directory Description aspectj directory in the Spring framework to use aspectj source code and test program files. Aspectj is the first java application framework for the provision of AOP. dist directory is a Spring re

  • Liferay 5.2.3 development environment to build 2010-05-02

    Liferay 5.2.3 development environment to build Prepared by: recky Version: 1.1 E-mail: [email protected] Date: 2010-03-04 1. About Liferay Liferay is a complete portal solution, based on J2EE applications, using technologies such as EJB and JMS, part o