68
Struts 2.0(i18n) http://www.blogjava.net/max/category/16130.html Web Struts 1.xFILE-NAME_xx_XX.propertiesFILE- NAMEstruts-config.xml<bean:message> “”Struts 2.0

Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

  • Upload
    others

  • View
    23

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts 2.0(i18n)http://www.blogjava.net/max/category/16130.html

Web

Struts 1.xFILE-NAME_xx_XX.propertiesFILE-NAMEstruts-config.xml<bean:message>

“”Struts2.0

Page 2: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

HelloWorld

——HelloWorldHelloWorld

1. Eclipse Struts 2.0 2. srcstruts.properties

struts.custom.i18n.resources=globalMessages

Struts2.0struts.xmlstruts.propertiesWEB-INF/classes/struts.xmlstruts.propertiesStruts 2.0Runtime

3. srcglobalMessages_en_US.propertiesHelloWorld=HelloWorld!

4. srcglobalMessages_zh_CN.propertiesHelloWorld=

propertiesEclipsepluginUnicodenative2asciiEclipseSoftware Update

1EclipseHelpSoftware UpdateFindandInstall2Install/UpdateSearch fornewfeaturestoinstallNext3InstallNewRemote Site4NewUpdate SiteName“PropEdit”URLhttp://propedit.sourceforge.jp/eclipse/updates/5Site toincludetosearchsiteFinsih6UpdatesSelectthe featurestoinstall“3.1.x”Eclipse3.27Finish8EclipseviEclpisepropertiesP

5. WebContentHelloWorl.jsp<%@pagecontentType="text/html;charset=UTF-8"%>

Page 3: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<%@taglibprefix="s"uri="/struts-tags"%><html><head><title>HelloWorld</title></head><body><h2><s:textname="HelloWorld"/></h2><h2><s:propertyvalue="%{getText('HelloWorld')}"/></h2></body></html>

6. http://localhost:8080/Struts2_i18n/HelloWorld.jsp1

17. “”2

2

Struts1.xStruts 2.0Struts——UIgetTextlabel

Page 4: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<s:textfieldname="name"label="%{getText('UserName')}"/>

Page 5: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts2.0propertiesStruts

1. package2. package.propertiespackage_xx_XX.properties3. ActionActionActionActionActionproperties

ActonOnetitle“”titleActionTwo“”4. <s:i18n>properties Struts 2.0Tag <s:i18n>

<s:i18nname="xxxxx"></s:i18n>xxxxxStruts

Struts2.03

Page 6: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

3

ChildActiongetText("user.title")Struts2.0

1. ChildAction_xx_XX.propertiesChildAction.properties2. ChildActionMyInterface.properties

Page 7: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

3. ChildActionParentActionpropertiesParentAction.properties4. ChildActionModelDrivengetModel()5. package.properties6.7. ValueStackuserusertitle;8. struts.properties1;9. user.title

Page 8: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

runtimeStruts

1. OGNL${}validation.require=${getText(fileName)}isrequired

2. java.text.MessageFormat{ 0,number|date|time|choice,}validation.between=Datemustbetween{0,date,short}and{1,date,short}

1. value0value1...valueN<s:textname="validation.required"value0="UserName"/>

2. paramparam<s:textname="validation.required"><s:paramvalue="UserName"/></s:text>

Page 9: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

——StrutsActionContext.getContext().setLocale(Localearg)ScenarioStruts 2.0i18nInterceptorInterceptorActioni18n"request_locale"LocaleLocaleLocalesession“WW_TRANS_I18N_LOCALE”

packagetutorial;

importjava.util.Hashtable;importjava.util.Locale;importjava.util.Map;

publicclassLocales{publicMap<String,Locale>getLocales(){Map<String,Locale>locales=newHashtable<String,Locale>(2);locales.put("AmericanEnglish",Locale.US);locales.put("SimplifiedChinese",Locale.CHINA);returnlocales;}}

tutorial/Locales.java

