In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.