Struts development skills

xiaoxiao2021-03-06  51

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 Defines a module in the system, you need to define this module before development, the Struts configuration file is named: struts-config-xxx.xml xxx is a lowercase English name or abbreviation Such as: struts-config-sysman.xml Note: The middle is "-" instead of "_" connectors

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 from each configuration file name

In addition, all static JSP needs to define its ".do" in the configuration file, save in the struts-config-pages.xml file, the content is as follows: ... ... 2. Tiles configuration file definition system frame profile is tiles-defs_zh_cn.xml (via .properties attribute) Document supports international application, default is tiles-defs.xml), the frame structure of the module needs to be defined inside, as in the following example: ... < Put name = "linksite" value = "/ layouts / link.html" /> ... frame naming specification Press "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 is not required to write a Tiles: Insert page, as follows: 3. The comments in the module need not only need to add the necessary comments in the program.

When defining the configuration file, you must also need to add a corresponding comment, mainly add comments in Struts-config-xxx.xml and tiles-defs_en_cn.xml, to explain the function of the Action or configuration module, put in the configuration item In the front, see the configuration file above 4. For the editing of the configuration file, you cannot use the XML editing function in JBuilder, because JB will automatically change the encoding and content inside the XML, so edit the XML configuration file, to use editing software Such as the class such as the UE, the class, Actionform, and ".do" definition submitted in JSP, and submission of page action, login as an example of Login: 1. The name definition of class (the first letter is capitalized) is "Action Name Action / Form" such as: LoginAction.class, Loginform.class 2. Page address definition (the first letters should be lowercase) If there are two words or more, the second word first letter is capitalized, and this type of push is "action name" such as: login.do or loginsys.do JSP file naming is also naming this specification 3 . Page Action Defining because of the FORM corresponding ActionForm in the JSP page, itself has an action attribute, so if the page action is also defined, it will cause unnecessary trouble, so put the page action unique to "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 mechanisms triggered for menus and operational events Due to the ".do" form in the system, the menu and operations are issued by the ".do" form, it is necessary to control which is the event request for the menu, which is the operation Event request; 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 agreed: JSP page for the request for the menu in the system Is a "get" method, all of the action 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 The permissions table, determine the user's menu permission; if it is "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 user's permission point is performed.

Parameter information Get 1. Public parameter information When the web service is started in the application through the PLUGIN mode, the variable is placed in the Application so that it can be called in any JSP that requires the variable; the method is as follows: public void setServletContext (ActionServlet ActionServlet) {Try {servletContext sc = actionServlet.getServletContext (); // SysInitPwd sc.setAttribute (Constants.SYS_INIT_PWD, SelectLists.getSysConfig ( "PWDINIT")); ... Called in action and other programs: getServlet () getServletContext () getAttribute ( ".. ... ");

2. Data that needs to be displayed on the page is allowed to be stored in the range of Request, which can reduce the server-side memory load, the way is as follows: // Call the employee handle class staffdeal sd = new staffdeal (); // According to the employee status querying employee arraylist al = SD.QRYSTAFF (STRSTATE); // Add Request Request.setttribute ("staffinfo", al); 3. Private or need to obtain parameter information according to user properties, you can use "ControlClass =" in Tiles XXX "This method is obtained, the configuration is as follows: Requires the Perform method in IMPLEments Controller, Example code is as follows: public void perform (ComponentContext componentContext, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws IOException, ServletException {HttpSession session = request.getSession (); // 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_salutatory where userid = '" uid ""; try { ...} catch (exception ex) {throw new servletexception (ex.getMessage ());}} 4. For background error messages displayed on the front page displayed "Message.common = {0}" and in the Action class when using ActionErrors or ActionMessages, as follows: ... ActionMessages ams = new ActionMessages (); // exception process Try {...} catch (exception ex) {ex.printStackTrace (); ams.add (ActionMessages.GLOBAL_MESSAGE, new ActionMessage ( "Message.common", ex.getMessage ()));} finally {if (! ams.isempty ()) {SaveMessages (Request, AMS);

}} ... Usage in the JSP page is as follows: < / html: Messages> If there is a background message generated, the front page can appear error information 5. System configuration file system parameters such as database connections are configured in the sysconfi.xml file, stored under "Web-INF / XML" folder Please refer to this file.

Development specification and public methods For the development specifications of Java, see "Java Programming Specification .DOC", only some specification in Struts development: 1. Java file is stored according to business logic, and use modules as the package name, such as : Telecombi.logic.sysman.security package name All Action and Actionform are stored under the same package, easy to manage, do not cross-package call 2. All attributes in all ActionForms are "first word lowercase second The form of a word first letter ... ", such as: StaffID, StaffName, does not allow" _ "for word connectivity 3. You need to verify the client and server two validation (ie, FORM in JSP) Performing a JavaScript verification and verification in the Excute method in the action), you cannot use only one of the methods to prevent customers from bypassing JS directly; when verifying the logs submitted, you must use StaticJavaScript = "false", otherwise it will javascript written pages, such as: