STRUTS1.2+AJAX
系統
1646 0
在一個頁面上有兩個DIV,DIV1和DIV2,在DIV1中有一棵樹,在DIV2中有一個struts ? form,在struts ? form中有一個文本框和一個提交按鈕,我想實現如下功能:在文本框中寫上信息,然后按提交按鈕,提交按鈕會調用struts的action,將文本框中的數據寫入數據庫中,然后DIV2中的struts ? form刷新為初始狀態,我想知道如何實現頁面DIV2的刷新而DIV1頁面保持不變?有代碼可以參考嗎?
-----div ? ? ? 1------------ ? ? ? ? ? ? ? ? ------div ? ? 2------------------------
| ? ? ? ? tree ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? | ? ? ? ? ------struts ? form--------------|
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? | ? ? ? | ? ? ? ? text ? area ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? | ? ? ? | ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? button ? ? ? ? ? ? ? ? ? ? |
| ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? | ? ? ? --------------------------------|
------------------------- ? ? ? ? ? ? ? ? -----------------------------------
|
?
|
?
|
問題點數:
100
?回復次數:
6
顯示所有回復顯示星級回復顯示樓主回復
修改
刪除
舉報
引用
回復
|
加為好友
發送私信
在線聊天
-
shenglijay
-
-
等級:
-
可用分等級:中農
-
總技術專家分:1253
-
總技術專家分排名:16177
|
發表于:
2007-02-08 08:50:00
2
樓?得分:
0
|
點擊button,通過觸發onclick事件。寫點ajax代碼應該可以實現的。通過你的js代碼來提交給action.在通過響應信息來刷新你的form
|
?
|
<!---->
修改
刪除
舉報
引用
回復
|
|
加為好友
發送私信
在線聊天
-
zhangsc
-
-
等級:
-
可用分等級:富農
-
總技術專家分:56
-
總技術專家分排名:118779
|
發表于:
2007-02-08 11:49:55
3
樓?得分:
0
|
有一點我沒有明白,在通過js來調用struts ? action,action結束后會調用forward,會使整個頁面刷新,我想知道如何實現局部刷新,能給出具體的代碼嗎?
|
?
|
<!---->
修改
刪除
舉報
引用
回復
|
|
加為好友
發送私信
在線聊天
-
shenglijay
-
-
等級:
-
可用分等級:中農
-
總技術專家分:1253
-
總技術專家分排名:16177
|
發表于:
2007-02-08 13:08:37
4
樓?得分:
0
|
你調struts ? action ? 時侯,action ? 里面 ? 最后要返回一個forward對象時, ? 你寫成 ? return ? null; ? 這樣它就會返回到你的頁面。實現的效果就是局部刷新
|
?
|
<!---->
修改
刪除
舉報
引用
回復
|
|
加為好友
發送私信
在線聊天
-
zhangsc
-
-
等級:
-
可用分等級:富農
-
總技術專家分:56
-
總技術專家分排名:118779
|
發表于:
2007-02-08 13:28:50
5
樓?得分:
0
|
是可以這樣做,但是卻沒有使用上Ajax技術,Ajax技術的優勢一點都沒有體現出來
|
?
|
<!---->
修改
刪除
舉報
引用
回復
|
|
加為好友
發送私信
在線聊天
-
shenglijay
-
-
等級:
-
可用分等級:中農
-
總技術專家分:1253
-
總技術專家分排名:16177
|
發表于:
2007-02-08 17:18:29
6
樓?得分:
100
|
頁面:
------------------------------------------------------------
<html>
<head>
<script ? type= "text/javascript ">
var ? http ? = ? getHttpObject();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /* ? 確保在ie的各式版本,以及在firefox或其他瀏覽器下,都有用的xmlhttpRequest ? ? */
function ? getHttpObject(){
var ? xmlhttp ? = ? false;
if(window.XMLHttpRequest){
xmlhttp ? = ? new ? XMLHttpRequest();
if(xmlhttp.overrideMimeType){
xmlhttp.overrideMimeType( 'text/xml ');
? }
}
else{
try{
xmlhttp ? = ? new ? ActiveXObject( "Msxml2.XMLHTTP ");
}catch(e){
try{
xmlhttp ? = ? new ? ActiveXObject( "Microsoft.XMLHTTP ");
}catch(e){
xmlhttp ? = ? false;
}
? ? ? ? }
}
return ? xmlhttp;
}
function ? handleHttpResponse(){
if(http.readyState ? == ? 4){
if(http.status ? == ? 200){
var ? text ? = ? http.responseText;
if(text ? != ? null ? || ? text.length ? > 0)
// ? update ? the ? textarea;
document.getElementById( "input ").value ? = ? text;
}
else{
alert( "error!!! ");
alert(http.status);
}
}
}
function ? getInfo(){
var ? url ? = ? "aaa.do "; ? // ? your ? action ? name
http.open( "GET ",url,true);
http.onreadystatechange ? = ? handleHttpResponse;
http.send(null);
return ? ;
}
</script>
</head>
<body>
<div ? id= "tree " ? style= "width:150px;height:500px;float:left;background-color:limegreen; "> this ? is ? a ? menu ? tree </div>
<div ? id= "form " ? style= "width:820px;height:500px;float:left;background-color:yellow; ">
textArea: <input ? type= "text " ? id= "input "> <br>
<input ? type= "button " ? onclick= "getInfo(); " ? value= "Submit ">
</div>
</body>
</html>
-------------------------------------------
Action
--------------------------------------------
public ? class ? GetMsgAction ? extends ? Action ? {
public ? ActionForward ? execute(ActionMapping ? mapping, ? ActionForm ? form,
HttpServletRequest ? request, ? HttpServletResponse ? response) ? {
response.setContentType( "text/html ");
response.setCharacterEncoding( "utf-8 ");
try ? {
response.getWriter().write( "hello,this ? is ? the ? update ? text!! ");
} ? catch ? (IOException ? e) ? {
e.printStackTrace();
}
// ? should ? be ? return ? null;
return ? null;
}
}
-------------------------------------
大致就是這樣啊,至于你要更新什么,還是從數據庫取什么在更新就自己做吧,我只是做個簡單的例子
|
|
STRUTS1.2+AJAX
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元