[EXTJS5 Study notes] 32nd section Sencha ExtJS 5 and STRUTS2 Ajax interaction Configuration

Source: Internet
Author: User

This address: http://blog.csdn.net/sushengmiyan/article/details/43487751

This article Sushengmiyan

--------------------------------------------------------------------------------------------------------------- ---------------------


Sencha ExtJS 5 Adds a struts2 configuration that allows you to interact with the background to get the data displayed after setting up the foreground layout. One problem is that Struts2 's jump action support for URLs is good, but for Ajax requests like ExtJS, the implementation of a URL-free jump requires a little bit of finesse. This article explains a Ext.Ajax.request request instance and returns the results after the background processing.


Step one: Create the struts2 configuration file Struts.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!  DOCTYPE struts public      "-//apache software foundation//dtd struts Configuration 2.0//en"      "/http Struts.apache.org/dtds/struts-2.0.dtd ">     <struts>         <constant name=" Struts.devmode "value=" true "/>         <package name=" basicstruts2 "extends=" Struts-default ">             <action name=" index ">              < result>/index.jsp</result>          </action>         </package>     

This file needs to be placed in the SRC directory, you can find a file in the download Struts2 WebApp to use directly, do not need their own line of code to knock, after all, we will be able to use.


Step Two: Modify the Web. XML to add support for STRUTS2

<filter>    <filter-name>struts2</filter-name>    <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>  </filter>     <filter-mapping>    <filter-name>struts2</filter-name>     <url-pattern>/*</ Url-pattern>  </filter-mapping>  
This means that struts2 will take over all requests so that our front desk requests will be included in Struts2 's support.


Step three: Add Struts2 core jar packages and struts2-dependent jar packages

The following are some of the required jar packages:

1.commons-fileupload-1.3.1.jar

2.commons-io-2.2.jar

3.commons-lang3-3.2.jar

4.freemarker-2.3.19.jar

5.javassist-3.11.0-ga.jar

6.ognl-3.0.6.jar

7.struts2-core-2.3.20.jar

8.xwork-core-2.3.20.jar

These 8 jar packages can be found in the Struts2 Lib package, copied to the project Lib directory can be, depending on the jar package has been specifically listed before, refer to:

http://blog.csdn.net/sushengmiyan/article/details/43272061


So far, our project has been successfully configured STRUTS2, how to verify success or not? We use ExtJS to write an AJAX request.

Step four: Write the foreground Ajax request

Ext.Ajax.request ({url: ' foo ', async:false,success:function (response) {Debugger;var data = Ext.decode ( Response.responsetext);                                Console.log (data);}});
Here, we use Ext.Ajax.request to send a request and specify a URL of foo which corresponds to an action connection for STRUTS2. I do not like to configure the XML file here, but prefer to use struts2 annotations to implement the jump, so next, add a note plug-in

Step Five Add struts2 Note plugin

Jar Pack Add Struts2-convention-plugin-2.3.20.jar

Step six: Familiarize yourself with the background implementation code

Add your own implementation class in the SRC directory

I've built a new package here, Com.oasystem.action.

Added a class testaction

Package Com.oasystem.action;import Java.io.ioexception;import Java.util.hashmap;import java.util.Map;import Javax.servlet.http.httpservletresponse;import Org.apache.struts2.servletactioncontext;import Org.apache.struts2.convention.annotation.action;import Org.apache.struts2.convention.annotation.result;import Com.opensymphony.xwork2.actionsupport;public class Testaction extends Actionsupport {private static final long Serialversionuid = 5316344216452587235l;private String _dc;//Here is where the struts2 Action call is specified using the annotation @action (value = "/foo")// method is set to void does not require the return data to be written back to response in the public void Foo () {HttpServletResponse response =                Servletactioncontext.getresponse (); Response.setcharacterencoding ("Utf-8");                String abc = "{' A ': ' Test '}"; try {response.getwriter (). write (ABC);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}; Public String get_dc () {return this._dc;}; public void Set_dc (String dc) {this._dc = DC;};}


OK test, start the Tomcat server and execute this AJAX request:


Command line output content:


OK until now, the STRUTS2 has been successfully associated with the Sencha ExtJS. All the backstage can be taken over by the struts2.

[EXTJS5 Study notes] 32nd section Sencha ExtJS 5 and STRUTS2 Ajax interaction Configuration

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.