<%@taglibprefix="s"uri="/struts-tags"%><scripttype="text/javascript"><!--functionlangSelecter_onChanged(){document.langForm.submit();}//--></script>

Page 10: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<s:setname="SESSION_LOCALE"value="#session['WW_TRANS_I18N_LOCALE']"/><s:beanid="locales"name="tutorial.Locales"/><formaction="<s:urlincludeParams="get"encode="true"/>"name="langForm"style="background-color:powderblue;padding-top:4px;padding-bottom:4px;">Language:<s:selectlabel="Language"list="#locales.locales"listKey="value"listValue="key"value="#SESSION_LOCALE==null?locale:#SESSION_LOCALE"name="request_locale"id="langSelecter"onchange="langSelecter_onChanged()"theme="simple"/></form>LangSelector.jsp

LangSelector.jspLocalesMaplocalesselectLangSelector<s:form>Javascript“reqeust_locale”Actionsession“WW_TRANS_I18N_LOCALE”locale

LangSelector.jspJSP<s:includevalue="/LangSelector.jsp"/>

1HellloWorld.jsp<body>struts.xmlAction<actionname="HelloWorld"><result>/HelloWorld.jsp</result></action>

JSPJSPAction<actionname="*"><result>/{1}.jsp</result></action>

http://localhost:8080/Struts2_i18n/HelloWorld.action4

Page 12: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts 2.0http://www.blogjava.net/max/category/16130.html

StrutsMVC2WebWebStruts

MVC2TagLibrary

“”Struts

“”

WebStruts2.0

1.1. ApacheStruts2.0

Page 13: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

2. Eclipse3.2Web

File\New\Project1

Page 14: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

1Web\DynamicWeb Project“Next”2

Page 15: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

2Web“Project Name”Struts2_HelloWorld“New”

Page 16: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

3“Apache\Apache Tomatv5.5”“Next”

4“Finish”

3. Struts2.0libjarbuildpath

Page 17: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

5Struts 2.0libctr+aEclipse“Project Explorer”Struts2_HelloWorld\WebContent\WEB-INF\libEclipse“Project Explorer”jarStruts2_HelloWorld\JavaResources:src\Libraries\WebAppLibraries6

6ProjectExplorer

4. web.xml

Page 18: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<?xmlversion="1.0"encoding="ISO-8859-1"?><!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><display-name>Struts2.0HelloWorld</display-name><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list></web-app>

5. struts.xml

Struts2_HelloWorld\JavaResources:src7

7 Other“Other”8

Page 19: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

8“Next”9

Page 20: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

9“Filename”sturts.xml“Finish”struts.xml

<!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><includefile="struts-default.xml"/></struts>

6. index.html

Page 21: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts2_HelloWorld\WebContent10

10 Other“Other”11

Page 22: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

11Web\HTML“Next”12

Page 23: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

12HTML“File Name”index.html“Next”13

Page 24: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

13“Finish”index.html

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"><title>HelloWorld</title></head><body><h3>HelloWorld!</h3>

Page 25: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

</body></html>

7. tomcat

Struts_HelloWorld14

14“Export\WAR file”15

Page 26: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

15“Web\WARfile”“Next”16

Page 27: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

16war%tomcat%\webapps\Struts2_HelloWorld.war“Finish”

8. tomcat

Internet Explorerhttp://localhost:8080/Struts2_HelloWorld/17

17 HelloWorld

2. Struts2.0——HelloWorld

Page 28: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

1. package

Struts2_HelloWorld\JavaResources:src18

18 "“New\Package”19

19Java“Name”tutorial“Finish”

2. HelloWorld.java

Struts2_HelloWorld\JavaResources:src\tutorial20

20“New\Class”21

Page 29: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

21 Java“Name”HelloWorld“Superclass”com.opensymphony.xwork2.ActionSupport“Finish”HelloWorld.java

packagetutorial;

importcom.opensymphony.xwork2.ActionSupport;

publicclassHelloWorldextendsActionSupport{privateStringname;publicStringgetName(){returnname;

Page 30: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

}publicvoidsetName(Stringname){this.name=name;}publicStringexecute(){name="Hello,"+name+"!";returnSUCCESS;}}

3. struts.xmlaction(mapping)<!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><includefile="struts-default.xml"/><packagename="tutorial"extends="struts-default"><actionname="HelloWorld"class="tutorial.HelloWorld"><result>HelloWorld.jsp</result></action></package></struts>

4. SayHello.jsp

“index.html”22

Page 31: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

22“Next”,23

Page 32: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

23JSP“File name”SayHello.jsp“Next”24

Page 33: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

24“Finish”SayHello.jsp

<%@pagecontentType="text/html;charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>SayHello</title></head><body><h3>Say"Hello"to:</h3>

Page 34: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<s:formaction="HelloWorld">Name:<s:textfieldname="name"/><s:submit/></s:form></body></html>

5. HelloWorld.jsp()HelloWorld.jsp<%@pagecontentType="text/html;charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>Hello</title></head><body><h3><s:propertyvalue="name"/></h3></body></html>

6.

tomcat, tomcatwebappsStruts2_HelloWorld.warStruts2_HelloWorld“tomcat”

7. tomcat

Internet Explorerhttp://localhost:8080/Struts2_HelloWorld/SayHello.jsp25

Page 35: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

25 SayHello.jsp“Name”WorldSubmitHelloWorld.jsp26

26HelloWorld.jsp

3. HelloWorld

StrutsStruts2.0

1. JUnit

Page 36: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts2_HelloWorld\JavaResources:src\tutorial27

27“Next\Other”

28“Java\JUnit\JUnitTestCase”“Next”

Page 37: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

29JUnit“NewJUnit4 test”“Name”HelloWorldTest“Classundertest”tutorial.HelloWorld“Next”

Page 38: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

30HelloWorld\executeFinishHelloWorldTest.javaIconJUnit 4jar

Struts2_HelloWorld

Page 39: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

31“BuildPath\Add Libararis”32

32“JUnit”“Next”

Page 40: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

33“JUnit4”“Finish”HelloWorldTest.java

packagetutorial;

importstaticorg.junit.Assert.assertTrue;

importorg.junit.Test;

importcom.opensymphony.xwork2.ActionSupport;

publicclassHelloWorldTest{

@TestpublicvoidtestExecute(){HelloWorldhello=newHelloWorld();hello.setName("World");Stringresult=hello.execute();assertTrue("Expectedasuccessresult!",ActionSupport.SUCCESS.equals(result));

Page 41: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

finalStringmsg="Hello,World!";assertTrue("Expectedthedefaultmessage!",msg.equals(hello.getName()));}

}

2.

Struts2_HelloWorld\JavaResources:src\tutorial\HelloWorldTest.java34

34“RunAs\JUnit Test”JUnit35

35JUnit35

4.

Web->Action->Struts

Page 43: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

@struts-default.xmlstruts2-core-2.0.0.jar

#re:Struts2.02006-11-0223:14| Tin

MaxWebWork http://webwork.javascud.org/http://wiki.javascud.org/display/ww2cndoc/WebWorkWebWork2.2Struts2.0.1WebWork 2.2.xWebWork2

#re:Struts2.02006-11-1000:47| Goingmm[]

struts2-all-2.0.1.jarstruts2-core-2.0.1.jar2struts-default.xmlstruts-2.0.1libjar562.0.1struts2-extras-2.0.0.jar

#re:Struts2.02006-11-2221:42|default-config.xmlincludestruts.xml

#re:Struts2.02006-11-2618:32| MaxDefault-config.xmlcontainssomeinterceptorstacksandcommonconfigurations.Includingitcanhelpyoutocreateactioneasier

#re:Struts2.02006-11-2712:36|

Page 45: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

#re:Struts2.02006-12-0721:20| konhon...,

#re:Struts2.02006-12-1415:04| xanbio@copy

#re:Struts2.02006-12-1415:07| xanbio1.classpathstruts.properties2.tomcatconttextreloadtrue

#2007-04-0317:44| lhxregister.jsp action“user.username”,<%@pagecontentType="text/html;charset=GBK"%><%@taglibprefix="s"uri="/struts-tags"%><html><head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>SignOn</title></head><body><s:formaction="RegisterAction"><table><tr><td></td><td><s:textfieldkey="user.username"/></td></tr><tr><td></td><td><s:textfieldkey="user.address"/></td></tr>

Page 46: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<tr><tdcolspan="2"><s:submit/></td></tr></table></s:form>

#re:Struts2.02007-04-0722:56| Max@lhx<%@pagecontentType="text/html;charset=GBK"%>GBK<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">gb2312utf-8

#re:Struts2.02007-04-0914:53| open@

HelloWorld

HelloWorld.action

#re:Struts2.02007-04-1813:36| jiajiatomcatstrurs2default-config.xmlstruts.xml

#re:Struts2.02007-04-1823:05| [email protected],struts-plugin.xml,struts.xml

#re:Struts2.02007-04-1909:49| jiajia

Page 47: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

maxstruts-plugin.xmljar

#re:Struts2.02007-04-1923:19| Max

@jiajiastruts-plugin.xmlpluginjarstruts2-spring-plugin-2.0.6.jar

#re:Struts2.02007-04-1923:28| [email protected] 2filterweb.xml<web-app></web-app><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter>

<filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>

#re:Struts2.02007-05-1017:31| gpiaofei2006

SayHello.jspHellonull

#re:Struts2.02007-05-1313:58| gpiaofei2006#re:Struts2.02006-10-2622:57|esmiles

HelloWorld.jsp<s:propertyname="name"/>

Page 48: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

namevalue<s:propertyvalue="name"/>

HelloWorld.jsp<s:propertyname="name"/>Hello,null;

Page 49: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

Struts 2.0Taghttp://www.blogjava.net/max/category/16130.html

Struts 2.0 Struts2.0 Struts 2.0 [email protected]

Struts1.xStruts2.0TagLibrary

Struts1.x Struts2.0HTMLTilesLogicBean

URI“/struts-tags”UIUI

expressionlanguages EL OGNLJSTLGroovyVelcity

“”“”Struts

JSPStruts 2.0JSP<%@taglibprefix="s"uri="/struts-tags"%>

1. UIifelseifelse

test Boolean elseid Object/String idUIHTMLid

<%@pagecontentType="text/html;charset=UTF-8"%>

Page 50: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>ConditionFlow</title></head><body><h3>ConditionFlow</h3><!--#parameters.name[0]nameincludeparamname#parameters#parameters.name

includeincluded#parametersrequest.getParameter("name")--><s:setname="name"value="<%="'"+request.getParameter("name")+"'"%>"/><s:iftest="#name=='Max'">Max'sfilehere</s:if><s:elseiftest="#name=='Scott'">Scott'sfilehere</s:elseif><s:else>Other'sfilehere</s:else></body></html>1condition.jsp

iterator

Page 51: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

java.util.Collectionjava.util.Iterator

status String IteratorStatusvalue Object/String iteratableListid Object/String idUIHTMLid

<%@pagecontentType="text/html;charset=UTF-8"%><%@pageimport="java.util.List"%><%@pageimport="java.util.ArrayList"%><%@taglibprefix="s"uri="/struts-tags"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><%Listlist=newArrayList();list.add("Max");list.add("Scott");list.add("Jeffry");list.add("Joe");list.add("Kelvin");request.setAttribute("names",list);%><html><head><title>Iterator</title></head><body><h3>Names:</h3><!--1propertyiterator2statusstutsiterator#stutsIteratorStatusIteratorS

Page 52: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

tatus--><ol><s:iteratorvalue="#request.names"status="stuts"><s:iftest="#stuts.odd==true"><li>White<s:property/></li></s:if><s:else><listyle="background-color:gray"><s:property/></li></s:else></s:iterator></ol></body></html>2iterator.jsp

i18n

textaction

