Struts 2为大家提供了不少常用的很酷的表单标志,简化了我们程序员的工作。不过,由于这些都是新标志,大家可能在使用上还存在不少疑问。本文将就朋友们的回复、留言或Email上的问题,分别对这些酷标志进行讲述。
表单标志使用小技巧Struts 2的表单标志在输出(render)HTML时,使用了模板的概念,增加了复杂性(因为它不像Struts 1.x的表单标志,它通常都是一个标志对应HTML的一个元素),因此大家在使用时,需要一些技巧:
下面我将分别对这些标志进行讲述: 1、<s:checkboxlist />大家对<s:checkboxlist />的最大的疑问可能是:“如何在默认情况下,选中某些checkbox?” 答案其实很简单,只需要将其“value”属性设为你的要选中的值,如以代码所示: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
清单1 WebContent/checkboxlist.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <title>Struts 2 Cool Tags - <s:checkboxlist/ ></title> <s:head /> </head> <body> <h2><s:checkboxlist/></h2> <s:form action="Store" > <s:checkboxlist name="skills1" label="Skills 1" list="{ 'Java', '.Net', 'RoR', 'PHP' }" value="{ 'Java', '.Net' }" /> <s:checkboxlist name="skills2" label="Skills 2" list="#{ 1:'Java', 2: '.Net', 3: 'RoR', 4: 'PHP' }" listKey="key" listValue="value" value="{ 1, 2, 3 }"/> </s:form> </body> </html> 分布运行应用程序,在浏览器中键入:http://localhost:8080/Struts2_CoolTags/checkboxlist.jsp,出现如下图所示页面:
2、<s:doubleselect />大家看Struts 2的showcase的例子,<s:doubleselect />的用法如下所示: <s:doubleselect
清单3 Showcase中<s:doubleselect />
tooltip="Choose Your State" label="State" name="region" list="{'North', 'South'}" value="'South'" doubleValue="'Florida'" doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" doubleName="state" headerKey="-1" headerValue="---------- Please Select ----------" emptyOption="true" /> 很多朋友问:“上面的'list’属性只有两个值,如果我有三个或更多的值,'doublelist’属性应该如何设定呢?” 我建议的做法是先定义一个Map类型的对象,键为“list”的集合,值则为“doubleList”的集合,然后“doubleList”的OGNL写成“#myMap[top]”,如以下代码所示: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
清单4 WebContent/doubleselect.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <title>Struts 2 Cool Tags - <s:doubeselect/ ></title> <s:head /> </head> <body> <h2><s:doubleselect/></h2> <s:form action="Store" > <s:set name="foobar" value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /> <s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology" /> </s:form> </body> </html> 分布运行应用程序,在浏览器中键入:http://localhost:8080/Struts2_CoolTags/doubleselect.action,出现如下图所示页面:
3、<s: token />这个标志可能大家不常用,不过本人认为它还是挺有用的。在使用Struts 1.x时,因为跳转通常是用Forward(而不是Redirect)实现的,所以当用户完成请求后,按“F5”刷新页面时,就会重新提交上次的请求,这样经常会出错。要解决这个问题,<s:token />可以帮你忙。 实现原理在页面加载时,<s: token />产生一个GUID(Globally Unique Identifier,全局唯一标识符)值的隐藏输入框如: <input type="hidden" name="struts.token.name" value="struts.token"/>
清单6 <s:token />的HTML输出
<input type="hidden" name="struts.token" value="BXPNNDG6BB11ZXHPI4E106CZ5K7VNMHR"/> 同时,将GUID放到会话(session)中;在执行action之前,“token”拦截器将会话token与请求token比较,如果两者相同,则将会话中的token删除并往下执行,否则向actionErrors加入错误信息。如此一来,如果用户通过某种手段提交了两次相同的请求,两个token就会不同。 具体实现首先看一下Action的代码: package tutorial;
清单7 src/tutorial/CoolTagAction.java
import com.opensymphony.xwork2.ActionSupport; public class CoolTagAction extends ActionSupport { private static final long serialVersionUID = 6820659617470261780L; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public String execute() { System.out.println("Executing action, your message is " + message); return SUCCESS; } } 以上代码一目了然,再看看JSP的写法: %@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
清单8 WebContent/token.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml"> <head> <title>Struts 2 Cool Tags - <s:token/ ></title> <s:head /> </head> <body> <h2><s:token/></h2> <s:actionerror /> <s:form action="Token" > <s:textfield name="message" label="Message" /> <s:token /> <s:submit /> </s:form> </body> </html> JSP也很简单,就是加入<s:token />标志。接下来是Actoin配置的XML片段: <?xml version="1.0" encoding="UTF-8"?>
清单9 src/struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts./dtds/struts-2.0.dtd"> <struts> <package name="Struts2_COOL_TAGS_DEMO" extends="struts-default"> <action name="Token" class="tutorial.CoolTagAction"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="token" /> <result name="invalid.token">/token.jsp</result> <result>/token.jsp</result> </action> <action name="*"> <result>/{1}.jsp</result> </action> </package> </struts> 以上XML片段值注意的是加入了“token”拦截器和“invalid.token”结果,因为“token”拦截器在会话token与请求token不一致时,将会直接返回“invalid.token”结果。 发布运行应用程序,在浏览器中键入:http://localhost:8080/Struts2_CoolTags/token.jsp,出现如下图所示页面:
随便填点东西并提交页面,一切正常返回以上页面,然后按“F5”刷新页面,在弹出的对话框中点击“Retry”,出现如下图所示页面:
4、<s:datetimepicker />、<s:optiontransferselect />和<s:updownselect />这几个标志的使用相对简单,所以我想小举一例即可,以下是JSP的代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
清单12 WebContent\others.jsp页面
<html xmlns="http://www./1999/xhtml"> <head> <title>Struts 2 Cool Tags - Others</title> <s:head /> </head> <body> <h2>Others</h2> <s:form action="Store" > <s:datetimepicker name="birthday" label="Birthday" /> <s:updownselect label = "Favourite Countries" list="#{'england':'England', 'america':'America', 'germany':'Germany'}" name="prioritisedFavouriteCountries" headerKey="-1" headerValue="--- Please Order Them Accordingly ---" emptyOption="true" /> <s:optiontransferselect label="Favourite Cartoons Characters" name="leftSideCartoonCharacters" leftTitle="Left Title" rightTitle="Right Title" list="{'Popeye', 'He-Man', 'Spiderman'}" multiple="true" headerKey="headerKey" headerValue="--- Please Select ---" emptyOption="true" doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" doubleName="rightSideCartoonCharacters" doubleHeaderKey="doubleHeaderKey" doubleHeaderValue="--- Please Select ---" doubleEmptyOption="true" doubleMultiple="true" /> </s:form> </body> </html> 发布运行应用程序,在浏览器中键入:http://localhost:8080/Struts2_CoolTags/others.jsp,出现如下图所示页面: 总结Struts 2在标志上的确比Struts 1.x丰富了许多,同时模板机制也给程序员带来不少方便(如果你不太喜欢个性化的风格)。另外,Struts 2还有一些AJAX(如<s: autocompleter />等)的标志和非表单的UI标志(如<s: tree />等),我会在以后的文章中讲述其使用。 评论: # re: Strus 2的新表单标志的使用[未登录] 2007-05-21 17:31 | javaman
小弟最近在学struts2 在datetimepicker 遇到一个问题?
class: public class myclass1 extends ActionSupport{ private Date birthday; public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday= birthday; } public String execute(){ return SUCCESS; } } index.jsp: <s:form action="myaction1"> <s:datetimepicker name="birthday" label="生日"/></<br> <s:submit/> </s:form> 问题:在index.jsp页面上始终不能显示时间的选择框,请问是怎么回事呢?? 回复 更多评论 # re: Strus 2的新表单标志的使用[未登录] 2007-05-22 10:27 | javaman
哈哈,问题终于解决了!~
<head><s:head/></head> OK了 解释: Renders parts of the HEAD section for an HTML file. This is useful as some themes require certain CSS and JavaScript includes. If, for example, your page has ajax components integrated, without having the default theme set to ajax, you might want to use the head tag with theme="ajax" so that the typical ajax header setup will be included in the page. The tag also includes the option to set a custom datepicker theme if needed. See calendarcss parameter for description for details. If you use the ajax theme you can turn a debug flag on by setting the debug parameter to true. 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-05-22 22:07 | jezz
我在测试哪个token的时候tomcat控制台报错:
严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Err or setting expression 'struts.token' with value '[Ljava.lang.String;@13576a2' 2007-5-22 21:36:17 com.opensymphony.xwork2.interceptor.ParametersInterceptor set Parameters 严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Err or setting expression 'struts.token.name' with value '[Ljava.lang.String;@ff8c74 ' 2007-5-22 21:36:17 com.opensymphony.xwork2.validator.ActionValidatorManagerFacto ry <clinit> 信息: Detected AnnotationActionValidatorManager, initializing it... Executing action, your message is 23566 2007-5-22 21:36:25 com.opensymphony.xwork2.interceptor.ParametersInterceptor set Parameters 严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Err or setting expression 'struts.token' with value '[Ljava.lang.String;@c68a98' 2007-5-22 21:36:25 com.opensymphony.xwork2.interceptor.ParametersInterceptor set Parameters 严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Err or setting expression 'struts.token.name' with value '[Ljava.lang.String;@354749 ' 2007-5-22 21:36:25 org.apache.struts2.util.TokenHelper validToken 警告: Form token LNTDZ5OTANEM6E0Y0ZDGJSPA7WOXLI4 does not match the session toke n GA8LF6M6WO4PY7YZ0PBXQLTVP3R6ZQS5. 请MAX帮我看下!以后我会经常来的!我也在学struts2 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-07-10 13:36 | yiwuyun
清单4怎么会出现这种情况:
FreeMarker template error! Expression parameters.formName is undefined on line 76, column 43 in template/simple/doubleselect.ftl. The problematic instruction: ---------- ==> ${parameters.formName} [on line 76, column 41 in template/simple/doubleselect.ftl] in include "/${parameters.templateDir}/simple/doubleselect.ftl" [on line 3, column 1 in template/xhtml/doubleselect.ftl] ---------- 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-07-11 22:56 | Max
@yiwuyun
你是否通过下*.action访问页面? ----------------------------------------------------------------- 在浏览器中键入:http://localhost:8080/Struts2_CoolTags/doubleselect.action 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-07-15 21:31 | 蛋白质
<s:optiontransferselect name="admin" list="loves" doubleName="admin2" doubleList="gl" leftTitle="左边的窗口" rightTitle="右边的窗口" label="从左到右,从右到左" headerKey="headerKey" headerValue="---请选择---" doubleHeaderKey="doubleHeaderKey" doubleHeaderValue="---已选择---"></s:optiontransferselect>
教我下,接收有问题啊,name="admin" doubleName="admin2" 要接受这两个东西,action里要定义什么类型?String[] admin; Object[] admin; List admin = new ArraryList();都不行,怎么搞啊 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-07-22 15:13 | mango
运行doubleselect的问题
在浏览器中输入 http://localhost:8080/Struts2_CoolTags/doubleselect.action 这里需要在struts.xml中配置Action="doubleselect"吗 我没有配置,出错信息There is no Action mapped for namespace / and action name doubleselect. 需要的话,class是什么呢 回复 更多评论 # re: Strus 2的新表单标志的使用[未登录] 2007-08-01 09:47 | Allen
关于doubleselect的问题:
我直接Copy过来就出现了错误:我同事机器上运行也有同样的错误,能帮忙给解决下么?谢谢 freemarker.log.JDK14LoggerFactory$JDK14Logger error FreeMarker template error! Expression parameters.formName is undefined on line 76, column 43 in template/simple/doubleselect.ftl. The problematic instruction: ---------- ==> ${parameters.formName} [on line 76, column 41 in template/simple/doubleselect.ftl] in include "/${parameters.templateDir}/simple/doubleselect.ftl" [on line 3, column 1 in template/xhtml/doubleselect.ftl] ---------- 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-10-30 16:35 | sgwood
按照max的做法:直接访问JSP文件不成功,错误提示:
System Runtime Error: An exception occurred processing JSP page /token.jsp at line 8 5: 6: 7: 8: 9: 10: 11: <s:token/> Stacktrace: 5: <html xmlns="http://www./1999/xhtml"> 6: <head> 7: <title>Struts 2 Cool Tags - <s:token/ ></title> 8: <s:head /> 9: </head> 10: <body> 11: <h2><s:token/></h2> Stacktrace:> org.apache.jasper.JasperException: An exception occurred processing JSP page /token.jsp at line 8(省略) 进而用Action去访问,而用http://localhost:8080/myweb/token.action运行可以访问(指向/token.jsp)但提交数据后,显示:16:22:13,109 INFO [STDOUT] 2007-10-30 16:22:13,109 ERROR [com.opensymphony.xwork2.interceptor.ParametersInterceptor] - <ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'MyToken' on 'class tutorial.CoolTagAction: Error setting expression 'MyToken' with value '[Ljava.lang.String;@1af2a9c'> 16:22:13,109 INFO [STDOUT] 2007-10-30 16:22:13,109 ERROR [com.opensymphony.xwork2.interceptor.ParametersInterceptor] - <ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'struts.token.name' on 'class tutorial.CoolTagAction: Error setting expression 'struts.token.name' with value '[Ljava.lang.String;@11674f0'> 提交后刷新能够提示是重复提交,但后台显示: WARN [org.apache.struts2.util.TokenHelper] - <Form token DU5UW3PLUZ17I9PHCB1V7AUUKEMI3V52 does not match the session token 1M148DM2DXLCXDP9SNSBZ828P3B8CX2L.> 上面有位兄弟有相同的错误,max建议改名但还是传报两个ParametersInterceptor (只是其中一个改为MyToken),希望Max能解答下,我初步怀疑是我的项目中的配置(web.xml等)和您的不一样了。 回复 更多评论 # re: Strus 2的新表单标志的使用 2007-11-23 11:12 | sspanzer
@MAX
请教一个问题: 在作doubleselect例子时.copy了doubleselect.jsp,用action来访问.出现如下错误: Messages: There is no Action mapped for namespace / and action name doubleselect. ...... You are seeing this page because development mode is enabled. Development mode, or devMode, enables extra debugging behaviors and reports to assist developers. To disable this mode, set: struts.devMode=false in your WEB-INF/classes/struts.properties file. 用JSP方式来访问则是和上面兄弟一样的错: ==> ${parameters.formName} [on line 98, column 41 in template/simple/doubleselect.ftl] in include "/${parameters.templateDir}/simple/doubleselect.ftl" [on line 25, column 1 in template/xhtml/doubleselect.ftl] 在struts.properties 中加入struts.devMode=false后.action访问出现 HTTP Status 404 - There is no Action mapped for namespace / and action name doubleselect. 意识到是在struts.xml中少了action 加入 <action name="doubleselect" > <result>/doubleselect.jsp</result> </action> 修改doubleselect.jsp为: <s:form action="doubleselect" > action访问成功,后又试JSP方式也可,还请教MAX这是为何? 试着去掉struts.devMode = false后.访问一样可以 google得知struts.devMode = false指定Struts 2处于开发状态 还请解释struts.devMode=false或true有什么区别?对开发有什么影响 谢谢! 回复 更多评论 # re: Strus 2的新表单标志的使用[未登录] 2008-01-09 14:51 | michael
不知道Max 有没有遇到过这个问题。
[14:45:40.753] Expression parameters.doubleId is undefined on line 56, column 108 in template/simple/optiontransferselect.ftl. [14:45:40.753] The problematic instruction: [14:45:40.753] ---------- [14:45:40.753] ==> ${parameters.doubleId?html} [on line 56, column 106 in template/simple/optiontransferselect.ftl] [14:45:40.753] in include "/${parameters.templateDir}/simple/optiontransferselect.ftl" [on line 2, column 1 in template/xhtml/optiontransferselect.ftl] [14:45:40.753] ---------- [14:45:40.753] 回复 更多评论 # re: Strus 2的新表单标志的使用 2008-02-28 09:13 | itanyang
<s:set name="foobar"
value="#{'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'}}" /> <s:doubleselect list="#foobar.keySet()" doubleName="technology" doubleList="#foobar[top]" label="Technology" /> </s:form> 在doubleselect中怎样实现如果选择Java 提交的是1 ,选择 Spring 提交 11 这样的功能呢。 请教MAX 谢谢 itanyang120@163.com 回复 更多评论 |
|
来自: 宾仔 > 《struts2.》