Text instrument An example of the implementation of struts2+jquery implementation of Ajax landing method, the following steps:
First, create a new Web project, name test. Configure the STRUTS2 environment and import jquery's JS files to the project.
Second, under the Com.action package, a new Loginaction.java
Loginaction.java's code is as follows
Package com.action;
Import org.apache.struts2.convention.annotation.Action;
Import Org.apache.struts2.convention.annotation.ParentPackage;
Import Org.apache.struts2.convention.annotation.Result;
Import Org.apache.struts2.convention.annotation.Results;
Import Com.opensymphony.xwork2.ActionSupport; @Action ("Login") @ParentPackage (value = "Json-default") @Results ({@Result (name = "Success", type = "json", params = {"Da Ta "," Flag "}), public class Loginaction extends Actionsupport {/** * */private static final long Serialv
Ersionuid = 1751244794407005783L;
Private String flag;
Private String username;
private String password; Public String Execute () {try {if (getusername () = null | | getusername (). Trim (). Equals ("")) {Setflag (
"User name cannot be empty");
return SUCCESS;
else if (getpassword () = null | | GetPassword (). Trim (). Equals ("")) {Setflag ("Password cannot be blank");
return SUCCESS;
else if (getusername (). Trim (). Equals ("admin") && GetPassword (). Equals ("admin")) {Setflag ("login succeeded");
return SUCCESS;
else {setflag ("User name or password error");
return SUCCESS;
} catch (Exception e) {e.printstacktrace ();
Setflag ("Landing anomaly");
return SUCCESS;
} public String Getflag () {return flag;
} public void Setflag (String flag) {This.flag = flag;
Public String GetPassword () {return password;
} public void SetPassword (String password) {this.password = password;
Public String GetUserName () {return username;
} public void Setusername (String username) {this.username = username;
}
}
Iii. new index.jsp and success.jsp in the Webroot catalogue
Index.jsp for landing interface, success.jsp for landing after the successful jump interface.
Index.jsp contents are as follows:
<%@ 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" >