Struts Development Practice - Practical Tips

xiaoxiao2021-03-06  76

Article originates from a netizen

This article is written by smilelily users. Yesterday, I found my own email record. He has a user in 9cbs, and the article source address http://dev.9cbs.net/User/smilelily struts Problem and solution

First, Struts uses tips (Mainly from "struts in action)

1. Use Array in ActionForm to match repetitive variables. For example, when using MultiBox, the Array can be defined in the Form.

2. Use to display the total number of records of Collection.

EG: . If there is two records, 2 is displayed.

3. Display the cyclic serial number mark:

4. Use to check if the object exists. Check if the properties exist in .

5. Relatively stable drop-down list data sets use scope = "application". (This method has always been used, but it is still not clear that it will be used directly when the system is logged in, and it can be used directly).

6. Use the to set the path to the CSS and JS files. (This does not see any difference with direct use).

7. JavaScript interacts with the FORM value: This is very frequent

EG:

9. In the page using JavaScript, the names of the two FORM objects cannot appear. Especially submit buttons, we often do not write Name, the default Name is Submit, which is wrong if there is an onclick event trigger.

10. Several ActionForm public properties can define a subclass in the ActionForm.

EG: Class BasicInfo Implement Serializable {};

It is necessary to define BasicInfo BasicInfo = New BasicInfo () in ActionForm;

Property = "BasicInfo.a" in the JSP page

Second, upload documents

1. You need to define the FormFile type field in ActionForm. EG: Formfile MyPhoto

2. In the JSP page

3. Error may appear after Submit: No Multipart Request Date Sent Solution:

1. Set the Redirect tree in struts-config.xml to TRUE.

2. If it is a jump with parameters, use the following method:

ActionForward Forward = mapping.forward ("Success");

StringBuffer bf = new stringbuffer (forward.getpath ());

bf.append ("? id = 1"); // parameter

Return New ActionForward (bf.tostring (), true);

Third, the picture shows

1. Write an action of displaying a picture, the code structure is as follows:

Public class photoAdition extends action {

Private static factory string content_type = "image / gif";

Public ActionForward Perform (ActionMapping Mapping, Actionform Actionform, HttpservletRequest Request, HttpservletResponse Response)

Throws servletexception, ioException {

Response.setContentType (Content_Type);

... // Get Param EmployeeID

ServletOutputStream out = response.getOutputStream ();

InputStream in = NULL;

Try {

IN = ... // Get Blob Pic

}

Catch (throwable e) {

E.PrintStackTrace ();

}

IF (in! = NULL) {

Try {

Int Len;

BYTE BUF [] = New byte [4096];

While ((Len = In. Read (BUF, 0, 4096))! = -1) {

Out.write (buf, 0, len);

}

}

Catch (IOException IoE) {

IoE.PrintStackTrace ();

}

}

Return NULL;

}

}

2. Write the following code in the JSP page displaying the picture:

Fourth, enable ApplicationResources.properties to support Chinese display:

Go to the command prompt page, enter the bin directory under the JDK installation path, execute the command:

Native2ASCII - Encoding GB2312 ApplicationResources_ISo.properties (Original File) AllicationResources.properties (new generation file)

中 中 的 文文 in the JSP page.

However, there is still a problem that there is no solution, which is "NULL ERROR" in the JSP page. Do you have the same problem? How to solve it? Five: The file path in Action:

An XML file is required in Action, which is not a problem with the relative path to compile, but if you don't have the file path on the server. I found a lot of solutions, I finally called the Request.GetRealPath ("/") method, the line of the line, but this method is not recommended. Do you have a better way?

Six: JSP page Display strings array:

7: How to define your JSP file issued path (Practice in Windows2000 Environment):

Open the server.xml file under the CONF folder under the Tomcat directory, find the tab

After the tag, add your own publishing path, as follows:

Restart Tomcat, visit: / localhost: 8080 / mybwebapps /, ok!

8: How to solve the problem of remote words:

Use the big character set to set all the JSP page character sets as follows: <% @ page contenttype = "text / html; charSet = GBK"%> Note: GBK must be capitalized.

Nine: Page refresh data Repeated submission issues:

The method of solving is to use redirection. The same as the second solution:

1. Set the Redirect property in struts-config.xml to TRUE.

2. If it is a jump with parameters:

ActionForward Forward = mapping.forward ("Success");

StringBuffer bf = new stringbuffer (forward.getpath ());

bf.append ("? id = 1"); // parameter

Return New ActionForward (bf.tostring (), true);

Ten: Others:

Programming specification suggestion:

Database interface class named: * Dao.Java

Logical class named: * bo.java

Action naming: * action.java

Actionform Named: * form.java

Then a function module is placed in a packet.

They are some of the comparison of primary questions, but I hope to help you learn from the beginning of Struts, and I hope to give pointers to the questions in the text, :).


New Post(0)