STRUTS2 Support for Ajax

Source: Internet
Author: User

First, Introduction
Struts2 is really a great MVC framework. This section describes Struts2 's support for Ajax.There are two ways to implement Ajax, one is to use native JavaScript code, one is to use a third-party AJAX framework (Jquery,dwr,dojo, etc.), and jquery and DWR are relatively simple and often used when doing projects. Instead of inventing a new AJAX framework, STRUTS2 uses two more popular frameworks, the Dojo framework and the DWR framework.
Dojois an open source DHTML Toolkit implemented in JavaScript language,is a client-side AJAX framework, is a JS tool set. STRUTS2 provides a number of dojo-based tags that support AJAX functionality.
The DWR framework is the abbreviation for direct a server-side AJAX framework。 He contains a server-side Java class Library, a Dwrservlet, and a JavaScript library.
Struts2 's integration with Ajax is largely dependent on Ajax-themed (theme) implementations. Struts2 A total of three themes: simple, XHTML, Ajax. The default is the XHTML theme, the Ajax theme is an extension of XHTML, and the AJAX features are added.
Second, STRUTS2 integrated dojo steps

1th Step: Add Jar Pack Struts2-dojo-plugin-
2nd step: Introducing the Tag Library (JSP code)
<%@ taglib uri= "/struts-tags" prefix= "s"%>
<%@ taglib uri= "/struts-dojo-tags" prefix= "SX"%>

3rd Step: Introduction of header information (JSP code)
<!--Introducing header information--

A, the function of FCK
<!--using TEXTAREA label--
<sx:textarea name= "Intro" rows= "Ten" cols= "label=" "Introduction" ></sx:textarea>

B, automatic prompt time function
<!--time Labels--
<sx:datetimepickername= "Birth" ></sx:datetimepicker>

C, the function of the tree
<sx:tree label= "City" "id=" >
<sx:treenode label= "Beijing" id= "BJ" >
<sx:treenode label= "Chaoyang" id= "Cy" ></sx:treenode>
<sx:treenode label= "Haidian" id= "Cy" ></sx:treenode>
<sx:treenode label= "Changping" id= "Cy" ></sx:treenode>
<sx:treenode label= "Hebei" id= "BJ" >
<sx:treenodelabel= "Shijiazhuang" ></sx:treenode>
<sx:treenodelabel= "Baoding" ></sx:treenode>
<sx:treenode label= "Zhangjiakou" >
<sx:treenode label= "Vian County" >
<sx:treenodelabel= "Wanquan County" ></sx:treenode>
<sx:treenodelabel= "Shangyi County" ></sx:treenode>
<sx:treenodelabel= "Zhangbei" ></sx:treenode>
<sx:treenode label= "Henan" id= "BJ" ></sx:treenode>

D, automatic prompt function
<!--autocompleter Auto-complete
<sx:autocompleter name= "SLE" list= "{' abc ', ' ABCD ', ' abcde ', ' abcdef ', ' ABCDEFG '}" >

Struts2 Integrated DWR step (

DWR Comprehensive Analysis: )

DWR consists of 2 main parts:
(1) A javaservlet running on the server side, which processes the request and sends back a response to the browser.
(2) JavaScript running on the browser, which sends the request and can also dynamically update the Web page.
DWR works by dynamically turning Java classes into JavaScript. Its code is like Ajax magic, and you feel like the invocation is happening on the browser side, but actually the code call takes place on the server side, and DWR is responsible for the data transfer and transformation. This remote invocation of functionality from Java to JavaScript makes DWR a regular RPC mechanism that is much like RMI or soap, and the advantage of DWR is that it does not require any Web browser plugins to run on a Web page.

Download and configure DWR

1) Load the DWR jar package in the Web application

Currently 2.0 not supported STRUTS2

If you are using a dwr2.0 jar package, you also need to import both Log4j.jar and Commons-loggin.jar packages

2) Configure the Web. xml File(Configure DWR's core servlet, which exposes the server-side Java methods)
<!--DWR's core servlet--> configured in Web. xml

3) in the Web-inf/lib directory, create the Dwr.xml file. The Java method is exposed, but the specific methods to be exposed require that the exposed method be configured in the Dwr.xml.
<?xmlversion= "1.0" encoding= "UTF-8"?>

<!--START Snippet:dwr--
"-//getaheadlimited//dtd Direct Web Remoting 1.0//en"
"Http://" >

<create creator= "new" javascript= "Validator" >
<param name= "class" value= "Org.apache.struts2.validators.DWRValidator"/>
<convert converter= "Bean"
Match= "Com.opensymphony.xwork2.ValidationAwareSupport"/>

<! [cdata[
Import Java.util.Map;
Import Org.apache.struts2.validators.DWRValidator;

Dwrvalidator.dopost (String, String, map<string,string>);
In the Dwr.xml configuration file, the Org.apache.struts2.validators.DWRValidator class is created as a JavaScript object, named Validator. The DWR framework provides a way to convert a method that calls a Dwrvalidator instance when the client calls the validator method.

4) Set up the foreground JSP landing page
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
String path = Request.getcontextpath ();
String BasePath =request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01transitional//en" >
<basehref= "<%=basePath%>" >

<title>my JSP ' index.jsp ' startingpage</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is MyPage" >
<link rel= "stylesheet" type= "Text/css" href= "Styles.css" >

<s:headtheme= "ajax"/> <-- can also be placed on the head or anywhere--
<s:form method= "POST" action= "RS"validate= "true" Theme= "Ajax ">
<s:textfield label= "User name" name= "username"/>
<s:password label= "password" name= "password" ></s:password>
<s:textfield label= "Age" name= "ages" ></s:textfield>
<s:submit value= "Register"/>
Note that you need to set the form as an Ajax theme and set validate= "true". When an input component loses focus, the system is responsible for sending the input to the server side for validation.

5) Create action class

@SuppressWarnings ("Serial")
public class ReAction extends Actionsupport {
Privatestring username;
Privatestring Userpass;
Private intage;
Public Intgetage () {
Public Voidsetage (int.) {
This.age =age;
Publicstring GetUserName () {
Public Voidsetusername (String username) {
This.username = Username;
Publicstring Getuserpass () {
Public Voidsetuserpass (String userpass) {
This.userpass = Userpass;
Publicstring execute () throws exception{

6) Establish the corresponding Struts.xml file
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apachesoftware foundation//dtd Struts Configuration 2.0//en"
"Http://" >
<constant name= "Struts.devmode" value= "true" ></constant>
<constant name= "struts.il8n.encoding" value= "GBK" ></constant>
<package name= "GE" extends= "struts-default" >
<actionname= "rs" class= "Com.zhuxuli.action.ReAction" >
<resultname= "Input" >/index.jsp</result>
<resultname= "Success" >/success.jsp</result>

7) Now a complete landing process is finished, and the last thing is to configure a verification file under the action package
Note that this process is a user login Ajax authentication, the file name is: "Action class name-validation.xml"
<! DOCTYPE validators Public
"-//opensymphony group//xwork Validator 1.0.2//en"
"Http://" >

<field name= "username" >
<field-validatortype= "Requiredstring" >
<message> user name cannot be empty </message>
<field name= "Userpass" >
<field-validatortype= "Requiredstring" >
<message> password cannot be empty </message>
</field><fieldname= "Age" >
<field-validator type= "int" >
<paramname= "Min" >1</param>
<paramname= "Max" >100</param>
<message> age must be between 1 and 100 </message>

now a complete dwr in struts2 validation mode has been successfully

STRUTS2 Support for Ajax

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: 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.