4. Other knowledge of Struts2

Source: Internet
Author: User

4.1 , Modeldriven

If you want to pass object information to action, but you need to pass a large number of parameters when you have more object information, and you want to set these parameters, it is much more work, so the recommended way is to create the corresponding object directly in the action, in which case the parameters can be passed in the JSP page in the following way.

However, the problem with this approach is that the names in the form cannot be defined according to the habits of our developers, and we need to pass Moderndriven to achieve the following values.

1. Let action implement Modeldriven interface.

2, Cover Getmodel method

3, when a class implements the Modeldriven, it will put the model into root, so our input without any additional object information can be placed directly into the MSG object

4.2 , type conversion

When using Modeldriven to implement parameter acceptance, it may be necessary to use a type converter because of inconsistent types (especially some complex types) that result in an error in the accepted parameter.

A type converter has two modes, a global mode (all actions have a role), and a local pattern (which works for a particular action)

Global Mode Implementation Steps

1. Write a class inheritance Strutstypeconvert

2. Create the Xwork-conversion.properties file in the Classpath and describe the object to be converted in this file

The global converters above are specified for all classes that need to be converted, but sometimes they may be converted according to different actions, and this time a local converter is used

1. Create a class to inherit Strutstypeconverter

2. Create actionname-conversion.properties in the package where action is located


4.3 , File Upload

STRUTS2 provides a natural support for file uploads, as long as you set a form field to file and provide the type of form as tultipart/form-data,struts2 automatically completes the recognition of normal forms and file forms.

1, define a file to provide the appropriate upload form

2. Define a property in action to receive the file object, if it is a set of properties that define an array, and if it is an attribute, define an object. The name of the property must be a name in the form


4.4 , interceptors


Interceptors are a very important control method in Struts2.

Interceptors can be used in development to intercept requests from users and to control permissions accordingly.

1. Creating interceptors

Write a class inheritance Abstractinterceptor

2. Configuring interceptors

2.1. Create interceptors first

2.2. Use Interceptors in action

Problem: When using this interceptor, there is a problem, the original value should be passed into null.

Because when we set up the interceptor, the default interceptor does not work (the default interceptor is set in Struts-default.xml), so you must have the original default interceptor when creating a new interceptor, and you can use the interceptor stack to handle

Learn the examples of interceptors in video tutorials

4.5 , internationalization (i18n)


1. The concept of internationalization

Resourcebudle can support internationalization, through configuration files (. properties) to complete the internationalization of support

2. Requirements for creating configuration files

Message_zh_cn.properties (_CN represents the country's code, can be omitted)

Message_en_uk.properties (can also omit _uk for U.S. English profiles)

3, the use of Java.util Resourcebudle can be achieved

4, the realization of STRUTS2

Local, Global

Local General not used

There are two ways to do this globally:

4.1. Global Settings for Package

• Create package_zh.properties or package_en.propterties under any path to the package where the action is located

• Add the corresponding key and value to these two files

• Allow the action of the country to be implemented after the Actionsupport class is used on the page using <s:text name= ""/> Complete the call

4.2. Global Internationalization

• Create the appropriate resource file under the Classpath

• Add the name of the global resource file to the configuration

4.6 , struts2 the common label


STRUTS2 provides a very good set of form to help us to complete the development

1, Struts2 's theme

稴 Truts2 provides a set of topics to help developers implement interface operations, the default theme is XHTML,

Modifying a theme can be set in the configuration file

2. Common label

4.7 , server-side validation

STRUTS2 provides a number of server-side validation methods (Xml,annotation, etc.)

4. Other knowledge of Struts2

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.