Struts 2
o 1.1 Struts 2 environment
+ Class Library 1.1.1 complete list
+ Foundation class library 1.1.2 list
+ 1.1.3 basic configuration
o 1.2 configuration file
o 1.3 Action
o 1.4 I18N/I10N
o 1.5 blocker
+ 1.5.1 elements
# 1.5.1.1 in implementation of Struts 2 File Upload
# 1.5.1.2 at Struts 2.0 in international (i18n) applications
# 1.5.1.3 in implementation of Struts 2.0 form data validation (Validation)
Struts 2 at # 1.5.1.4 Data Type Conversion
# 1.5.1.5 implementation of Struts 2 at IoC
# 1.5.1.6 achieve a Struts2 with Spring's IOC procedure
Struts 2
Struts 2 environment
A complete list of libraries
antlr-2.7.2.jar commons-beanutils-1.6.jar commons-chain-1.1.jar commons-logging-1.0.4.jar commons-logging-api-1.1.jar commons-validator-1.3.0.jar freemarker-2.3.8.jar ognl-2.6.11.jar oro-2.0.8.jar struts-core-1.3.5.jar struts2-codebehind-plugin-2.0.11.1.jar struts2-config-browser-plugin-2.0.11.1.jar struts2-core-2.0.11.1.jar struts2-jasperreports-plugin-2.0.11.1.jar struts2-jfreechart-plugin-2.0.11.1.jar struts2-jsf-plugin-2.0.11.1.jar struts2-pell-multipart-plugin-2.0.11.1.jar struts2-plexus-plugin-2.0.11.1.jar struts2-sitegraph-plugin-2.0.11.1.jar struts2-sitemesh-plugin-2.0.11.1.jar struts2-spring-plugin-2.0.11.1.jar struts2-struts1-plugin-2.0.11.1.jar struts2-tiles-plugin-2.0.11.1.jar tiles-api-2.0.4.jar tiles-core-2.0.4.jar tiles-jsp-2.0.4.jar xwork-2.0.4.jar
List of basic library
commons-logging-1.0.4.jar freemarker-2.3.8.jar ognl-2.6.11.jar struts2-core-2.0.11.jar xwork-2.0.4.jar
Basic configuration
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>
Struts.xml
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<!-- Add packages here -->
</struts> Configuration file
struts.xml location: classes under the Action configuration
package
struts of the organizational structure of the configuration
Similar to java package
Property:
* Name: Name of the package, the only marked
extends: inheritance
namespace: namespace
abstract: abstract affirm
namespace
1 default namespace ""
2 root namespace "/"
The order of 3 action english
The current namespace --- up a level english
Web application requests the path be regarded as namespace
4 visits / aaa.action when the order of search
"/" ---> ""
5 namespace NOT directory information
6 package contains the 0 - N An action
<pre>
; Action: <pre>
name: Unique label name.action ---> action name
class: implementation inheritance actionsupport class category
method: Ways to call the name of the dynamic method calls
Result
name: the return to match action value
type: is the result-type for predefined / custom common dispatcher, forward
<result-types>
<result-type name="dispatcher" class ="" default="true"> </ result-type>
</ result-types>
Special place:
<global-results>
<result name=""> </ result>
....
</ global-results>
Special parameters:
<result name="next_opertor"> $ (var_name) </ result>
var_name must be action inside a private property, and have get / set methods
Must comply with the bean naming rules: lowercase at the beginning of
set / get capital at the beginning of the property
include
Struts tag can be placed only in the next level
To the introduction of other struts configuration file
file = ""
He introduction of xml file is a standard configuration of the struts
interceptors => blocker
<interceptors>
<interceptor name=""/>
<intercptor-stack name="">
<interceptor-ref name=""/>
<interceptor-ref name=""/>
</interceptor-stack>
<interceptors>
<default-interceptor-ref name="">
<action 。。。。。。 >
<interceptor-ref name="" /> constants
<constant name="" value="" />
Can achieve
the work done struts.properties
Can achieve
the work accomplished by constant
struts.properties
Struts configuration properties (dynamic method invocation, the virtual machine)
struts-default.xml struts ... jar inside ,----> Zero Configuration templates struts-default.vm
Action
I18N/I10N
Interceptor
1. Interceptors in struts 2 after receiving the request, before the arrival of Action, as well as play a role after leaving the Action
1. Arrived in Action, you can handle parameters (file upload, struts2 interceptors have built-in), or to intercept the request
2. To leave the Action, the Action can handle the return Result (nested template blocker)
2. Interceptor configuration
1. Struts provide their own default interceptors,
2. Interceptor the first step in the use, configuration, can be understood as the registration
3. Blocker used in the second step, configuration, definition of default interceptor-stack, or a directly specified in the use of interceptors Action
4. A time when the use of interceptors to Stack unit
5. Interceptors can be covered, taking the nearest interceptor configuration:: namespace or scope of action within the scope of
6. Blocker sequence of trip: In accordance with the contents of configuration files, top-down (before the arrival of Action), bottom (after leaving the Action)
Registered Interceptors :
<interceptors>
<interceptor name="name_1"
/>
.......
</interceptors>
Registered Interceptor -stack:
<interceptor-stack name="stack_name_1">
<interceptor-ref name="name_1"/>
.......
</interceptor-stack>
Use the Interceptor -stack
In the package definition:
<default-interceptor-ref name="stack_name_1"/>
In the Action defined in the
<action .......>
<interceptor-ref name="stack_name_1"/>
</action>
<pre>
## The system comes with interceptor
###Alias Interceptor
#### Convert parameter name , Content does not change <pre>
name="alias"
1. Read implementation interceptor parameters Parameters Interceptor
1. Put the request parameters in place to Action: parameters --- "Action of the Property (attribute)
2. Placed in the default stack, in the case of coverage can be directly used
name = "params"
2. Internationally implementation of I18n Interceptor Interceptor
1. The provision of documents to read the internationalization of key properties of the Ways
2. Placed in the default stack, in the case of coverage can be directly used
name = "i18n"
3. Implementation of read and write cookie blocker and configure the cookie Interceptor
1. Action in the operation of the provision of the Ways Cookie
2. Placed in the default stack, in the case of coverage can be directly used
name = "cookie"
4. Struts2 validation implementation interceptor function validation Interceptor
1. Placed in the default stack, in the case of coverage can be directly used
2. Action at the implementation of the default configuration or the method call before the validation methods
name = "validation"
5. Struts implementation of data (parameters) before and after the sharing Chainning Interceptor
1. Action can read the context before and after the parameters (such as the attributes of the previous Action)
2. To put all of the Action property of the attribute placed in the context of
6. Create Session Interceptor automatically creates a Session
1. Provide other based on the Session of the Interceptor / Action to provide session
name = "createSession"
7. Abnormal Exception Interceptor Interceptor
1. The abnormal positioning of the page to a precise
name = "exception"
8. To provide Session with Action operation Application (Servlet context) Ways of Scope Interceptor
1. Action Direct operating session / application method
name = "scope"
9. File Upload fileuploader interceptor interceptor
1. Struts2-based file upload function
name = "fileUpload"
10. Logger Interceptor output the name of Action
Points
In the implementation of Struts 2 File Upload
1. Jar files needed
Library of struts2 basic (commons-logging-1.0.4.jar logging, freemarker-2.3.8.jar template implementation, ognl-2.6.11.jar onnl language support, struts2-core-2.0.11.jar struts2 the core class library, xwork-2.0.4.jar struts2 library of foundation)
apache upload components (commons-fileupload-x.xxxx.jar)
apache io components (commons-io-x.xx.xx.jar)
2. Required by the configuration file
; struts.properties:
In that document a need to amend the temporary file storage path, file size
struts.multipart.saveDir = / upload directory to preserve, it is worth are realpath
struts.multipart.maxSize = 209715200 Upload file size limit
struts.multipart.parser = jakarta analytical information upload (multipart / form-data,
Upload file the form when a certain type of data that is the case)
; struts.xml:
In that paper, the type of configuration file to the form of parameters placed on the interceptor fileUpload cited in
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image / png, application / pdf
</ param>
</ interceptor-ref>
3. Required by the operation of
At Action, the MultiPartRequestWrapper to obtain direct access to upload files and parameters (file upload time
Parameters to obtain through normal channels can not be --- params interceptor to us the necessary parameters, because the interceptors could not be resolved multipart
Type of request)
This time through our Request received ServletActionContext itself is a real MultiPartRequestWrapper
Example, we have pre-conversion to the type of object.
multipartInfo.getParameter ( "") to be required of the general parameters of our
multipartInfo.getFiles (""); required documents to be our target (but we have received the document, the document
Has been uploaded and stored in struts.properties directory as defined in, the Request has been back to the client when
Temporary files will be deleted)
Struts 2.0 in at international (i18n) applications
1. Text of the use of struts tags can be read at struts.properties message file defined in the message text file
struts.custom.i18n.resources definition file name (including path information, including path information, the path separator to use.)
struts.locale = zh-cn definition of default Location
Actually read the documents: the file name + Locale value +. Properties
If you can not find the file, reads the file name +. Properties
If you still can not find the file, indicating the value of key
At all the struts ui tag (the tag for the screen has a key property, can be directly read on the message file corresponding to the value of key
When non-ASCII CODE, we need to use local language characters coding the content of the preparation, and then provided the use of jdk
native2ascii transcoding tools become iso8859-1 encoded text file, or else there will be garbled, the operation and IDE (Eclipse) established
Set the default character set, the document character set, etc. has nothing to do
2. The use of struts, as well as tags that can be used in Action manner getText message to the message text file
Text in the struts of the tag is also called the method
Action at the parent class has defined ActionSupport the method can be used directly
Action at Medium (does not contain jsp page, the template page), Do not need the definition of package.properties document, but you can read the file directly in the corresponding key-value information,
package.properties local news paper
document the overall message struts.custom.i18n.resources
The definition of the local news papers, local news paper will be the first to be read.
The definition of the same file name filename_local.properties
3. Struts2 document the use of local resources can to get parameter
Java.util.Locale are examples of parameters
Set up the way: ActionContext.getContext (). SetLocale (locale parameters)
Through other means to set the locale, or self-realization, but is not the way of implementation of Struts2.
4. Struts2 internationalization of the possible there is garbled, garbled solution after
Reference document character set (jsp there are three: jsp own two: contentType and the charset
pageEncoding, html a: meta of the contentType the charset, implementation template file based on different templates, but with similar jsp), do not meet pre-defined, or the three different character sets.
Back to the client server data encoding and the client request does not meet the
The document itself is a garbled message: garbage characters are not necessarily prepared, but used to read when the document character set with the province's character
Not been set, by default when using the operating system reads the default character set their own reading, there are four separate xp itself (one of the three are unicode, unicode compatibility of different types complement each other - ASCII an exception)
Solution:
1, switch to code or filter, can be registered to use the character set to provide apache filter ()
2, update the local message file character set, coding content, (save for the new character set files into the new character set or encoding of the document provided by the use of java into native2ascii the iso8859-1 character set)
3, modified jsp / template file to the relevant character set definition, if the definition of both jsp their charset is not always, the compiler itself would go wrong, need to amend the agreement only if the modifications are in line with still garbled, can be replaced with new character set
In fact, only the character set will involve: utf-8 / iso8859-1 / gbk
CJK character sets: Chinese, Japanese and Korean Character Set JPN-xxxx; outsourcing project development will be encountered when
Struts 2.0 in the implementation of form data validation (Validation)
1. Through the interceptor to achieve, in the Action of the method is called before calling validation methods or other authentication method
2. After the authentication, if authentication fails, return the result (result) for a fixed input, the contents of return information would contain errors.
Return to the wrong key for the collection of "fieldErrors" a data object stored in request, if the use of template technology,
The error message is displayed in the lower part of the control (the exact location of the template control), if we do not use template technology, we can use the Struts2 Tag
Read "fieldErrors" show the value.
3. Struts validation function for the server-side validation
Check code:
Struts.xml profile of Action must be <result name="input"> </ result>
Check the code has required addfieldError methods (try to use the method in the I18N code to facilitate the international show, because the code is hard-coded and not easy to modify)
Need to add
@ Override
public void validate () (
addfieldError (....);
)
Ways to Action, all of the verification operation in the implementation of the method.
4. If proved to be successful, call the Action method definition, should be back on the logical results.
Struts 2 at the data type conversion
Struts
field user.name
user.password
----> In action in search of a property, known as the user
if find
set user.name to user.name 's value
set user.passwordto user.name 's password
else
DEBUG: print message
-----> After Find it:
Data type conversion:
Be an example of user property (if for null, through the creation of examples of class, pay attention to the corresponding field type, bean contains no parameters required Construction)
Call the set method to the object assignment 【place finish at the data type conversion】
【Place finish at the data type conversion】
Basic data types <----> String
Quote <----> String data type
For instance:
Conversion of data types in, Date required separate treatment, the reasons for the format of the questions are:
Date corresponding to the request of the Locale of the short format DateFormat
Manual conversion type (date):
Conversion mode,
Struts rewrite one of the StrutsTypeConverter
Struts in the register at 2
ActionName-conversion.properties [for Action]
xwork-conversion.properties [for Struts App]
3 types of non-Struts custom conversion @ interface, that is after jdk5 Notes
In the proceedings before the date of the type of register can be automatically invoked without interference.
Struts 2 in the implementation of IoC
1. Implementation of Struts 2 at IoC
IoC
Inversion Of Control inversion of control, the definition of our own class to the container (running the context of the environment) to control, not in the category of internal control, dependency injection (Dependency Injection) is one thing and the actual Ioc
class A implement x
class B implement x
interface x
xxxFactory
xxxFactory.get(parameter); In this way the call can be a dynamic interface of a known type of implementation, or are a subclass of abstract class.
Before the call, the call is unknown.
This approach is known as drive interfaces (Interface Driver Design)
In this way the use of interface on the basic form of implementation has been achieved on the category of non-immersion into the way to achieve the kind of implementation without modifying circumstances achieve more than the original code of the job, a direct consequence of the implementation interface is slow in the interface code can be added prior to implementation.
Such cases are referred to as dependency injection (Dependency Injection) === Inversion Of Control
Configuration
Who put into the to whom, if there is no definition, then, still is a typical factory model code should not really achieve the Ioc operation, through the configuration and management framework
Struts2 use Ioc;
Add spring-xxx.jar 【provide spring support , spring is a lightweight Ioc container, if there is no complete jar file, you can use to be four packages: spring-context-2.5.4.jar, spring-web - 2.5.4.jar, spring-core-2.5.4.jar, spring-beans-2.5.4.jar 】
Add struts2-spring-plugin-2.0.11.1.jar 【used to provide the use of the Struts control spring Ioc link】
Add profile
Ioc 【ApplicationContext .xml configuration of Java Bean (POJO, the Ejb non-JavaBean) 】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean>
<property name ="Field_Name" >
<ref local ="another_bean_id" />
</property >
<property name ="Field_Name" >
</propertie>
</bean>
</beans> 1. Find action
2. Search action with classname in applicationcontext.xml
3. Use a action class's instance defined by applicationcontext.xml
Struts2 with Spring achieve a procedure of the IOC
1. Struts2 in Action definition 【bean _id using applicationContext.xml in the U.S. Action as a class 】
<action name="CatchIocDataMethod"
class="com.baoding.Action.CatchIocDataMethod">
Become:
<action name="CatchIocDataMethod"
class="ActionClassId in ApplicationContext">
2. The definition of Spring
1. Copy jar: spring-context-2.5.4.jar, spring-web-2.5.4.jar, spring-core-2.5.4.jar, spring-beans-2.5.4.jar, struts2-spring-plugin -2.0.11.1.jar 5 files to / WEB-INFO/lib
2. ApplicationContext.xml to set up file / WEB-INFO / directory 【web .xml defined in the spring configuration file path, a different matter 】
Configuration WEB.xml way into the Middle Road
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>
1.
1. Struts.properties documents or modify struts.xml document, the definition of struts in the ObjectFactory containers for Spring
【】 Struts .properties
struts.objectFactory = spring / / remove the diverted Notes
【】 Struts .xml
<constant name="struts.objectFactory" value="spring" />
At all struts.properties constants appeared in documents in this way can be used in coverage at struts.xml
2. Prompt information
Initializing Struts-Spring integration ** struts2-spring-plugin was prompted after the use of information
Bean factory for application context ..... Spring Ioc container Successful initialization information after
Related Posts of Struts 2
-
struts.xml profile Xiangjie
<! DOCTYPE struts PUBLIC "- / / Apache Software Foundation / / DTD Struts Configuration 2.0 / / EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <! - Include node is struts2 in component-based approach c ...
-
Learning to talk about Struts
Release Date: 2006.03.15 19:49 Source: Matrix Author: blue_sunn This article is to introduce the basic principles of Struts 1.1 framework, and to introduce Struts 1.1 installation and a simple example. 1. Struts origin Struts as an integral part of A ...
-
struts.xml and struts.properties configuration Xiangjie
Struts 2 framework has two key configuration files: struts.xml and struts.properties One struts.xml document management applications is mainly responsible for the Action mapping, as well as the definition of Action contained in the Result. In additio ...
-
struts2 and commons-fileupload2.1 used in combination
Analysis of a lot of mistakes yesterday, today released the correct configuration is as follows: First, the need to set up struts.properties struts.i18n.encoding = UTF-8 struts.locale = zh_CN struts.multipart.saveDir = \ temp struts.multipart.maxSize ...
-
Constants commonly use
struts.i18n.encoding: UTF-8 struts.action.extension: action, do struts.serve.static.browserCache: true - cache the static content struts.configuration.xml.reload: false struts.devMode: false - print detailed error message on development mode struts.u ...
-
Struts 1.2
Struts Basic concepts: 1. What is MVC? What's the use? 2. Controller structure 3. What is Struts? What's the use? 4. Struts content and environment configuration What is MVC? What's the use? MVC is a design framework, the program flow cle ...
-
struts & rails
Recent news a lot of struts 2.1.3, struts Help still very welcome. And I is a betrayer of struts and spring, is the programming philosophy ROR enjoy ... ... But be vigilant in peace time, I have to re-consider the possibility of return SSH. I suddenl ...
-
Xiang Jie struts2 in struts.properties
Struts 2 framework has two key configuration files: struts.xml and struts.properties One struts.xml document management applications is mainly responsible for the Action mapping, as well as the definition of Action contained in the Result. In additio ...
-
struts.xml file does not automatically prompt
1: Click on window - preferences - Search catalog - add 2: Select the key type for the URI 3: Key: http://struts.apache.org/dtds/struts-2.0.dtd This file is struts.xml file, copy can be 4: location: the corresponding dtd file, located in struts-core ...
-
Detailed struts development
Detailed struts development Struts concepts and architecture Struts has a group of mutual cooperation category, Serlvet as well as the composition of Jsp TagLib. Struts-based web application framework JSP Model2 basically in line with the design stan ...













Leave a Reply