value Object/String com.xxxx.resources.AppMsgid Object/String idUIHTMLid

HelloWorld=HelloWrold!3classes\ ApplicationMessages.properties

<%@pagecontentType="text/html;charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%>

Page 53: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>Internationization</title></head><body><h3><s:i18nname="ApplicationMessages"><s:textname="HelloWorld"/></s:i18n></h3></body></html>3i18n.jsp

include

servletservletjsp

value String jspservletid Object/String idUIHTMLid

<%@pagecontentType="text/html;charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>Iterator</title>

Page 54: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

</head><body><h3>InteratorPage</h3><s:includevalue="/condition.jsp"><s:paramname="name">Max</s:param></s:include><h3>i18n</h3><s:includevalue="/i18n.jsp"/></body></html>4include.jsp

param

,includebean. name,ComponentaddParameter(String,Object),UnnamedParametric(TextTag)

value,valuetext,:<paramname="color">blue</param><!--(A)--><paramname="color"value="blue"/><!--(B)-->(A)Stringstatck.(B)java.lang.Objectstatck.

name Stringvalue String valueid Object/String idUIHTMLid

4

set

Page 55: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

set,:

name String

scope String ,application,session,request,page,action.

value Object/Stringid Object/String idUIHTMLid

1

text

actionresource bundle,message,tagbodymessage,tagbody,messagenamemessage

name Stringid Object/String idUIHTMLid

3

url

url,"param"request

includeParams'all''get', param,

Page 56: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<%@pagecontentType="text/html;charset=UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><title>URL</title></head><body><h3>URL</h3><ahref='<s:urlvalue="/i18n.jsp"/>'>i18n</a><br/><s:urlid="url"value="/condition.jsp"><s:paramname="name">Max</s:param></s:url><s:ahref="%{url}">if\elseif\else</s:a></body></html>5url.jsp

property

'value',value,

default String nulldefaultescape true Booelean escapeHTMLvalue Objectid Object/String idUIHTMLid

Page 57: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

2

2. UI

UIUIUIUIStruts1.xHTMLStrutsdoubleselecttimepickeroptiontransferselect

http://wiki.javascud.org/display/ww2cndoc/TagsWebWork2

http://cwiki.apache.org/WW/tag-reference.htmlTagDevelopersGuide

Struts2.0ShowCase

/**$Id:UITagExample.java4203852006-07-1000:57:05Zmrdon$**Copyright2006TheApacheSoftwareFoundation.**LicensedundertheApacheLicense,Version2.0(the"License");*youmaynotusethisfileexceptincompliancewiththeLicense.*YoumayobtainacopyoftheLicenseat**http://www.apache.org/licenses/LICENSE-2.0**Unlessrequiredbyapplicablelaworagreedtoinwriting,software*distributedundertheLicenseisdistributedonan"ASIS"BASIS,*WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.*SeetheLicenseforthespecificlanguagegoverningpermis

Page 58: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

sionsand*limitationsundertheLicense.*/packageorg.apache.struts2.showcase;

importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.opensymphony.xwork2.Validateable;importcom.opensymphony.xwork2.util.OgnlValueStack;

importjava.util.ArrayList;importjava.util.Collections;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.io.File;

