ajax交互Struts2的action(客戶(hù)端/服務(wù)器端)
更新時(shí)間:2013年08月14日 17:52:46 作者:
本文為大家探討下ajax交互Struts2的action并有客戶(hù)端及服務(wù)器端代碼,感興趣的朋友可以參考下,希望對(duì)大家有所幫助
1.客戶(hù)端網(wǎng)頁(yè)代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>檢測(cè)用戶(hù)名是否唯一</title>
<script language="javascript">
function createRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // 非IE瀏覽器
http_request = new XMLHttpRequest(); //創(chuàng)建XMLHttpRequest對(duì)象
} else if (window.ActiveXObject) { // IE瀏覽器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP"); //創(chuàng)建XMLHttpRequest對(duì)象
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); //創(chuàng)建XMLHttpRequest對(duì)象
} catch (e) {
}
}
}
if (!http_request) {
alert("不能創(chuàng)建XMLHttpRequest對(duì)象實(shí)例!");
return false;
}
http_request.onreadystatechange = getResult; //調(diào)用返回結(jié)果處理函數(shù)
http_request.open('GET', url, true); //創(chuàng)建與服務(wù)器的連接
http_request.send(null); //向服務(wù)器發(fā)送請(qǐng)求
}
function getResult() {
if (http_request.readyState == 4) { // 判斷請(qǐng)求狀態(tài)
if (http_request.status == 200) { // 請(qǐng)求成功,開(kāi)始處理返回結(jié)果
document.getElementById("toolTip").innerHTML = http_request.responseText; //設(shè)置提示內(nèi)容
document.getElementById("toolTip").style.display = "block"; //顯示提示框
} else { // 請(qǐng)求頁(yè)面有錯(cuò)誤
alert("您所請(qǐng)求的頁(yè)面有錯(cuò)誤!");
}
}
}
function checkUser(userName) {
if (userName.value == "") {
alert("請(qǐng)輸入用戶(hù)名!");
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);
}
}
</script>
<style type="text/css">
<!--
#toolTip {
position: absolute;
left: 331px;
top: 39px;
width: 98px;
height: 48px;
padding-top: 45px;
padding-left: 25px;
padding-right: 25px;
z-index: 1;
display: none;
color: red;
background-image: url(images/tooltip.jpg);
}
-->
</style>
</head>
<body style="margin: 0px;">
<form method="post" action="" name="form1">
<table width="509" height="352" border="0" align="center"
cellpadding="0" cellspacing="0" background="images/bg.gif">
<tr>
<td height="54"> </td>
</tr>
<tr>
<td height="253" valign="top">
<div style="position: absolute;">
<table width="100%" height="250" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="18%" height="54" align="right" style="color: #8e6723"><b>用戶(hù)名:</b></td>
<td width="49%"><input name="username" type="text"
id="username" size="32"></td>
<td width="33%"><img src="images/checkBt.jpg" width="104"
height="23" style="cursor: hand;"
onClick="checkUser(form1.username);"></td>
</tr>
<tr>
<td height="51" align="right" style="color: #8e6723"><b>密碼:</b></td>
<td><input name="pwd1" type="password" id="pwd1" size="35"></td>
<td rowspan="2">
<div id="toolTip"></div>
</td>
</tr>
<tr>
<td height="56" align="right" style="color: #8e6723"><b>確認(rèn)密碼:</b></td>
<td><input name="pwd2" type="password" id="pwd2" size="35"></td>
</tr>
<tr>
<td height="55" align="right" style="color: #8e6723"><b>E-mail:</b></td>
<td colspan="2"><input name="email" type="text" id="email"
size="45"></td>
</tr>
<tr>
<td> </td>
<td colspan="2"><input type="image" name="imageField"
src="images/registerBt.jpg"></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
2.服務(wù)器端代碼
Action類(lèi)的代碼
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="用戶(hù)名已經(jīng)被注冊(cè)";
Map map = (Map)ActionContext.getContext().get("request");
map.put("info", "用戶(hù)名已經(jīng)被注冊(cè)");
return "success";
}else{
//info="用戶(hù)名可以注冊(cè)";
Map map = (Map)ActionContext.getContext().get("request");
map.put("info", "用戶(hù)名可以注冊(cè)使用");
return "fail";
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
struts.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myPackage" extends="struts-default">
<!-- 定義action -->
<action name="checkUser" class = "com.action.CheckUserAction" method="findUserByName">
<!-- 定義處理成功后的映射頁(yè)面 -->
<result >/info.jsp</result>
</action>
</package>
</struts>
info.jsp為顯示信息頁(yè)面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%=request.getAttribute("info")%>
info.jsp是jsp頁(yè)面,對(duì)于與安卓客戶(hù)端交互的jsp頁(yè)面而言,盡量略去不必要的html代碼,只需要保留控制編碼格式的代碼和<%%>之間的處理代碼,這樣就避免了在安卓客戶(hù)端顯示不必要的垃圾代碼,且提高了執(zhí)行效率,降低了服務(wù)器負(fù)載。
數(shù)據(jù)庫(kù)截圖:
最終效果圖:
復(fù)制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>檢測(cè)用戶(hù)名是否唯一</title>
<script language="javascript">
function createRequest(url) {
http_request = false;
if (window.XMLHttpRequest) { // 非IE瀏覽器
http_request = new XMLHttpRequest(); //創(chuàng)建XMLHttpRequest對(duì)象
} else if (window.ActiveXObject) { // IE瀏覽器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP"); //創(chuàng)建XMLHttpRequest對(duì)象
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); //創(chuàng)建XMLHttpRequest對(duì)象
} catch (e) {
}
}
}
if (!http_request) {
alert("不能創(chuàng)建XMLHttpRequest對(duì)象實(shí)例!");
return false;
}
http_request.onreadystatechange = getResult; //調(diào)用返回結(jié)果處理函數(shù)
http_request.open('GET', url, true); //創(chuàng)建與服務(wù)器的連接
http_request.send(null); //向服務(wù)器發(fā)送請(qǐng)求
}
function getResult() {
if (http_request.readyState == 4) { // 判斷請(qǐng)求狀態(tài)
if (http_request.status == 200) { // 請(qǐng)求成功,開(kāi)始處理返回結(jié)果
document.getElementById("toolTip").innerHTML = http_request.responseText; //設(shè)置提示內(nèi)容
document.getElementById("toolTip").style.display = "block"; //顯示提示框
} else { // 請(qǐng)求頁(yè)面有錯(cuò)誤
alert("您所請(qǐng)求的頁(yè)面有錯(cuò)誤!");
}
}
}
function checkUser(userName) {
if (userName.value == "") {
alert("請(qǐng)輸入用戶(hù)名!");
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);
}
}
</script>
<style type="text/css">
<!--
#toolTip {
position: absolute;
left: 331px;
top: 39px;
width: 98px;
height: 48px;
padding-top: 45px;
padding-left: 25px;
padding-right: 25px;
z-index: 1;
display: none;
color: red;
background-image: url(images/tooltip.jpg);
}
-->
</style>
</head>
<body style="margin: 0px;">
<form method="post" action="" name="form1">
<table width="509" height="352" border="0" align="center"
cellpadding="0" cellspacing="0" background="images/bg.gif">
<tr>
<td height="54"> </td>
</tr>
<tr>
<td height="253" valign="top">
<div style="position: absolute;">
<table width="100%" height="250" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td width="18%" height="54" align="right" style="color: #8e6723"><b>用戶(hù)名:</b></td>
<td width="49%"><input name="username" type="text"
id="username" size="32"></td>
<td width="33%"><img src="images/checkBt.jpg" width="104"
height="23" style="cursor: hand;"
onClick="checkUser(form1.username);"></td>
</tr>
<tr>
<td height="51" align="right" style="color: #8e6723"><b>密碼:</b></td>
<td><input name="pwd1" type="password" id="pwd1" size="35"></td>
<td rowspan="2">
<div id="toolTip"></div>
</td>
</tr>
<tr>
<td height="56" align="right" style="color: #8e6723"><b>確認(rèn)密碼:</b></td>
<td><input name="pwd2" type="password" id="pwd2" size="35"></td>
</tr>
<tr>
<td height="55" align="right" style="color: #8e6723"><b>E-mail:</b></td>
<td colspan="2"><input name="email" type="text" id="email"
size="45"></td>
</tr>
<tr>
<td> </td>
<td colspan="2"><input type="image" name="imageField"
src="images/registerBt.jpg"></td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
2.服務(wù)器端代碼
Action類(lèi)的代碼
復(fù)制代碼 代碼如下:
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="用戶(hù)名已經(jīng)被注冊(cè)";
Map map = (Map)ActionContext.getContext().get("request");
map.put("info", "用戶(hù)名已經(jīng)被注冊(cè)");
return "success";
}else{
//info="用戶(hù)名可以注冊(cè)";
Map map = (Map)ActionContext.getContext().get("request");
map.put("info", "用戶(hù)名可以注冊(cè)使用");
return "fail";
}
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
struts.xml配置
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myPackage" extends="struts-default">
<!-- 定義action -->
<action name="checkUser" class = "com.action.CheckUserAction" method="findUserByName">
<!-- 定義處理成功后的映射頁(yè)面 -->
<result >/info.jsp</result>
</action>
</package>
</struts>
info.jsp為顯示信息頁(yè)面
復(fù)制代碼 代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%=request.getAttribute("info")%>
info.jsp是jsp頁(yè)面,對(duì)于與安卓客戶(hù)端交互的jsp頁(yè)面而言,盡量略去不必要的html代碼,只需要保留控制編碼格式的代碼和<%%>之間的處理代碼,這樣就避免了在安卓客戶(hù)端顯示不必要的垃圾代碼,且提高了執(zhí)行效率,降低了服務(wù)器負(fù)載。
數(shù)據(jù)庫(kù)截圖:
最終效果圖:
您可能感興趣的文章:
- 詳解在Java的Struts2框架中配置Action的方法
- struts2 action跳轉(zhuǎn)調(diào)用另一個(gè)程序
- struts2中action實(shí)現(xiàn)ModelDriven后無(wú)法返回json的解決方法
- Struts2中Action中是否需要實(shí)現(xiàn)Execute方法
- 用js模擬struts2的多action調(diào)用示例
- 在Action中以Struts2的方式輸出JSON數(shù)據(jù)的實(shí)例
- Struts2之Action接收請(qǐng)求參數(shù)和攔截器詳解
- Struts2 ActionContext 中的數(shù)據(jù)詳解
- struts2通過(guò)action返回json對(duì)象
- Struts2學(xué)習(xí)教程之Action類(lèi)如何訪問(wèn)WEB資源
相關(guān)文章
ajax實(shí)現(xiàn)改變狀態(tài)和刪除無(wú)刷新的實(shí)例
下面小編就為大家分享一篇ajax實(shí)現(xiàn)改變狀態(tài)和刪除無(wú)刷新的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Ajax serialize() 表單進(jìn)行序列化方式上傳文件
這篇文章主要介紹了Ajax serialize() 表單進(jìn)行序列化方式上傳文件的相關(guān)資料,需要的朋友可以參考下2017-04-04
用ajax xml的數(shù)據(jù)讀取的HelloWorld程序
我們經(jīng)常會(huì)使用JavaScript實(shí)現(xiàn)動(dòng)態(tài)的改變div里面的內(nèi)容,尤其是使用ajax的時(shí)候,尤為重要。2009-04-04
Ajax中post方法直接返回以0開(kāi)頭數(shù)字出錯(cuò)問(wèn)題分析
這篇文章主要介紹了Ajax中post方法直接返回以0開(kāi)頭數(shù)字出錯(cuò)問(wèn)題分析,需要的朋友可以參考下2017-03-03
使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟
這篇文章主要介紹了使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
jQuery通過(guò)Ajax向PHP服務(wù)端發(fā)送請(qǐng)求并返回JSON數(shù)據(jù)
這篇文章主要介紹了jQuery通過(guò)Ajax向PHP服務(wù)端發(fā)送請(qǐng)求并返回JSON數(shù)據(jù),設(shè)計(jì)到的知識(shí)點(diǎn)有jquery、ajax、php、json,感興趣的朋友一起學(xué)習(xí)下jquery ajax 返回json2015-10-10
asp.net 全部選中與取消操作,選中后的刪除(ajax)實(shí)現(xiàn)無(wú)刷新效果
現(xiàn)在我們?cè)诰幊痰臅r(shí)刻總是要利用一些最新的技術(shù)去解決問(wèn)題。。。。下面是我用ajax與jequery結(jié)合在一起使用的一個(gè)實(shí)例。希能給一起學(xué)習(xí)的朋友們帶來(lái)幫助。2009-06-06

