Struts2 core technology introduction, struts2 Core Technology

Source: Internet
Author: User
Tags i18n

Struts2 core technology introduction, struts2 Core Technology

Struts2Core Technologies

To use the Struts2 framework, pay attention to the following three elements: configuration file, ing file, and Action:

Global attribute file struts. properties: stores some parameter variables run by the system. The entire system has only one attribute file;

Struts ing file struts. xml: used to configure the Action and interceptor for request ing. There can be multiple ing files;

Business controller Action: You can use the POJO (similar to JavaBean) class or integrate ActionSupport to obtain not only form data, but also context variables. The following is a detailed description:

1,Global attribute FileStruts. properties

The struts. properties file mainly defines system attributes. See the following sample code:

 

### Struts default properties ### (can be overridden by a struts. properties file in the root of the classpath) ##### specify the configuration class of Struts2, which is the following configuration by default, you can also inherit ### Specifies the Configuration used to configure Struts ### one cocould extend org. apache. struts2.config. configuration ### to build one's mimize way of getting the configurations parameters into Struts # struts. configuration = org. apache. struts2.config. defaul TConfiguration ### set the default inner Code of International region information and international information ### This can be used to set your default locale and encoding scheme # struts. locale = en_USstruts.i18n.encoding = UTF-8 ### specify the object factory class, you can also implement your own factory class ### if specified, the default object factory can be overridden here ### Note: short-hand notation is supported in some cases, such as "spring" ### Alternatively, you can provide a com. opensymphony. xwork2.ObjectFactory subclass na Me here # struts. objectFactory = spring ### specify the auto-embedded autoWrite mechanism when using Spring factory classes ### specifies the autoWiring logic when using the SpringObjectFactory. ### valid values are: name, type, auto, and constructor (name is the default) struts. objectFactory. spring. autoWire = name ### whether to use class cache when Spring integration is used. Optional values include: true and false ### indicates to the struts-spring integration if Class instances shocould be cached ### this shocould, Until a future Spring release makes it possible, be left as true ### unless you know exactly what you are doing! ### Valid values are: true, false (true is the default) struts. objectFactory. spring. useClassCache = true ### specify the object type checker. The available values include "tiger" and "notiger". You can also use your own classes ### if specified, the default object type determiner can be overridden here ### Note: short-hand notation is supported in some cases, such as "tiger" or "notiger" ### Alternatively, you can provide a com. opensymphony. xwork2.util. objectTypeDeterminer implem Entation name here ### Note: By default, com. opensymphony. xwork2.util. defaultObjectTypeDeterminer is used which handles type detection ### using generics. com. opensymphony. xwork2.util. genericsObjectTypeDeterminer was deprecated since XWork 2, it's ### functions are integrated in DefaultObjectTypeDeterminer now. ### To disable tiger support use the "notiger" property value here. # struts. objectTypeDeterm Iner = tiger # struts. objectTypeDeterminer = notiger ### specify the Parser when MIME-type multipart/form-data is used ### Parser to handle http post requests, encoded using the MIME-type multipart/form-data # struts. multipart. parser = cos # struts. multipart. parser = pellstruts. multipart. parser = jakarta # uses javax. servlet. context. tempdir by defaultstruts. multipart. saveDir = struts. multipart. maxSize = 2097152 ### specify a custom property file ### Load custo M property files (does not override struts. properties !) # Struts. custom. properties = application, org/apache/struts2/extension/custom ### URL and Action ing processor ### How request URLs are mapped to and from actions # struts. mapper. class = org. apache. struts2.dispatcher. mapper. defaultActionMapper ### specify the URL extension. multiple extensions can be specified and separated by commas, for example, action, jnlp, do ### Used by the DefaultActionMapper ### You may provide a comma separated list, e.g. struts. action. extension = action, jnlp, dostruts. action. Extension = do ### used by FilterDispatcher, true indicates that Struts serves static content comes from internal jar ### Used by FilterDispatcher ### If true then Struts serves static content from inside its jar. ### If false then the static content must be available at <context_path>/strutsstruts. serve. static = true ### whether the static content provided in the Struts filter should be cached by the browser to the header attribute ### Used by FilterDispatcher ### This is good for development where one wants changes to The static content be ### fetch on each request. ### NOTE: This will only have effect if struts. serve. static = true ### If true-> Struts will write out header for static contents such that they will ## be cached by web browsers (using Date, Cache-Content, Pragma, expires) ### headers ). ### If false-> Struts will write out header for static contents such that they are ### NOT to be cached by web browser (Using Cache-Content, Pragma, Expires ### headers) struts. serve. static. browserCache = true ### whether to allow dynamic method calls. If it is true, you can configure the following <action> parameter ing ### mappings, such as <action name = "*/*" method = "{2}" class = "actions. {1} ">### Set this to false if you want to disable implicit dynamic method invocation ### via the URL request. this includes des URLs like foo! Bar. action, as well as params ### like method: bar (but not action: foo ). ### An alternative to implicit dynamic method invocation is to use wildcard ### mappings, such as <action name = "*/*" method = "{2}" class = "actions. {1} "> struts. enable. dynamicMethodInvocation = true ### whether to allow "/" in the action name ### Set this to true if you wish to allow slashes in your action names. if false, ### Actions names cannot have slash Es, and will be accessible via any directory ### prefix. this is the traditional behavior expected of WebWork applications. ### Setting to true is useful when you want to use wildcards and store values ### in the URL, to be extracted by wildcard patterns, such as ### <action name = "*/*" method = "{2}" class = "actions. {1} "> to match"/foo/edit "or ###"/foo/save ". struts. enable. slashesInActionNames = false ### Alternative syntax % {} can be used to replace tags ### use alternative syntax that requires % {} in most places ### to evaluate expressions for String attributes for tagsstruts. tag. altSyntax = true ### is the Struts development mode ### when set to true, Struts will act much more friendly for developers. this ### supported des: ###-struts. i18n. reload = true ###-struts. configuration. xml. reload = true ###-raising various debug or ignorable problems to er Rors ### For example: normally a request to foo. action? SomeUnknownField = true shocould ### be ignored (given that any value can come from the web and it ### shocould not be trusted ). however, during development, it may be ### useful to know when these errors are happening and be told of ### them right away. struts. devMode = false ### whether to automatically load International Information ### when set to true, resource bundles will be reloaded on _ every _ request. ### this is good during development, but shocould never be used in productionstruts. i18n. reload = false ### Standard UI theme ### Change this to reflect which path shocould be used for JSP control tag templates by defaultstruts. ui. theme = xhtmlstruts. ui. templateDir = template # sets the default template type. either ftl, vm, or jspstruts. ui. templateSuffix = ftl ### in struts. whether to allow overloading when the xml file is changed ### Configuration reloading ### This will cause the configuration to reload struts. xml when it is changedstruts. configuration. xml. reload = false ### path of the velocity configuration file. The default value is velocity. properties ### Location of velocity. properties file. ults to velocity. propertiesstruts. velocity. configfile = velocity. properties ### Cvelocity context list ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContextstruts. velocity. contexts #### Location of the velocity toolbox ### Location of the velocity toolboxstruts. velocity. toolboxlocation ### used to build URLs, such as the UrlTagstruts. url. http. port = 80struts. url. https. port = 443 ### possible values are: none, get or allstruts. url. includeParams = get ### load the additional internationalized attribute file (not including. properties) ### Load custom default resource bundlesstruts. custom. i18n. resources = com. demo. struts2.resources. applicationResources ### workaround for some app servers that don't handle HttpServletRequest. getParameterMap () ### often used for WebLogic, Orion, and OC4Jstruts. dispatcher. parametersWorkaround = false ### configure the Freemarker Manager class to be used ### Allows user to plug-in customised Freemarker Manager if necessary ### MUST extends off org. apache. struts2.views. freemarker. freemarkerManager # struts. freemarker. manager. classname = org. apache. struts2.views. freemarker. freemarkerManager ### Enables caching of FreeMarker templates ### Has the same effect as copying the templates under WEB_APP/templatesstruts. freemarker. templatesCache = false ### Enables caching of models on the BeanWrapperstruts. freemarker. beanwrapperCache = false ### See the StrutsBeanWrapper javadocs for more ionionstruts. freemarker. wrapper. altMap = true ### configure the specified tresult class to use stylesheet caching. ### Set to true for developers and false for production. struts. xslt. nocache = false ### list of configuration files automatically loaded by struts ### A list of configuration files automatically loaded by Strutsstruts. configuration. files = struts-default.xml, struts-plugin.xml, struts. xml ### Whether to always select the namespace to be everything before the last slash or notstruts. mapper. alwaysSelectFullNamespace = falseView Code

 

