Struts 2常用的Ajax标签
<!-- 引入Ajax标签 -->
<%@ taglib uri="/struts-dojo-tags" prefix="sx"%>
<head>
<!-- 在JSP页面中加入head标签
负责在页面上导入Dojo所需要的CSS库和JavaScript库 -->
<sx:head />
</head>
|
方法名
|
说 明
|
|
<sx:div>
|
创建一个div区域,可以通过Ajax向其中加载内容,以实现局部刷新
|
|
<sx:submit>
|
通过Ajax来更新某个元素的内容或提交表单
|
|
<sx:a>
|
通过Ajax来更新某个元素的内容或提交表单
|
|
<sx:tabbedPanel>
|
创建一个标签面板,由<s:div>来提供内容。
|
|
<sx:autocompleter>
|
根据用户输入提供输入建议,或者帮助用户自动完成输入
|
|
<sx:tree>
|
创建一个支持Ajax的树形组件(Widget)
|
div标签例子


实现代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入struts2的标签库 -->
<%@ taglib uri="/struts-tags" prefix="s"%>
<!-- 引入Ajax标签 -->
<%@ 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页面中加入head标签
负责在页面上导入Dojo所需要的CSS库和JavaScript库 -->
<sx:head />
</head> <body>
<s:url id="time" value="time.action" />
<s:url id="welcome" value="welcome.action" /> <!-- 每隔1秒异步访问一次 -->
<sx:div href="%{time}" updateFreq="1000" />
<!-- 只异步访问一次 -->
<sx:div href="%{welcome}" />
<!-- 无异步访问-->
<sx:div>
初始化的内容!
</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>
<%
//获得当前时间
long currentTime = System.currentTimeMillis(); //取出session中存入的现在时间
Long stratTime = (Long) session.getAttribute("currentTime");
if (stratTime == null) {
//第一次访问
session.setAttribute("currentTime", currentTime);
} else {
//以秒计算计算已用时间
long used = (currentTime - stratTime) / 1000;
session.setAttribute("used", used);
//当用户浏览网页时间超过60秒则提示用户休息一下。
boolean flag = false;
if (used > 60) {
flag = true;
}
session.setAttribute("flag", flag);
}
%>
<s:if test="#session.flag==true">
你该稍微休息一下了。
</s:if>
<s:else>
你已经访问的时间:<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属性不写默认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>
欢迎你登录本系统!
</body>
</html>
welcome.jsp