/***/publicclassUITagExampleextendsActionSupportimplementsValidateable{privatestaticfinallongserialVersionUID=-94044809860988047L;Stringname;Datebirthday;Stringbio;StringfavoriteColor;Listfriends;booleanlegalAge;Stringstate;Stringregion;Filepicture;StringpictureContentType;StringpictureFileName;StringfavouriteLanguage;StringfavouriteVehicalType="MotorcycleKey";

Page 59: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

StringfavouriteVehicalSpecific="YamahaKey";ListleftSideCartoonCharacters;ListrightSideCartoonCharacters;ListfavouriteLanguages=newArrayList();ListvehicalTypeList=newArrayList();MapvehicalSpecificMap=newHashMap();Stringthoughts;publicUITagExample(){favouriteLanguages.add(newLanguage("EnglishKey","EnglishLanguage"));favouriteLanguages.add(newLanguage("FrenchKey","FrenchLanguage"));favouriteLanguages.add(newLanguage("SpanishKey","SpanishLanguage"));VehicalTypecar=newVehicalType("CarKey","Car");VehicalTypemotorcycle=newVehicalType("MotorcycleKey","Motorcycle");vehicalTypeList.add(car);vehicalTypeList.add(motorcycle);Listcars=newArrayList();cars.add(newVehicalSpecific("MercedesKey","Mercedes"));cars.add(newVehicalSpecific("HondaKey","Honda"));cars.add(newVehicalSpecific("FordKey","Ford"));Listmotorcycles=newArrayList();motorcycles.add(newVehicalSpecific("SuzukiKey","Suzuki"));motorcycles.add(newVehicalSpecific("YamahaKey","Yamaha"));

Page 60: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

vehicalSpecificMap.put(car,cars);vehicalSpecificMap.put(motorcycle,motorcycles);}publicListgetLeftSideCartoonCharacters(){returnleftSideCartoonCharacters;}publicvoidsetLeftSideCartoonCharacters(ListleftSideCartoonCharacters){this.leftSideCartoonCharacters=leftSideCartoonCharacters;}publicListgetRightSideCartoonCharacters(){returnrightSideCartoonCharacters;}publicvoidsetRightSideCartoonCharacters(ListrightSideCartoonCharacters){this.rightSideCartoonCharacters=rightSideCartoonCharacters;}publicStringgetFavouriteVehicalType(){returnfavouriteVehicalType;}publicvoidsetFavouriteVehicalType(StringfavouriteVehicalType){this.favouriteVehicalType=favouriteVehicalType;}publicStringgetFavouriteVehicalSpecific(){returnfavouriteVehicalSpecific;}publicvoidsetFavouriteVehicalSpecific(StringfavouriteVehicalSpecific){

Page 61: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

this.favouriteVehicalSpecific=favouriteVehicalSpecific;}publicListgetVehicalTypeList(){returnvehicalTypeList;}publicListgetVehicalSpecificList(){OgnlValueStackstack=ServletActionContext.getValueStack(ServletActionContext.getRequest());ObjectvehicalType=stack.findValue("top");if(vehicalType!=null&&vehicalTypeinstanceofVehicalType){Listl=(List)vehicalSpecificMap.get(vehicalType);returnl;}returnCollections.EMPTY_LIST;}publicListgetFavouriteLanguages(){returnfavouriteLanguages;}

publicStringexecute()throwsException{returnSUCCESS;}

/* GettersandSetters */publicStringdoSubmit(){returnSUCCESS;}//===innerclasspublicstaticclassLanguage{Stringdescription;Stringkey;

Page 62: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

publicLanguage(Stringkey,Stringdescription){this.key=key;this.description=description;}publicStringgetKey(){returnkey;}publicStringgetDescription(){returndescription;}}publicstaticclassVehicalType{Stringkey;Stringdescription;publicVehicalType(Stringkey,Stringdescription){this.key=key;this.description=description;}publicStringgetKey(){returnthis.key;}publicStringgetDescription(){returnthis.description;}publicbooleanequals(Objectobj){if(!(objinstanceofVehicalType)){returnfalse;}else{returnkey.equals(((VehicalType)obj).getKey());}}publicinthashCode(){returnkey.hashCode();}

Page 63: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

}publicstaticclassVehicalSpecific{Stringkey;Stringdescription;publicVehicalSpecific(Stringkey,Stringdescription){this.key=key;this.description=description;}publicStringgetKey(){returnthis.key;}publicStringgetDescription(){returnthis.description;}publicbooleanequals(Objectobj){if(!(objinstanceofVehicalSpecific)){returnfalse;}else{returnkey.equals(((VehicalSpecific)obj).getKey());}}publicinthashCode(){returnkey.hashCode();}}}

6 org.apache.struts2.showcase.UITagExample.java

<%@pagecontentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><html><head><title>UITagsExample</title><s:head/></head>

