JAVA_WEB Struts2学习:Struts2Ajax标签的学习

来源:互联网 发布:超声波检测软件 编辑:程序博客网 时间:2024/05/29 14:54

导入Struts2 Ajax标签:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%@ 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>  <sx:head/>


Struts2 Ajax标签 实例1:

index:


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%@ 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>  <sx:head/>    <base href="<%=basePath%>">        <title>DOJO学习</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>     <s:form>    <s:url id="time" value="myAction!times"></s:url>    <s:url id="welcome" value="myAction!welcome"></s:url>        <sx:div id="div1" updateFreq="1000" href="%{time}"></sx:div>    <sx:div id="div2" href="%{welcome}"></sx:div>    <sx:div id="div3">    这是初始化DIV层     </sx:div>    </s:form>  </body></html>


struts2.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="TestManager" extends="struts-default"><action name="myAction" class="com.zuxia.yc42.action.MyTestAction"><result name="time">/jsp/time.jsp</result><result name="welcome">/jsp/welcome.jsp</result><result name="login">/MyJsp.jsp</result></action></package></struts>    



public class MyTestAction extends ActionSupport

package com.zuxia.yc42.action;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class MyTestAction extends ActionSupport {private Long time;private String usName;private String usPass;private String msg;public String times(){long currentTime = System.currentTimeMillis(); //获取当前时间HttpSession session =ServletActionContext.getRequest().getSession();//获取开始时间Long startTime = (Long) session.getAttribute("startTime");if (startTime == null) { //第一次访问startTime = currentTime;session.setAttribute("startTime", startTime);}time =(currentTime-startTime)/1000;//以秒计算的已用时间return "time";}public String login(){if(usName.equals("admin") &&usPass.equals("123")){msg = "登陆成功";}else{msg = "登陆失败";}return "login";}public String welcome(){return "welcome";}public Long getTime() {return time;}public void setTime(Long time) {this.time = time;}public String getUsName() {return usName;}public void setUsName(String usName) {this.usName = usName;}public String getUsPass() {return usPass;}public void setUsPass(String usPass) {this.usPass = usPass;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}}



time.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ 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>    您已经访问本网站<s:property value="time"/>秒  </body></html>



welcome.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%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>    欢迎您访问本网站<br>  </body></html>




Struts2 Ajax标签 实例2:

show.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%@ 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>  <sx:head/>    <base href="<%=basePath%>">        <title>My JSP 'show.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>    <sx:a href="login.jsp" targets="div1">登陆</sx:a>    <sx:div id="div1">    图层二    </sx:div>    <sx:div id="div2">    图层三    </sx:div>  </body></html>



login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><%@ 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>  <sx:head/>    <base href="<%=basePath%>">        <title>My JSP 'login.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>   <s:form>   <table>   <s:textfield name="usName" label="用户名"></s:textfield>   <s:password name="usPass" label="密码"></s:password>   <sx:submit targets="div2" href="myAction!login"></sx:submit>   </table>   </s:form>  </body></html>



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="TestManager" extends="struts-default"><action name="myAction" class="com.zuxia.yc42.action.MyTestAction"><result name="time">/jsp/time.jsp</result><result name="welcome">/jsp/welcome.jsp</result><result name="login">/MyJsp.jsp</result></action></package></struts>    



public class MyTestAction extends ActionSupport :

package com.zuxia.yc42.action;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class MyTestAction extends ActionSupport {private Long time;private String usName;private String usPass;private String msg;public String times(){long currentTime = System.currentTimeMillis(); //获取当前时间HttpSession session =ServletActionContext.getRequest().getSession();//获取开始时间Long startTime = (Long) session.getAttribute("startTime");if (startTime == null) { //第一次访问startTime = currentTime;session.setAttribute("startTime", startTime);}time =(currentTime-startTime)/1000;//以秒计算的已用时间return "time";}public String login(){if(usName.equals("admin") &&usPass.equals("123")){msg = "登陆成功";}else{msg = "登陆失败";}return "login";}public String welcome(){return "welcome";}public Long getTime() {return time;}public void setTime(Long time) {this.time = time;}public String getUsName() {return usName;}public void setUsName(String usName) {this.usName = usName;}public String getUsPass() {return usPass;}public void setUsPass(String usPass) {this.usPass = usPass;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}}



MyJsp.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ 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 'MyJsp.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>    <s:property value="msg"/>  </body></html>




原创粉丝点击