实现代码:
package com.entity;
/**
* 用户类
* @author asus
*
*/
public class Users { /** 属性 */
private String name;
private String password; /** 构造方法 */
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实体类
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入Struts2标签库 -->
<%@ taglib uri="/struts-tags" prefix="s" %>
<!-- 引入Ajax标签库 -->
<%@ 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页面中加入head标签
负责在页面上导入Dojo所需要的CSS库和JavaScript库 -->
<sx:head/>
</head> <body>
<!-- url标签 用于生成一个URL地址 -->
<s:url id="login" value="tologin.action" />
<!-- 单击超链接异步访问指定Action -->
<sx:a href="%{login}" targets="div1" >登录</sx:a> <sx:div id="div1" cssStyle="border:1px solid red;" >
第一个sx:div显示登录
</sx:div><br>
<sx:div id="div2" cssStyle="border:1px solid green" >
第二个DIV,显示登录结果。
</sx:div>
</body>
</html>
home.jsp 主页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入struts2的标签库 -->
<%@ taglib uri="/struts-tags" prefix="s"%>
<!-- 引入Ajax标签 -->
<%@ 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页面中加入head标签
负责在页面上导入Dojo所需要的CSS库和JavaScript库 -->
<sx:head />
</head>
<body>
<h2>用户登录</h2>
<!-- Struts2 表单标签 -->
<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>
<!-- 表单内异步提交表单 -->
<sx:submit type="button" value="表单内异步提交按钮" 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 嵌入登陆页面
<?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> <!-- 单击表单提交按钮显示登录结果 -->
<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;
/**
* 控制器类
* 作用:处理登录的请求
* @author asus
*
*/
public class LoginAction extends ActionSupport { /** 属性 */
private Users user; /** 重写execute方法 :此方法作用,为指定处理请求的方法时,默认走此方法*/
public String execute(){ return "";
} /** 登录验证的方法 */
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 登录成功页面
<%@ 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 登录失败页面
Struts 2常用的Ajax标签的更多相关文章
- struts2视频学习笔记 29-30(Struts 2常用标签,防止表单重复提交)
课时28 Struts 2常用标签解说 property标签 property标签用于输出指定值: <s:set name="name" value="'kk'&q ...
- Struts 2 常用技术
目录 Struts 2 常用技术 1. 常用类和接口 1.1 getter 和 setter 方法 1.2 Action 接口 1.3 ActionSupport 类 1.4 通过 Act ...
- JSF中使用f:ajax标签无刷新页面改变数据
ajax本是用在前端的一种异步请求数据的操作,广泛用于js中,一般的js框架如jq都有被封装好的方法,用于发起异步请求操作.异步操作可以增强用户体验和操作,越来越多的程序都在使用ajax.JSF的fa ...
- struts2与常用表格ajax操作的json传值问题
struts与常用的dataTables和jqueryGrid等表格进行ajax传值时,经常会传值不适配的问题,这是因为struts在进行ajax操作时已经对你要操作的json数据进行了处理,所以不需 ...
- HTML之:fieldset——一个不常用的HTML标签
2016年4月14日17:10:02记录 一个不常用的HTML标签fieldset,不过我觉得比较有意思,其语法如下: <fieldset><legend>fieldset名称 ...
- JSP 标准标签库(JSTL)之最常用的JSTL标签总结
JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. Apache Tomcat安装JSTL 库步骤如下: 从Apache的标准标签库中下载的二进包(jakarta-t ...
- 常用HTML meta 标签属性(网站兼容与优化需要),meta标签
常用HTML meta 标签属性(网站兼容与优化需要),meta标签 标签提供关于HTML文档的元数据.元数据不会显示在页面上,但是对于机器是可读的.它可用于浏览器(如何显示内容或重新加载页面),搜索 ...
- Struts 有哪些经常使用标签库
Struts 有哪些经常使用标签库 1.html标签库 2.bean标签库 3.logic标签库
- Django常用的模板标签
django 目前了解的各个文件的作用: manage.py: 运行服务 urls: 路由 views: 处理数据 传递给 html模板 html文件: 通过 {{变量名}}接收变量 通过 模板标 ...
随机推荐
- linux部署war包方案
batch.sh内容: su - -c" 使用管理员权限 service tomcat6 stop; 停止tomca6t服务 mkdir /home/jnfwzFtp/bushubackup ...
- Codeforces Beta Round #17 C. Balance DP
C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...
- windows下ruby安装环境配置
Ruby 安装 从源代码在windows下安装Ruby是非常苦逼的差事,可以从http://rubyinstaller.org/ 或者 http://railsinstaller.org/ 下载已经打 ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- 如何启动ResourceManager和NodeManager
登录到bigtop1上,vagrant ssh bigtop1 将/usr/lib/hadoop/libexec/init-hdfs.sh文件内容替换为: #!/bin/bash -ex # # Li ...
- Ext JS 6 新特性和工具
Ext JS 6 新特性和工具 Ext JS 6 带来很多新特性.工具和改进.以下是一些亮点: • 合并了 Ext JS & Sencha Touch - 在 Ext 6, 你可以访问 Ext ...
- Windows下修改Oracle默认的端口1521
数据库最好不对公网开放,如果要开放,最好把默认端口改掉,防止一些针对 1521端口的入侵 1.找到 product\11.2.0\dbhome_1\NETWORK\ADMIN 下面的 listene ...
- ffrpc的php客户端lib
摘要: ffrpc 是c++异步通讯库,使用ffrpc可以非常容易的构建服务器程序.为了使用方便,ffrpc提供了python.php的客户端lib,这样使用php于c++构建的server也是顺手拈 ...
- Cocos2d-JS项目之四:UI界面的优化
测试环境: iphone4.iOS6.1.2.chrome 37.2062.60,Cocos2d-js 3.6 之前写了不少,实际项目也按这个去优化了,也有效果,但到最后才发现,尼玛,之前都搞错了,之 ...
- CSS基础(三):选择器
常用选择器 元素选择器,即html标记如div,ul,li,p,h1~h6,table等. p { font-size:14px; } h1 { color:#F00; } 复合选择器, 由两个选择器 ...