Page 64: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<body>

<s:actionerror/><s:actionmessage/><s:fielderror/>

<s:formaction="exampleSubmit"method="post"enctype="multipart/form-data"tooltipConfig="#{'jsTooltipEnabled':'true'}"><s:textfieldlabel="Name"name="name"tooltip="EnteryourNamehere"/>

<s:datepickertooltip="SelectYourBirthday"label="Birthday"name="birthday"/>

<s:textareatooltip="EnteryourBiography"label="Biograph"name="bio"cols="20"rows="3"/>

<s:selecttooltip="ChooseYourFavouriteColor"label="FavoriteColor"list="{'Red','Blue','Green'}"name="favoriteColor"emptyOption="true"headerKey="None"headerValue="None"/>

<s:selecttooltip="ChooseYourFavouriteLanguage"label="FavouriteLanguage"

Page 65: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

list="favouriteLanguages"name="favouriteLanguage"listKey="key"listValue="description"emptyOption="true"headerKey="None"headerValue="None"/>

<s:checkboxlisttooltip="ChooseyourFriends"label="Friends"list="{'Patrick','Jason','Jay','Toby','Rene'}"name="friends"/>

<s:checkboxtooltip="ConfirmedthatyourareOver18"label="Age18+"name="legalAge"/>

<s:doubleselecttooltip="ChooseYourState"label="State"name="region"list="{'North','South'}"value="'South'"doubleValue="'Florida'"doubleList="top=='North'?{'Oregon','Washington'}:{'Texas','Florida'}"doubleName="state"headerKey="-1"headerValue="----------PleaseSelect----------"emptyOption="true"/>

<s:doubleselecttooltip="ChooseyourVehical"label="FavouriteVehical"name="favouriteVehicalType"list="vehicalTypeList"

Page 66: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

listKey="key"listValue="description"value="'MotorcycleKey'"doubleValue="'YamahaKey'"doubleList="vehicalSpecificList"doubleListKey="key"doubleListValue="description"doubleName="favouriteVehicalSpecific"headerKey="-1"headerValue="----------PleaseSelect----------"emptyOption="true"/>

<s:filetooltip="UploadYourPicture"label="Picture"name="picture"/><s:optiontransferselecttooltip="SelectYourFavouriteCartoonCharacters"label="FavouriteCartoonsCharacters"name="leftSideCartoonCharacters"leftTitle="LeftTitle"rightTitle="RightTitle"list="{'Popeye','He-Man','Spiderman'}"multiple="true"headerKey="headerKey"headerValue="---PleaseSelect---"emptyOption="true"doubleList="{'Superman','MickeyMouse','DonaldDuck'}"doubleName="rightSideCartoonCharacters"doubleHeaderKey="doubleHeaderKey"doubleHeaderValue="---PleaseSelect---"doubleEmptyOption="true"doubleMultiple="true"/><s:textarea

Page 67: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

label="YourThougths"name="thoughts"tooltip="Enteryourthoughtshere"/><s:submitonclick="alert('aaaa');"/><s:resetonclick="alert('bbbb');"/></s:form></body></html>6example.jsp

<actionname="example"class="org.apache.struts2.showcase.UITagExample"><result>example.jsp</result><resultname="input">example.jsp</result></action>6struts.xml

postedon2006-10-1812:02Max(7119)(56): Struts2.0

:

#re:Struts2.0Tag2006-12-0111:21| lxfstruts

#re:Struts2.0Tag2006-12-0116:36| Max<s:form>theme="simple"form:<s:formaction="Login"method="POST"theme="simple"><table><tr><td>Username:<s:textfieldname="name"label="Username"/></td>

Page 68: Struts 2.0(i18n) //documentation.help/Struts-2.0-cn/documentation.pdf · Struts 1.xStruts 2.0Tag Library Struts 1.x Struts 2.0 HTMLTiles LogicBean URI“/struts-tags” UIUI expression

<td>Password:<s:passwordname="password"label="Password"/></td></tr></table></s:form>