Struts 2常用的Ajax标签
生活随笔
收集整理的這篇文章主要介紹了
Struts 2常用的Ajax标签
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Struts 2對(duì)Ajax的支持 ?Struts 2對(duì)Ajax提供了很好的支持 –Struts 2.1提供了基于Dojo的Ajax標(biāo)簽,對(duì)Ajax操作進(jìn)行了進(jìn)步封裝,可以更快捷容易的使用Ajax ?使用Struts 2.1的Ajax標(biāo)簽前必須進(jìn)行如下操作 1)將struts2-dojo-plugin-2.1.x.x.jar復(fù)制到WEB-INF\lib目錄 2)在JSP頁(yè)面中導(dǎo)入Ajax標(biāo)簽 <!-- 引入Ajax標(biāo)簽 --> <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> 3)在JSP頁(yè)面中加入head標(biāo)簽,負(fù)責(zé)在頁(yè)面上導(dǎo)入Dojo所需要的CSS庫(kù)和JavaScript庫(kù) <head><!-- 在JSP頁(yè)面中加入head標(biāo)簽負(fù)責(zé)在頁(yè)面上導(dǎo)入Dojo所需要的CSS庫(kù)和JavaScript庫(kù) --><sx:head /></head>
Struts 2.1的Ajax標(biāo)簽
| 方法名 | 說(shuō)??? 明 |
| <sx:div> | 創(chuàng)建一個(gè)div區(qū)域,可以通過(guò)Ajax向其中加載內(nèi)容,以實(shí)現(xiàn)局部刷新 ? |
| <sx:submit> | 通過(guò)Ajax來(lái)更新某個(gè)元素的內(nèi)容或提交表單 |
| <sx:a> | 通過(guò)Ajax來(lái)更新某個(gè)元素的內(nèi)容或提交表單 |
| <sx:tabbedPanel> | 創(chuàng)建一個(gè)標(biāo)簽面板,由<s:div>來(lái)提供內(nèi)容。 |
| <sx:autocompleter> | 根據(jù)用戶輸入提供輸入建議,或者幫助用戶自動(dòng)完成輸入 |
| <sx:tree> | 創(chuàng)建一個(gè)支持Ajax的樹形組件(Widget) |
div標(biāo)簽例子 ?使用Ajax技術(shù)實(shí)現(xiàn)如下需求: –頁(yè)面有三個(gè)div:div1、div2、div3 –div1的內(nèi)容每隔5秒時(shí)間自動(dòng)更新一次,每隔30分鐘提示用戶休息一下(刷新多次) –開始訪問(wèn)時(shí),在div2中顯示歡迎信息(刷新一次) –整個(gè)訪問(wèn)過(guò)程中div3中內(nèi)容保持不變(無(wú)刷新) 使用<sx:div>標(biāo)簽來(lái)實(shí)現(xiàn)
實(shí)現(xiàn)代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 引入struts2的標(biāo)簽庫(kù) --> <%@ taglib uri="/struts-tags" prefix="s"%> <!-- 引入Ajax標(biāo)簽 --> <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> <%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'ajaxTime.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><!-- 在JSP頁(yè)面中加入head標(biāo)簽負(fù)責(zé)在頁(yè)面上導(dǎo)入Dojo所需要的CSS庫(kù)和JavaScript庫(kù) --><sx:head /></head><body><s:url id="time" value="time.action" /><s:url id="welcome" value="welcome.action" /><!-- 每隔1秒異步訪問(wèn)一次 --><sx:div href="%{time}" updateFreq="1000" /><!-- 只異步訪問(wèn)一次 --><sx:div href="%{welcome}" /><!-- 無(wú)異步訪問(wèn)--><sx:div>初始化的內(nèi)容!</sx:div></body> </html> ajaxTime.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'time.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%//獲得當(dāng)前時(shí)間long currentTime = System.currentTimeMillis();//取出session中存入的現(xiàn)在時(shí)間Long stratTime = (Long) session.getAttribute("currentTime");if (stratTime == null) {//第一次訪問(wèn)session.setAttribute("currentTime", currentTime);} else {//以秒計(jì)算計(jì)算已用時(shí)間long used = (currentTime - stratTime) / 1000;session.setAttribute("used", used);//當(dāng)用戶瀏覽網(wǎng)頁(yè)時(shí)間超過(guò)60秒則提示用戶休息一下。boolean flag = false;if (used > 60) {flag = true;}session.setAttribute("flag", flag);}%><s:if test="#session.flag==true">你該稍微休息一下了。</s:if><s:else>你已經(jīng)訪問(wèn)的時(shí)間:<s:property value="#session.used" default="0" />秒</s:else></body> </html> time.jsp <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > <struts><package name="struts2" extends="struts-default" namespace="/" ><action name="time"><!-- name屬性不寫默認(rèn)success --><result>/time.jsp</result> </action><action name="welcome"><result>/welcome.jsp</result> </action></package> </struts> struts.xml <%@ 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"> <html><head><base href="<%=basePath%>"><title>My JSP 'welcome.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>歡迎你登錄本系統(tǒng)!</body> </html> welcome.jsp submit和a標(biāo)簽例子 ?使用Ajax技術(shù)實(shí)現(xiàn)如下需求: –頁(yè)面有兩個(gè)div,還有一個(gè)超鏈接 –單擊超鏈接后在div1中出現(xiàn)登錄表單 –如果登錄成功,在div2中顯示登錄成功;登錄失敗在div2中顯示登錄失敗 使用<sx:div>標(biāo)簽來(lái)定義div,使用<sx:a>和<sx:submit>定義超鏈接和提交按鈕實(shí)現(xiàn)?
實(shí)現(xiàn)代碼:
package com.entity; /*** 用戶類* @author asus**/ public class Users {/** 屬性 */private String name;private String password;/** 構(gòu)造方法 */public Users() {super();}public Users(String name, String password) {super();this.name = name;this.password = password;}/** javaBean */public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}} Users實(shí)體類 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 引入Struts2標(biāo)簽庫(kù) --> <%@ taglib uri="/struts-tags" prefix="s" %> <!-- 引入Ajax標(biāo)簽庫(kù) --> <%@ taglib uri="/struts-dojo-tags" prefix="sx" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'home.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> <!-- 在JSP頁(yè)面中加入head標(biāo)簽負(fù)責(zé)在頁(yè)面上導(dǎo)入Dojo所需要的CSS庫(kù)和JavaScript庫(kù) --><sx:head/></head><body><!-- url標(biāo)簽 用于生成一個(gè)URL地址 --><s:url id="login" value="tologin.action" /><!-- 單擊超鏈接異步訪問(wèn)指定Action --><sx:a href="%{login}" targets="div1" >登錄</sx:a><sx:div id="div1" cssStyle="border:1px solid red;" >第一個(gè)sx:div顯示登錄</sx:div><br><sx:div id="div2" cssStyle="border:1px solid green" >第二個(gè)DIV,顯示登錄結(jié)果。</sx:div></body> </html> home.jsp 主頁(yè)面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 引入struts2的標(biāo)簽庫(kù) --> <%@ taglib uri="/struts-tags" prefix="s"%> <!-- 引入Ajax標(biāo)簽 --> <%@ taglib uri="/struts-dojo-tags" prefix="sx"%> <%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'ajaxTime.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><!-- 在JSP頁(yè)面中加入head標(biāo)簽負(fù)責(zé)在頁(yè)面上導(dǎo)入Dojo所需要的CSS庫(kù)和JavaScript庫(kù) --><sx:head /></head><body><h2>用戶登錄</h2><!-- Struts2 表單標(biāo)簽 --><s:form id="s_form" action="login.action" method="post" theme="simple"><br>用戶名: <s:textfield label="用戶名" name="user.name" /><br>密碼: <s:textfield label="密碼" name="user.password" /><br><!-- 表單內(nèi)異步提交表單 --><sx:submit type="button" value="表單內(nèi)異步提交按鈕" targets="div2" /></s:form><!-- 表單外異步提交 --><sx:submit type="button" value="表單外異步提交按鈕" formId="s_form" targets="div2"/><sx:a formId="s_form" targets="div2" >我也可以提交表單</sx:a></body> </html> login.jsp 嵌入登陸頁(yè)面 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" > <struts><package name="struts2" extends="struts-default" namespace="/" ><!-- 單擊“用戶登錄”超鏈接顯示登錄菜單 --><action name="tologin" ><result >/login.jsp</result></action><!-- 單擊表單提交按鈕顯示登錄結(jié)果 --><action name="login" class="com.struts.LoginAction" method="login" ><result>/success.jsp</result> <result name="error">/error.jsp</result></action></package> </struts> struts.xml 配置 package com.struts;import com.entity.Users; import com.opensymphony.xwork2.ActionSupport; /*** 控制器類* 作用:處理登錄的請(qǐng)求* @author asus**/ public class LoginAction extends ActionSupport {/** 屬性 */private Users user;/** 重寫execute方法 :此方法作用,為指定處理請(qǐng)求的方法時(shí),默認(rèn)走此方法*/public String execute(){return "";}/** 登錄驗(yàn)證的方法 */public String login(){if(user!=null){if(user.getName().equals("admin") && user.getPassword().equals("admin")){return SUCCESS;}}return ERROR;}/** JavaBean */public Users getUser() {return user;}public void setUser(Users user) {this.user = user;}} LoginAction 控制器 <%@ 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"> <html><head><base href="<%=basePath%>"><title>My JSP 'success.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>登錄成功!</body> </html> success.jsp 登錄成功頁(yè)面 <%@ 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"> <html><head><base href="<%=basePath%>"><title>My JSP 'file.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>登陸失敗!</body> </html> error.jsp 登錄失敗頁(yè)面轉(zhuǎn)載于:https://www.cnblogs.com/wkrbky/p/5905589.html
總結(jié)
以上是生活随笔為你收集整理的Struts 2常用的Ajax标签的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 音响超载,功放机超载灯会亮吗?
- 下一篇: 在java web项目中编写自己的代码生