struts.xml 配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="tags" namespace="/" extends="struts-default"> <action name="ajaxA" class="lesson9.AjaxATag"> </action> <action name="ajaxAutocompleter" class="lesson9.AjaxAutocompleterTag"> </action> <action name="ajaxBind" class="lesson9.AjaxBindTag"> </action> <action name="ajaxTabbedpanel" class="lesson9.AjaxTabbedpanelTag"> </action> <action name="ajaxTree" class="lesson9.AjaxTreeTag"> </action> </package> </struts>
AjaxATag 控制器
package lesson9; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AjaxATag extends ActionSupport{ public String execute() throws Exception { ServletActionContext.getResponse().setContentType( "text/html;charset=utf-8"); ServletActionContext.getResponse().getWriter().write("a标签请求成功。"); ServletActionContext.getResponse().getWriter().close(); return null; } }
sx:a 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head> <title>sx:a 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head debug="true" cache="false" compressed="false" /> </head> <body> <!-- 在异步请求action完成前,显示进度条图片来提供友好的用户界面 --> <img id="loadingImage" src="LoadingAnimation.gif" style="display: none" /> <!-- 在异步请求action完成后,将结果显示在此div中 --> <s:div id="parentDiv"></s:div> <!-- s:form配置的是 sx:a单击时,异步提交到后台的action --> <s:form action="ajaxA" namespace="/"> <!-- indicator指定在加载时,使用哪个id组件来显示访求完成前的提示信息 --> <sx:a targets="parentDiv" showLoadingText="false" indicator="loadingImage"> 异步显示 </sx:a> </s:form> </body> </html>
AjaxAutocompleterTag 控制器
package lesson9; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AjaxAutocompleterTag extends ActionSupport{ public String execute() throws Exception { ServletActionContext.getResponse().setContentType( "text/json-comment-filtered;charset=utf-8"); StringBuffer sb = new StringBuffer(); //拼json串 sb.append("["); sb.append(" ['C/C++', '1'], " + " ['C#', '1'], " + " ['D', '2']," + " ['Java', '3'], " + " ['Javascript', '4'], " + " ['PHP', '5']"); sb.append("]"); ServletActionContext.getResponse().getWriter().write(sb.toString()); ServletActionContext.getResponse().getWriter().close(); return null; } }
sx:autocompleter 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head> <title>sx:autocompleter 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head cache="false" compressed="false" /> </head> <body> <!-- s:form配置的是 sx:a单击时,异步提交到后台的action --> <s:form> <!-- 直接配置list属性为一个固定的集合 --> <sx:autocompleter name="test1" list="{'java','javascript','json','jfreechart'}" autoComplete="true" label="请输入您感兴趣的技术"/> <!-- 配置href属性,指定集合的值来自于action --> <sx:autocompleter name="test2" href="ajaxAutocompleter.action" autoComplete="false" label="请输入您喜欢的语言"/> </s:form> </body> </html>
AjaxBindTag 控制器
package lesson9; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AjaxBindTag extends ActionSupport{ public String execute() throws Exception { ServletActionContext.getResponse().setContentType( "text/html;charset=utf-8"); ServletActionContext.getResponse().getWriter().write("bind标签请求成功。"); ServletActionContext.getResponse().getWriter().close(); return null; } }
sx:bind 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>sx:bind 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head debug="true" cache="false" compressed="false" /> </head> <body> <!-- 在异步请求action完成前,显示进度条图片来提供友好的用户界面 --> <img id="loadingImage" src="LoadingAnimation.gif" style="display: none" /> <!-- 在异步请求action完成后,将结果显示在此div中 --> <s:div id="parentDiv"></s:div> <s:form> <s:submit id="btn" value="测试bind标签"/> <!-- indicator指定在加载时,使用哪个id组件来显示访求完成前的提示信息 href属性表示此bind要执行action的URL,如果不配置此属性,则没有任何效果 --> <sx:bind sources="btn" href="ajaxBind.action" events="onclick" targets="parentDiv" showLoadingText="false" indicator="loadingImage"/> </s:form> </body> </html>
sx:datetimepicker 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head> <title>sx:datetimepicker 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head cache="false" compressed="false" /> </head> <body> <s:form> <sx:datetimepicker name="test1" label="默认日期格式" /> <sx:datetimepicker name="test2" label="YYYY-MM-DD日期格式" displayFormat="yyyy-MM-dd" /> <sx:datetimepicker name="test4" label="设置指定值" value="%{'2010-12-31'}" /> <sx:datetimepicker name="test5" label="设置为今天" value="%{'today'}"/> </s:form> </body> </html>
AjaxTabbedpanelTag 控制器
package lesson9; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AjaxTabbedpanelTag extends ActionSupport{ public String execute() throws Exception { ServletActionContext.getResponse().setContentType( "text/html;charset=utf-8"); ServletActionContext.getResponse().getWriter().write("tabbedpanel标签请求成功。"); ServletActionContext.getResponse().getWriter().close(); return null; } }
sx:tabbedpanel 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>sx:tabbedpanel 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head debug="false" cache="false" compressed="false" /> </head> <body> <!-- 引入sx:tabbedpanel标签 --> <sx:tabbedpanel id="test"> <!-- 第一个页卡配置静态表单内容 --> <sx:div id="one" label="静态内容" labelposition="top" closable="true"> 请输入下列信息<br /> <s:form> <s:textfield name="test1" label="用户名" /> <br /> <s:textfield name="test2" label="密码" /> </s:form> </sx:div> <!-- 第二个页面异步访问后台 --> <sx:div id="two" label="Ajax加载" href="ajaxTabbedpanel.action" > 默认信息 </sx:div> </sx:tabbedpanel> </body> </html>
sx:textarea 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head> <title>sx:textarea 示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head debug="true" cache="false" compressed="false" /> </head> <body> <sx:textarea cols="50" rows="10" name="test" /> </body> </html>
AjaxTreeTag 控制器
package lesson9; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class AjaxTreeTag extends ActionSupport{ // 对于异步树,会将树的id属性值以nodeId参数名提交到后台 private String nodeId; public String execute() throws Exception { ServletActionContext.getResponse().setContentType( "text/json-comment-filtered;charset=utf-8"); StringBuffer sb = new StringBuffer(); // 拼json串 sb.append("["); if (nodeId == null) { // 如果是根结点 sb.append(this.getRoot()); } else { // 如果不是根结点 sb.append(getOther()); } sb.append("]"); ServletActionContext.getResponse().getWriter().write(sb.toString()); ServletActionContext.getResponse().getWriter().close(); return null; } // 取得根结点的数据 private String getRoot() { return "{'label':'java', 'id':1, 'hasChildren': true} "; } // 取得其它结点的数据 private String getOther() { if ("1".equals(nodeId)) { return "{'label':'struts', 'id':2, 'hasChildren': true} "; } else if ("2".equals(nodeId)) { return "{'label':'ajaxTag', 'id':3, 'hasChildren': false} "; } return null; } public String getNodeId() { return nodeId; } public void setNodeId(String nodeId) { this.nodeId = nodeId; } }
sx:tree/treenode示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!-- 必须引入dojo标签 --> <%@ taglib prefix="sx" uri="/struts-dojo-tags"%> <html> <head> <title>sx:tree/treenode示例</title> <!-- 必须引入sx:head,否则会缺少支持dojo的javascript引入 --> <sx:head debug="false" cache="false" compressed="false" /> </head> <body> 静态树示例:<p /> <sx:tree id="tree1" label="静态树"> <sx:treenode id="node1" label="c/c++" /> <sx:treenode id="node2" label="java"> <sx:treenode id="node21" label="struts2" /> <sx:treenode id="node22" label="spring" /> <sx:treenode id="node23" label="hibernate" /> </sx:treenode> <sx:treenode id="node3" label="php" /> </sx:tree> <hr /> 动态树示例:<p /> <s:url var="url" namespace="/" action="ajaxTree" /> <sx:tree id="tree" href="%{#url}" /> </body> </html>