2,Ing FileStruts. xml

Struts. xml is the core configuration file of the Struts2 framework. This file is mainly responsible for managing the business controller Action of the Struts framework. This file contains a unique root element <struts>. You can add sub-tags to this element to implement your own configuration. General development mainly uses the following seven configuration functions:

(1) attribute definition element <constant>

You can use the <constant> label to define an attribute, which has the same name as the attribute in struts. properties and overwrites the value of this attribute in struts. properties. The advantage of this is that when multiple ing files are used, different attributes are allowed for each file.

(2) Import sub-file elements <include>

Use the <include> label to import a sub-file. The sub-file has the same structure as struts. xml and uses the same configuration syntax. This method provides a modular way to manage struts. xml files.

(3) package definition elements <package>

<Package> defines a group of <action> and interceptor elements. Multiple <action> elements can be defined. name indicates the package name, the extends property represents the name of the package inherited from the struts-default.xml.

(4) Action ing configuration element <action>

Action ing is the basic unit of work in the framework. The framework maps the Request Path to determine which Action to process the request.

(5) Result configuration element <result>

The Action class returns a string after processing a request. This string is used to select a <result> element. Generally, an action mapping has multiple <result> values, which indicate different results.

(6) configure the global ing element <global-results>

The <result> defined in the <action> element is called a local <result>. In addition, we can define the global <result>, these global <result> will sadly share multiple <actions>. The framework first looks for the <result> nested in the <action> element. If no match exists, it looks for the result in the global <result>.

(7) configure the interceptor <interceptors>

By using the interceptor, we can execute some methods we have defined in advance before the method in the Action is executed, or we can immediately execute some methods we have defined in advance after the method in the Action is executed.

See the followingStruts. xmlSample file code:

 

<? 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 --> <package name = "main" extends = "struts-default"> <action name = "test" class = "com. demo. struts2.actions. testAction "> <result name =" success "> success. jsp </result> <result name = "input"> input. jsp </result> </action> </package> </struts>View Code

 

3,Business ControllerAction

When using the Struts2 framework, you do not need to develop the ActionForm class. Pay attention to the following three points:

Action can be any POJO (similar to JavaBean) Class

Generally, it inherits the ActionSupport class.

Context variables can be accessed in Action

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.