`
semi_sleep
  • 浏览: 101582 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

20090317-20090319 (struts)

阅读更多

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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics