Conversion?
Validation?
How to bind values from request to action and from action to jsp?
Struts tag library?
Validation can be described through an XML document, or using annotations. The XML document is named after the Action being validated with a "-validation" suffix.
The convention plugin use certain convention so that you almost don't need to do any configuration.
Interceptors will be executed before and after action, PreResultListeners after action executes but before evaluating result.
Interceptors can be group as interceptor stack, them can be put in action declaration or set as default interceptor of the package.
When apply to all actions, you can use "excludeMethods" and "includeMethods" to determine whether or not to apply to a method.
Use <param> to override "excludeMethods" and "includeMethods" of an interceptor or interceptor stack (<interceptor-name>.<parameter-name>)
A Struts 2 Action instance is created for every request and do not need to be thread-safe. Conversely, Interceptors are shared between requests and must be thread-safe.
Swtich to another action is not recommended. Use ChainingInterceptor to make variables in source action visible to target action.
Actions should be treated as a Transaction Script, rather than as methods in a Business Facade. Ideally, Action classes should be as short as possible. All the core logic should be pushed back to a support class or a business facade, so that Actions only call methods. Actions are best used as adapters, rather than as a class where coding logic is defined.
Action is not suitable for reuse, the lack of support makes it hard to manage, reuse interceptor or business facade. This is what ES don't understand and one of the reasons why it's so ugly.
Common static content that is needed by the framework (JavaScript and CSS files, etc.) is served automatically by the FilterDispatcher filter. Any request starting with "/struts/" denotes that static content is required, and then mapping the value after "/struts/" to common packages in the framework and, optionally in the application's class path.
org.apache.struts2.static
template
(configured in web.xml for the FilterDispatcher filter)
org.apache.struts2.dispatcher.FilterDispatcher
config - a comma-delimited list of XML configuration files to load.
actionPackages - a comma-delimited list of Java packages to scan for Actions.
configProviders - a comma-delimited list of Java classes that implement the ConfigurationProvider interface that should be used for building the Configuration.
loggerFactory - The class name of the LoggerFactory implementation.
* - any other parameters are treated as framework constants.
If we change the filter mapping to something else, for example /*.html, we must take this in to account and extract the content that would normally be served from the Struts 2 jar files, or some other solution.
Use struts.properties to configurate struts (note the different between struts.xml), all properties can also be set using Constant Configuration in an XML configuration file.
The default (struts.xml) file and should reside on the classpath of the webapp.
A base configuration file named struts-default.xml is included in the struts2.jar file. This file is automatically included into struts.xml file to provide the standard configuration settings without having to copy them.
To exclude the struts-default.xml or to provide your own version, see the struts.configuration.files setting in struts.properties.
<bean>
"class", value used to create or retrieve a bean object, may be a class name or a spring bean name or other.
"type" and "name", used to inject this bean in to xwork framework, values for "name" should be unique among "type".
"static", used to inject values inside xwork framework in to this bean, setter methods should be marked using "@inject".
<constant>
Use <constant name="struts.devMode" value="true" /> in struts's xml, and use "name=value" in properties file, use
<init-param>
<param-name>struts.devMode</param-name>
<param-value>true</param-value>
</init-param>
in web.xml
The priority is as follow, note that web.xml has the highest priority.
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
<action>
<default-action-ref>
Wildcard Method, e.g.
<action name="*Crud" class="example.Crud" method="{1}">
<action name="List*s" class="actions.List{1}s">
<result>list{1}s.jsp</result>
</action>
* is not greedy, consider use "ListSponsors" against the previous example.
* Matches zero or more characters excluding the slash ('/') character.
** Matches zero or more characters including the slash ('/') character.
\* matches the character asterisk ('*'), and
\\ matches the character backslash ('\').
In the action mapping and action results, the wildcard-matched values can be accessed with the token {N} where N is a number from 1 to 9 indicating which wildcard-matched value to substitute. The whole request URI can be accessed with the {0} token.
The same for configuration file: e.g. "Crud_input-validation.xml", "Crud_delete-conversion.xml"
Dynamic Method Invocation ("Category!create.action") is not recommended by struts, use Wildcard Method instead.
<action name="*">
<result>/{1}.jsp</result>
</action>
It's important to put a "catchall" wildcard mapping like this at the end of your configuration so it won't attempt to map every request!
Post back, first render a page using an alternate method, like input and then have it submit back to the default execute method.
<s:form>
<s:textfield label="Please enter your name" name="name"/>
<s:submit/>
</s:form>
The form simply submits back to the action that created it.
ResultType default.
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
Dynamic Results
<action name="fragment" class="FragmentAction">
<result name="next" type="redirectAction">${nextAction}</result>
</action>
<bean type="com.opensymphony.xwork2.UnknownHandler" name="handler" class="myclasses.SomeUnknownHandler"/>
<unknown-handler-stack>
<unknown-handler-ref name="handler1" />
<unknown-handler-ref name="handler2" />
</unknown-handler-stack>
Handles unknow action/method/result, and return an action/method/result to be used.
First use ExceptionMappingInterceptor, it will push exception to ValueStack using:
exception - The exception object itself
exceptionStack - The value from the stack trace
Local exception handling:
<action name="DataAccess" class="com.company.DataAccess">
<exception-mapping exception="com.company.SecurityException" result="login"/>
<result name="SQLException" type="chain">SQLExceptionAction</result>
</action>
Global exception handling:
<global-exception-mappings>
<exception-mapping exception="java.sql.SQLException" result="SQLException"/>
<exception-mapping exception="java.lang.Exception" result="Exception"/>
</global-exception-mappings>
<global-results>
<result name="login" type="redirect">/Login.action</result>
<result name="Exception">/Exception.jsp</result>
</global-results>
分享到:
相关推荐
BBS-- STRUTS1.2BBS-- STRUTS1.2BBS-- STRUTS1.2BBS-- STRUTS1.2BBS-- STRUTS1.2
包含Struts2框架的核心类库,以及Struts2的第三方插件类库 struts2-core-2.0.14 xwork-2.0.7 ognl-2.6.11 commons-logging-1.0.4 freemarker-2.3.8 等等。
spring-webmvc-struts.jar对Struts和Spring整合时需要用到的包
跟我学Java_Web--Struts2技术详解,跟我学Java_Web--Struts2技术详解
轻量级J2EE企业应用实战--Struts+Spring+Hibernate整合开发笔记
Struts2学习之二--Struts2标签介绍
Java框架---Struts2 jar包,用于Struts2开发
精通java Web开发--基于Struts EJB.zip 随书光盘 精通java Web开发--基于Struts EJB.zip 随书光盘
精通JavaEE项目案例---基于struts,hibernate,spring 源代码SSH
Java项目案例--基于Struts+hibernate网上书城系统,包含有代码。
里面有个webapps目录,该目录下的struts-documentation.war里就已经包含有struts的api文档
struts2官方例子10--Struts2的Form标签
from表单,生成验证码(纯数字、纯字母、数字加字母)_ajax--------struts版
【JSP--struts】 登陆部分 数据库存放用户和密码 SQL2000
java开发常用jar包之spring--struts.rar
完整OA系统--基于struts2,hibernate,spring3
java面试知识点总结--Struts.pdf
JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. JavaEE主流开源框架-Struts部分rmvb格式. ...
struts2官方例子3--使用Struts2标签(Ant版的),主要是介绍Struts2简单标签的使用情况
深入struts 深入struts 深入struts