Struts Development Practice - Practical Tips (below)

zhaozj2021-02-16  57

three,

Picture display

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, :).

转载请注明原文地址:https://www.9cbs.com/read-21939.html

New Post(0)