Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the action of ajax interactive Struts2

Shulou Source: shulou.com Published: 2022-06-03 07:08:34 11月03日 Update

Editor to share with you how to achieve ajax interactive Struts2 action, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Client web page code

The code is as follows:

Check whether the user name is unique

Function createRequest (url) {

Http_request = false

If (window.XMLHttpRequest) {/ / non-IE browser

Http_request = new XMLHttpRequest (); / / create XMLHttpRequest object

} else if (window.ActiveXObject) {/ / IE browser

Try {

Http_request = new ActiveXObject ("Msxml2.XMLHTTP"); / / create XMLHttpRequest object

} catch (e) {

Try {

Http_request = new ActiveXObject ("Microsoft.XMLHTTP"); / / create XMLHttpRequest object

} catch (e) {

}

}

}

If (! http_request) {

Alert ("cannot create XMLHttpRequest object instance!")

Return false

}

Http_request.onreadystatechange = getResult; / / call the result handling function

Http_request.open ('GET', url, true); / / create a connection to the server

Http_request.send (null); / / send a request to the server

}

Function getResult () {

If (http_request.readyState = = 4) {/ / determine the request status

If (http_request.status = = 200) {/ / the request was successful. Start processing the returned result

Document.getElementById ("toolTip") [xss_clean] = http_request.responseText; / / set prompt content

Document.getElementById ("toolTip") .style.display = "block"; / / Show prompt box

} else {/ / request page error

Alert ("the page you requested has an error!")

}

}

}

Function checkUser (userName) {

If (userName.value = "") {

Alert ("Please enter a user name!")

UserName.focus ()

Return

} else {

/ / createRequest ('http://10.65.9.181:8090/ajax/checkUser.jsp?user='+userName.value);

CreateRequest ('http://10.65.9.181:8090/ajax/checkUser.action?user='

+ userName.value)

}

}

User name:

Password:

Confirm password:

E-mail:

two。 Server-side code

Code for the Action class

The copy code is as follows:

Package com.action

Import java.util.Map

Import com.opensymphony.xwork2.ActionContext

Import com.opensymphony.xwork2.ActionSupport

Import com.xzy.UserDAO

Public class CheckUserAction extends ActionSupport {

Private String user

Public String findUserByName () {

String info = null

UserDAO userdao = new UserDAO ()

If (userdao.findUserByName (user)) {

/ / info= "user name has been registered"

Map map = (Map) ActionContext.getContext () .get ("request")

Map.put ("info", "user name has been registered")

Return "success"

} else {

/ / info= "user name can be registered"

Map map = (Map) ActionContext.getContext () .get ("request")

Map.put ("info", "user name can be registered")

Return "fail"

}

}

Public String getUser () {

Return user

}

Public void setUser (String user) {

This.user = user

}

}

Struts.xml configuration

The code is as follows:

/ info.jsp

Info.jsp is to display the information page

The code is as follows:

Info.jsp is a jsp page, for jsp pages that interact with Android clients, try to omit unnecessary html code, and only need to keep the code that controls the coding format and the processing code between them, so as to avoid displaying unnecessary junk code on Android client, improve execution efficiency and reduce server load.

Database screenshot:

The above is all the contents of the article "how to implement the action of ajax interactive Struts2". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Code user name page object server service content client client article processing password browser result error Android prompt browse success Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS Docker vpn MySQL