struts2相干配置
struts2相关配置
1.struts2工程的基础包
struts2-core-2.0.14.jar
xwork-2.0.7.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
commons-logging-1.0.4.jar
2.区别于struts1作为servlet加载,struts2是以filter加载的,如:
web.xml
首先,在src下建package,取名locale,创建相关语言包如message_zh_CN.properties,message_en_US.properties等;
然后加载,适用于全局,在struts.xml中添加
基本采用框架验证,除非复杂情况需要在Action类中添加validate<Method>方法
取名方式<ActionClassName>-validation.xml或者
<ActionClassName>-<ActionAliasName>-validation.xml
存放于对应Action相同目录,具体配置如:
第一种非Ioc方式
1.struts2工程的基础包
struts2-core-2.0.14.jar
xwork-2.0.7.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
commons-logging-1.0.4.jar
2.区别于struts1作为servlet加载,struts2是以filter加载的,如:
web.xml
<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>3.struts.xml存放于classes根目录,即src根目录下,基本配置如:
<?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.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action,do"> </constant> <constant name="struts.custom.i18n.resources" value="local.message"> </constant> <package name="demo" extends="struts-default"> <action name="HelloWorld" class="org.xmh.demo.HelloWorld"> <interceptor-ref name="timer"></interceptor-ref> <result>/hello.jsp</result> </action> </package> <package name="test1" extends="struts-default" namespace="/mystruts"> <action name="FirstAction" class="org.xmh.demo.FirstAction"> <result name="positive">/demo/positive.jsp</result> <result name="negative">/demo/negative.jsp</result> </action> <action name="MultiAction" class="org.xmh.demo.MultiAction"> <result name="success">/demo/multiAction/index.jsp</result> <result name="input">/demo/multiAction/index.jsp</result> </action> <action name="ValidatorAction" class="org.xmh.demo.ValidatorAction"> <result name="success">/demo/validate/index.jsp</result> <result name="input">/demo/validate/index.jsp</result> </action> </package> </struts>4.国际化
首先,在src下建package,取名locale,创建相关语言包如message_zh_CN.properties,message_en_US.properties等;
然后加载,适用于全局,在struts.xml中添加
<constant name="struts.custom.i18n.resources" value="local.message">最后编辑jsp,如果是局部加载,需要在<s:form>外层加上
<s:i18n name="locale.message"> <body> <s:form action="Login" method="POST"> <s:textfield name="username" label="%{getText('login.name')}"></s:textfield> <s:textfield name="password" label="%{getText('login.password')}"></s:textfield> <s:submit value="%{getText('login.submit')}"></s:submit> </s:form> </body>5.验证validator
基本采用框架验证,除非复杂情况需要在Action类中添加validate<Method>方法
取名方式<ActionClassName>-validation.xml或者
<ActionClassName>-<ActionAliasName>-validation.xml
存放于对应Action相同目录,具体配置如:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="msg"> <field-validator type="requiredstring"> <message key="requiredstring" /> </field-validator> </field> <field name="age"> <field-validator type="int"> <message key="requiredstring" /> </field-validator> </field> </validators>6.Action获取session,request,response
第一种非Ioc方式
ActionContext.getContext().getSession();或者
HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); HttpSession session = request.getSession();第二种Ioc方式,对应Action类分别实现SessionAware,ServletRequestAware,ServletResponseAware等接口。