Struts development skills

zhaozj2021-02-16  122

Struts development skills

After experiencing the development of "China Telecom's Customer Care Service" project and current "China Telecom Management Analysis" project currently under development, some of the technical and techniques developed by Struts1.1 and Tiles, it is very convenient Development, and also believe that it can provide some help to the reader in developing Struts.

Module configuration

1. Struts profile definition

For a module in the system, you need to define the configuration of the module before development, and the Struts's configuration file is named:

Struts-config-xxx.xml

XXX is a lowercase English name or abbreviation of the module, such as: struts-config-sysman.xml

Note: The middle is "-" instead of "_" connector

Uniform Save under the "Web-INF / XML" folder and need to add the corresponding configuration file in web.xml

Address, the specific example:

...

config

/Web-inf/struts-config.xml, /web-inf/xml/struts-config-pages.xml ,/web-inf/xml/struts-config-sysman.xml

...

Note: You need to use "," connectors separated each configuration file name.

In addition, all static JSPs need to define their ".do" in the configuration file, save it.

Struts-config-pages.xml file, the content is as follows:

...

...

2. TILES configuration file definition

The system's frame configuration file is tiles-defs_en_cn.xml (through the .properties attribute file supports international application, default is tiles-defs.xml), the module's frame structure needs to be defined inside, as in the following example:

...

...

Frame naming specification Press the "System (Subsystem). Function Module. Page Module", as "Default.frame" above

The page action of the settings in the struts-config-pages.xml file can be written:

This will not have to write a TILES: INSERT page, as follows:

3. Comments in the module

Not only need to add the necessary annotations in the program, but also need to add the corresponding comments when defining the configuration file, mainly in struts-config-xxx.xml and tiles-defs_zh_cn.xml to add a comment, to put an action or configuration The function explanation of the module is clear, placed in front of the configuration item, see the configuration file above

4. Edit on the configuration file

You cannot use the XML editing function in JBuilder because JB will automatically change the encoding and content in XML, so edit the XML configuration file, you want to use editing software, such as UE, etc.

Event definition

The classes corresponding to the events mainly include Action, ActionForm, and ".do" definitions submitted in JSP, and submission of page action, login as an example:

1. Name definition of class (the first letter needs to be capitalized)

Form is "Action Name ACTION / FORM"

Such as: loginaction.class, loginform.class

2. Page address definition (first letter requires lowercase)

If there are two words or more, the second word first letter is written, according to such push

Form is "Action Name"

Such as: login.do or loginsys.do

JSP file naming also followed this specification

3. Page action definition

Because the FORM corresponding to the JSP page corresponds to this properties, it has an action this property, so if the page action is also defined, it will cause unnecessary trouble, so the page action is unforgettable as "ACT",

If you need to edit a record, the address is as follows:

"/EditRecord.do?act= edit"

If you need to delete, the address is as follows:

"/EditRecord.do?act=delete"

4. Control mechanism triggered for menus and operating events

Since menus and operations in the system are issued by ".do", a mechanism requires a mechanism to control which is an event request for menus, which is an event request for operations;

We introduce the Filter filter in the system, control all requests, and determine if the user logs in and has access to resources (menu, etc.);

The request for the menu in the JSP page is the "get" method, and the Action of the operation is "post" method;

With this convention, first judge the request method of Request in Filter, if it is a "get" method, it is considered to be a request to the menu, so go to the "menu table" to read the corresponding record according to the request address, and read the user Permissions table, determine the user's menu permissions;

If it is a "post" method, it is considered to be a request for the operation, and the "ACT" action in the request is extracted, and the determination of the right to the user is performed.

Parameter information acquisition

1. Public parameter information When the variable is placed in the Application via the PLUGIN method, it can be called in the JSP that needs this variable;

The way is as follows:

Public void setServletContext (ActionServlet ActionServlet) {

Try {

ServletContext sc = actionServlet.getServletContext ();

// sysinitpwd

Sc.setttribute (constants.sys_init_pwd, selectlists.getsysconfig ("pwdinit"));

...

Call mode in Action and other programs:

GetServlet (). getServletContext (). getAttribute ("...");

2. For the data you need to display on the page, you can reduce the server-side memory load in the request, and the method is as follows:

// Call the employee processing class

Staffdeal SD = new staffdeal ();

/ / Inquiry employee according to employee status

ArrayList Al = SD.QRYSTAFF (strState);

// put it in Request

Request.setattribute ("staffinfo", al);

3. Private or need to obtain parameter information according to the user's properties, you can use "ControlClass = XXX" in the definition of Tiles, and the configuration is as follows:

Requires the Perform method in the Implements Controller, the code is as follows:

Public void Perform (ComponentContext ComponentContext,

HTTPSERVLETREQUEST REQUEST,

HTTPSERVLETRESPONSE RESPONSE,

ServletContext servletContext) throws ioException, servletexception {

HttpSession session = request.getations ();

// Get Current session.

User User = (user) session.getattribute (constants.user_key);

IF (user == NULL) {

Return NULL;

}

String uid = user.getuserid (); string sql = "select userid, content from ti_salutator where userid = '" uid "'";

Try {

...

}

Catch (Exception EX) {

Throw new servletexception (ex.getMessage ());

}

}

4. Skills displayed on the front page on the background page

First configure "Message.common = {0}" in Properties

The method is then used in the Action class using actionerrors or actionMessages, the method is as follows:

...

ActionMESSAGES AMS = New ActionMess (); // Exception Processing

Try {

...

}

Catch (Exception EX) {

EX.PrintStackTrace ();

Ams.add (actionMessages.global_message,

New ActionMessage ("Message.common", EX.getMessage ()));

}

Finally {

IF (! ams.isempty ()) {

SaveMessages (Request, AMS);

}

}

...

The method is used in the JSP page as follows:

If there is a background of the MESSAGES in the background, an error message can occur when the front page is available.

5. System configuration file

System parameters such as database connections are configured in the sysconfi.xml file, stored in the "Web-INF / XML" folder, see this file.

Development specification and common methods

See "Java Programming Specification .doc" on Java Development Specifications, just give specification in Struts development:

1. Java file is stored according to the business logic, and the module is used as the form of the package name, such as: Telecombi.Logic.Sysman.security

The name is under small-sold form

All Action and ActionForm are stored under the same packet, easy to manage, do not interpociate

2. All the properties in all Actionform are the form of "first word lowercase second word first letters ...", such as: staffID, staffname, is not allowed to use "_" as a word connection

3. The page that needs to be verified requires two validation of the client and the server (ie, the FORM in JSP performs JavaScript verification and verification in the Excute method in the action), and cannot only use one of the methods to prevent customers from bypass. JS is directly submitted;

When verifying the FORM submitted, StaticJavaScript = "false", otherwise you will write JavaScript to the page, such as:

DynamicjavaScript = "true"

Staticjavascript = "false" />