6.8 select 标签
以下代码 都是基于 6.3
select 标签用于生成一个下拉列表框,必须指定一个 list 属性,list 可以是集合,也可以是 map
常用属性:
● listKey : 指定集合元素中的某个属性(例如集合元素为 Person 实例,指定 Person 实例的 name 属性) 作为复选框的 value 。如果是 map ,则可以使用 key 和 value 分别代表 Map 对象的 key 和 value 作为复选框的 value
● listValue : 指定集合元素的某个属性,作为复选框的标签。如果是 map ,则可以使用 key 和 value 分别代表 Map 对象的 key 和 value 作为复选框标签
● multiple : 设置该列表框是否允许多选
s-select.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:select生成下拉选择框</title> <s:head/> </head> <body> <h3>使用s:select生成下拉选择框</h3> <s:form action="selectAction" method="post" theme="css_xhtml"> <!-- 使用简单集合来生成下拉选择框 --> <s:select name="a" label="请选择您喜欢的图书" labelposition="top" multiple="true" list="{'Struts 2权威指南','轻量级Java EE企业应用实战', 'JavaScript: The Definitive Guide'}"/> <!-- 使用简单Map对象来生成下拉选择框 --> <s:select name="b" label="请选择您想选择出版日期" labelposition="top" list="#{'Struts 2权威指南':'2007年10月','轻量级Java EE企业应用实战':'2007月4月', '疯狂Ajax讲义':'2007年6月'}" listKey="key" listValue="value"/> <!-- 创建一个JavaBean实例 --> <s:bean name="js.BookService" id="bs"/> <!-- 使用集合里放多个JavaBean实例来生成下拉选择框 --> <s:select name="b" label="请选择您喜欢的图书" labelposition="top" multiple="true" list="#bs.books" listKey="author" listValue="name"/> <hr/> <s:submit align="left"></s:submit> <br/> <table border="1"> <tr> <td><s:select name="c" list="#request.list"></s:select></td> <td><s:select name="d" list="#request.map" listValue="value" listKey="key"></s:select></td> </tr> </table> </s:form> </body> </html>
SelectAction
package js; import java.util.*; import com.opensymphony.xwork2.ActionSupport; public class SelectAction extends ActionSupport { private String a = ""; private String[] b; private String c = ""; private String d = ""; private List<String> list = new ArrayList<String>(); private Map<String, String> map = new HashMap<String, String>(); public String execute() throws Exception { list.add("啊"); list.add("的的"); list.add("才"); list.add("不"); map.put("a", "啊"); map.put("b", "擦擦"); map.put("c", "爸爸"); map.put("d", "迭代"); System.out.println("a : " + a); if (b != null) { System.out.println("b : " + b.length); for(String t : b){ System.out.println(t); } } System.out.println("c : " + c); System.out.println("d : " + d); return SUCCESS; } public String getA() { return a; } public void setA(String a) { this.a = a; } public String[] getB() { return b; } public void setB(String[] b) { this.b = b; } public String getC() { return c; } public void setC(String c) { this.c = c; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String getD() { return d; } public void setD(String d) { this.d = d; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <action name="selectAction" class="js.SelectAction"> <result name="success">/09/s-select.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/selectAction.action');" cssStyle="cursor: hand;">s-select.jsp</s:a>
BookService
package js; public class BookService { public Book[] getBooks() { return new Book[]{ new Book("疯狂Java讲义","李刚"), new Book("Struts 2权威指南","李刚"), new Book("轻量级Java EE企业应用实战","李刚"), new Book("疯狂Ajax讲义","李刚") }; } }
Book
package js; public class Book { private String name; private String author; public Book() { } public Book(String name, String author) { this.name = name; this.author = author; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setAuthor(String author) { this.author = author; } public String getAuthor() { return this.author; } }
6.9 radio 标签
以下代码 都是基于 6.3
js.BookService.java 之前例子已存在
s-radio.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:radio生成多个单选框</title> <s:head/> </head> <body> <s:form action="radioAction" method="post" theme="css_xhtml"> <h3>使用s:radio生成多个单选框</h3> <!-- 使用简单集合来生成多个单选框 --> <s:radio name="a" label="请选择您喜欢的图书" labelposition="top" list="{'Struts 2权威指南','轻量级Java EE企业应用实战','疯狂Ajax讲义'}"/> <!-- 使用简单Map对象来生成多个单选框 --> <s:radio name="b" label="请选择您想选择出版日期" labelposition="top" list="#{'Struts 2权威指南':'2007年10月','轻量级Java EE企业应用实战':'2007月4月' , '疯狂Ajax讲义':'2007年6月'}" listKey="key" listValue="value"/> <!-- 创建一个JavaBean实例 --> <s:bean name="js.BookService" id="bs"/> <!-- 使用集合里放多个JavaBean实例来生成多个单选框 --> <s:radio name="c" label="请选择您喜欢的图书" labelposition="top" list="#bs.books" listKey="author" listValue="name"/> <hr/> <s:submit align="left"></s:submit> <br/> <table border="1" bgcolor="red"> <tr> <td> <s:radio list="#request.list" name="d"></s:radio> </td> <td> <s:radio list="#request.map" name="e" listKey="key" listValue="value"></s:radio> </td> </tr> <tr> <td> <input type="radio" name="f" value="aradio">选1</input> <input type="radio" name="f" value="bradio">选2</input> </td> </tr> </table> </s:form> </body> </html>
RadioAction
package js; import java.util.*; import com.opensymphony.xwork2.ActionSupport; public class RadioAction extends ActionSupport { private String a = ""; private String b = ""; private String c = ""; private String d = ""; private String e = ""; private String f = ""; private List<String> list = new ArrayList<String>(); private Map<String, String> map = new HashMap<String, String>(); public String execute() throws Exception { list.add("啊"); list.add("的的"); list.add("才"); list.add("不"); map.put("a", "啊"); map.put("b", "擦擦"); map.put("c", "爸爸"); map.put("d", "迭代"); System.out.println("a : " + a); System.out.println("b : " + b); System.out.println("c : " + c); System.out.println("d : " + d); System.out.println("e : " + e); System.out.println("f : " + f); return SUCCESS; } public String getA() { return a;} public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } public String getC() { return c; } public void setC(String c) { this.c = c;} public String getD() { return d; } public void setD(String d) { this.d = d; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public String getE() { return e; } public void setE(String e) { this.e = e; } public String getF() { return f; } public void setF(String f) { this.f = f; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <action name="radioAction" class="js.RadioAction"> <result name="success">/09/s-radio.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/radioAction.action');" cssStyle="cursor: hand;">s-radio.jsp</s:a>
6.10 optgroup 标签
以下代码 都是基于 6.3
optgroup 用于生成一个下拉框的选项组,因此,必须放在 <s:select .../> 中使用。
s-optgroup.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:optgroup生成下拉选择框的选项组</title> <s:head/> </head> <body> <h3>使用s:optgroup生成下拉选择框的选项组</h3> <s:form action="optgroupAction" method="post" theme="css_xhtml"> <!-- 直接使用Map为列表框生成选项 --> <s:select label="选择您喜欢的图书" name="book" size="7" list="#{'Struts 2权威指南':'李刚','轻量级Java EE企业应用实战':'李刚','疯狂Ajax讲义':'李刚'}" listKey="value" listValue="key"> <!-- 使用Map对象来生成选择框的选项组 --> <s:optgroup label="Rod Johnson" list="#{'Expert One-on-One J2EE Design and Development':'Johnson'}" listKey="value" listValue="key"/> <s:optgroup label="David Flanagan" list="#{'JavaScript: The Definitive Guide':'David'}" listKey="value" listValue="key"/> </s:select> <hr/> <s:submit align="left"></s:submit> <br/> <table border="1" > <tr> <td> <s:select name="book1" list="#request.map" listKey="key" listValue="value"> <s:optgroup list="#request.map1" listKey="key" listValue="value" label="test"></s:optgroup> </s:select> </td> <td> <s:select name="book2" list="#request.list" > <s:optgroup list="#request.map1" listKey="key" listValue="value" label="test"></s:optgroup> </s:select> </td> </tr> </table> </s:form> </body> </html>
OptgroupAction
package js; import java.util.*; import com.opensymphony.xwork2.ActionSupport; public class OptgroupAction extends ActionSupport { private String book = ""; private String book1 = ""; private String book2 = ""; private Map<String, String> map = new HashMap<String, String>(); private Map<String, String> map1 = new HashMap<String, String>(); private List<String> list = new ArrayList<String>(); private List<String> list1 = new ArrayList<String>(); public String execute() throws Exception { System.out.println("book : " + book); System.out.println("book1 : " + book1); System.out.println("book2 : " + book2); map.put("a", "暗暗");map.put("b", "拜拜");map.put("c", "擦擦");map.put("d", "迭代"); map1.put("a1", "暗暗1");map1.put("b1", "拜拜1");map1.put("c1", "擦擦1");map1.put("d1", "迭代1"); list.add("啦");list.add("啦隧道发生地方");list.add("啦发生的方式");list.add("啦迭代");list.add("啦22");list.add("啦1"); list1.add("a");list1.add("b");list1.add("c");list1.add("d");list1.add("e"); return SUCCESS; } public String getBook() { return book; } public void setBook(String book) { this.book = book; } public String getBook1() { return book1; } public void setBook1(String book1) { this.book1 = book1; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Map<String, String> getMap1() { return map1; } public void setMap1(Map<String, String> map1) { this.map1 = map1; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public List<String> getList1() { return list1; } public void setList1(List<String> list1) { this.list1 = list1; } public String getBook2() { return book2; } public void setBook2(String book2) { this.book2 = book2; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <action name="optgroupAction" class="js.OptgroupAction"> <result name="success">/09/s-optgroup.jsp</result> </action> <action name=""> <result>.</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/optgroupAction.action');" cssStyle="cursor: hand;">s-optgroup.jsp</s:a>
optgroup 标签,貌似只能用 map 不能用 list
6.11 token 标签
以下代码 都是基于 6.3
用于防止多次提交表单的标签。如果需要该标签起作用,则应该在 Struts 2 的配置文件中启动 TokenInterceptor 拦截器或 TokenSessionStoreInterceptor 拦截器
实现原理: 在表单中增加一个隐藏域,每次加载该页面时,该隐藏域的值都不相同。 而 TokenInterceptor 拦截器则拦截所有用户请求,如果 2 次请求该 token 对应隐藏域的值相同(前一次提交时 token 隐藏域的值保存在 session 里 ),则阻止表单提交
注意 : 默认情况下,token 标签生成的隐藏域的 name 为 struts.token 。因此,不要在表单中另外再定义一个名为 struts.token 的表单域
s-token.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:token防止重复提交</title> </head> <body> <h3>使用s:token防止重复提交</h3> <s:form action="pro"> <!-- 普通表单域 --> <s:textfield name="book" key="book"/> <!-- 用于防刷新的token --> <s:token/> <s:submit value="提交"/> </s:form> </body> </html>
ProAction
package js; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; public class ProAction implements Action { private String book; public void setBook(String book) { this.book = book; } public String getBook() { return this.book; } // 处理用户请求,保存用户参数,并返回SUCCESS public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); ctx.put("book", getBook()); return SUCCESS; } }
refresh.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>防刷新测试</title> </head> <body> 您的请求已被处理!请不要刷新页面 </body> </html>
show.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>防刷新测试</title> </head> <body> 用户正常提交,提交的书名为:${requestScope.book} </body> </html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <!-- 定义名为pro的Action,其实现类为lee.ProAction --> <action name="pro" class="js.ProAction"> <!-- 使用系统默认的拦截器栈 --> <interceptor-ref name="defaultStack"/> <!-- 使用防刷新的token拦截器 --> <interceptor-ref name="token"/> <!-- 定义重复提交转向的视图,该逻辑视图名必须是invalid.token --> <result name="invalid.token">/09/refresh.jsp</result> <!-- 如果处理结果返回success,对应/show.jsp视图资源 --> <result name="success">/09/show.jsp</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/s-token.jsp');" cssStyle="cursor: hand;">s-token.jsp</s:a>
messageResource_en_US.properties
book=Book Name
messageResource_zh_CN.properties 或 messageResource_ja_JP.properties
book=书名
HTML
<input type="hidden" name="struts.token.name" value="struts.token" /> <input type="hidden" name="struts.token" value="2OHZFTNO4QPM3A58IE7TGCZEUMQWRRKY" />
6.12 updownselect 标签
以下代码 都是基于 6.3
用法非常类似于 select 标签,区别是该标签生成的列表框可以上下移动。
支持 list listKey listValue ,还支持如下属性:
● allowMoveUp : 是否显示 上移 按钮。 默认 true
● allowMoveDown : 是否显示 下移 按钮。 默认 true
● allowSelectAll : 是否显示 全选 按钮,默认 true
● moveUpLabel : 设置 上移 按钮的文本 ,默认 ˆ
● moveDownLabel : 设置 下移 按钮的文本 默认 ˇ
● selectAllLabel : 设置 全选 按钮的文本 默认 *
js.BookService.java 在之前例子中已有
s-updownselect.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:updownselect生成可上下移动选项的下拉选择框</title> <s:head/> </head> <body> <h3>使用s:updownselect生成可上下移动选项的下拉选择框</h3> <s:form action="updownselectAction" method="post" theme="css_xhtml"> <!-- 使用简单集合来生成可上下移动选项的下拉选择框 --> <s:updownselect name="a" label="请选择您喜欢的图书" labelposition="top" moveUpLabel="向上移动" list="{'Struts 2权威指南' , '轻量级Java EE企业应用实战','疯狂Ajax讲义'}"/> <!-- 使用简单Map对象来生成可上下移动选项的下拉选择框 且使用emptyOption="true"增加一个空选项--> <s:updownselect name="b" label="请选择您想选择出版日期" labelposition="top" moveDownLabel="向下移动" list="#{'Struts 2权威指南':'2007年10月','轻量级Java EE企业应用实战':'2007月4月','疯狂Ajax讲义':'2007年6月'}" listKey="key" emptyOption="true" listValue="value"/> <s:bean name="js.BookService" id="bs"/> <!-- 使用集合里放多个JavaBean实例来可上下移动选项的生成下拉选择框 --> <s:updownselect name="c" label="请选择您喜欢的图书的作者" labelposition="top" selectAllLabel="全部选择" multiple="true" list="#bs.books" listKey="author" listValue="name"/> <hr/><s:submit align="left"></s:submit><hr/> <table border="1"> <tr> <td><s:updownselect name="d" list="#request.list" selectAllLabel="全部" moveDownLabel="下" moveUpLabel="上" /></td> <td><s:updownselect name="e" list="#request.map" selectAllLabel="全部" moveDownLabel="下" moveUpLabel="上" listKey="key" listValue="value"/></td> </tr> </table> </s:form> </body> </html>
UpdownselectAction
package js; import java.util.*; import com.opensymphony.xwork2.ActionSupport; public class UpdownselectAction extends ActionSupport { private String a = ""; private String b = ""; private String c = ""; private String d = ""; private String e = ""; private List<String> list = new ArrayList<String>(); private Map<String, String> map = new HashMap<String, String>(); public String execute() throws Exception { System.out.println("a : " + a); System.out.println("b : " + b); System.out.println("c : " + c); System.out.println("d : " + d); System.out.println("e : " + e); list.add("a啊");list.add("a不");list.add("a才");list.add("a的");list.add("a额"); map.put("1", "a");map.put("2", "b");map.put("3", "c");map.put("4", "d");map.put("5", "e"); return SUCCESS; } public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } public String getC() { return c; } public void setC(String c) { this.c = c; } public String getD() { return d; } public void setD(String d) { this.d = d; } public String getE() { return e; } public void setE(String e) { this.e = e; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <action name="updownselectAction" class="js.UpdownselectAction"> <result name="success">/09/s-updownselect.jsp</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/updownselectAction.action');" cssStyle="cursor: hand;">s-updownselect.jsp</s:a>
bug? 提交表单的时候 貌似会把所有选项都提交
七、 非表单标签
非表单标签主要用于页面中生成一些非表单的可视化元素,如 tab 页面、输出 HTML 树形结构等。当然也包含在页面显示 Action 里封装的信息。
非表单标签主要有如下几个:
● actionerror : Action 的 getActionError() 返回不为 null ,则输出该信息
● actionmessage : Action 的 getActionMessage() 返回不为 null ,则输出该信息
● component : 使用次标签可以生成一个自定义组件。
● fielderror : 如果 Action 实例存在表单域的类型转换错误、校验错误,该标签则负责输出这些错误提示
fielderror 在 类型转换、 数据校验部分有介绍,此处不说了
7.1 actionerror 和 actionmessage 标签
以下代码 都是基于 6.3
这2个标签用法完全一样,作用也几乎一样。 actionerror 输出 action 的 getActionError() ,actionmessage 输出 action 的 getActionmessage()
DemoAction.java
package js; import com.opensymphony.xwork2.ActionSupport; public class DemoAction extends ActionSupport { public String execute() { // 添加两条Error信息 addActionError("第一条错误消息!"); addActionError("第二条错误消息!"); // 添加两条普通信息 addActionMessage("第一条普通消息!"); addActionMessage("第二条普通消息!"); return SUCCESS; } }
s-msg.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:actionerror和s:actionmessage标签生成错误提示</title> <s:head/> </head> <body> <s:action name="demo" executeResult="true"/> </body> </html>
executeResult="true" 表示之间将 demoAction 的处理结果包含到本页面中
demo.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <s:actionerror/> <s:actionmessage /> </body> </html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.2.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="messageResource"/> <constant name="struts.i18n.encoding" value="GBK"/> <package name="js" extends="struts-default" namespace="/09"> <!-- 定义一个简单的Action --> <action name="demo" class="js.DemoAction"> <result>/09/demo.jsp</result> </action> </package> </struts>
<s:a href="" onclick="newWin('09/s-msg.jsp');" cssStyle="cursor: hand;">s-msg.jsp</s:a>
7.2 component 标签
以下代码 都是基于 6.3
component 标签可用于创建自定义视图组件。
属性:
● theme : 自定义组件所使用的主题,默认 xhtml 主题
● templateDir : 自定义组件的主题目录,默认使用系统的主题目录,即 template 目录
● template : 指定自定义组件所使用的模版
除此之外,还可以在 component 标签内使用 param 子标签,子标签表示向该标签模版中传入额外的参数。如果希望在模版中取得该参数,则:
$parameters.paramname 或者 $parameters['paramname']
提示 : 自定义模版可以采用 FreeMarker、JSP、Velocity 三种技术来书写
s-component.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>使用s:component标签</title> </head> <body> <h3>使用s:component标签</h3> 使用默认主题(xhtml),默认主题目录(template)<br/> 使用mytemplate.jsp作为视图组件 <s:component template="mytemplate.jsp"> <s:param name="list1" value="{'Struts2权威指南', '轻量级J2EE企业应用实战','基于J2EE的Ajax宝典'}"/> </s:component> <hr/> 使用自定义主题,自定义主题目录<br/> 使用myAnotherTemplate.jsp作为视图组件 <s:component templateDir="myTemplateDir" theme="myTheme" template="myAnotherTemplate.jsp" > <s:param name="list" value="{'Struts 2权威指南', '轻量级Java EE企业应用实战' , '疯狂Ajax讲义'}" /> </s:component> </body> </html>
/WebContent/template/xhtml/mytemplate.jsp
template 目录与 WEB-INF 同级
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <div style="background-color:#eeeeee;"> <b>JSP自定义模板<br> 请选择您喜欢的图书<br></b> <s:select list="parameters.list1"/> </div>
/WebContent/myTemplateDir/myTheme/myAnotherTemplate.jsp
myTemplateDir 目录与 WEB-INF 同级
<%@ page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags" %> <div style="background-color:#bbbbbb;"> JSP自定义模板<br> 请选择您喜欢的图书<br> <select> <s:iterator value="%{top.parameters.list}"> <option><s:property/></option> </s:iterator> </select> </div>
注意 : Struts 2.0 即使自行定义了 templateDir 、 theme 属性,一样可以在自定义视图模版中使用 select 等UI,struts 2.1 中就不行