Basic Implementation of OA (ssh) (poi generates Excel and struts2 dynamically downloads mysql Data

Source: Internet
Author: User
Oa Project Study Notes: milestone-12-11 with the Environment ssh1. create a web project 2. Add the project context to the server. xml file. That is, it provides context re-loading and access for the project. ContextpathoadocBaseD: ProgramFilesMyEclipse6.0eclipseworkspaceoaWebRootreloadabletrue

Oa Project Study Notes: milestone-12-11 with the Environment ssh 1. Create a web Project 2. Add the project context to the server. xml file. That is, it provides context re-loading and access for the project. Context path =/oa docBase = D:/Program Files/MyEclipse 6.0/eclipse/workspace/oa/WebRoot reloadable = true/

Oa project study notes:

Milestone 1: 2009-12-11
Environment ssh
1. Create a web Project
2. Add the project context to the server. xml file. That is, it provides context re-loading and access for the project.

3. First, package hibernate3.1 to the oa project.
4. Then, package spring2.0 to the oa project.
5. Package struts2 to the oa project.
6. Inject struts2 into the web environment, configure the string of struts2, and set the access path mode. It also provides context load listening for ssh framework integration.
7. Add the business logic and link it to the database.


Milestone 2:
1. Add struts. xml to internationalize.
2. Add service interfaces and implementations. Add @ SuppressWarnings ("unchecked") before the method to remove the warning when performing List generic forced conversion.
3. Method: you can click the method of an interface to directly access its implementation class, rather than the interface class.


Milestone 3: 2009-12-11

The best solution to Garbled text:
1. Set the database encoding method to UTF-8.
2. The default encoding method of struts2 is UTF-8. That is struts. i18n. encoding = UTF-8.
3. Set the jsp page encoding method to UTF-8.

This eliminates the need to convert or filter characters each time:
1. character encoding filters.
1.1 set character encoding in a custom filter mode
Configuration in web. xml


Struts2.x
Org. apache. struts2.dispatcher. FilterDispatcher



CharacterEncoding
Com. cs. tb. util. CharacterEncodingFilter

Encoding
UTF-8


Package com. cs. tb. util;

Import java. io. IOException;

Import javax. servlet. Filter;
Import javax. servlet. FilterChain;
Import javax. servlet. FilterConfig;
Import javax. servlet. ServletException;
Import javax. servlet. ServletRequest;
Import javax. servlet. ServletResponse;
/**
* It is used to set the HTTP request character encoding filter. The filter parameter encoding is used to specify the character encoding used to process the Chinese characters of the Html Form request parameters.
*/
Public class CharacterEncodingFilter implements Filter {
Private FilterConfig filterConfig;
Private String encoding = "";

Public void destroy (){
FilterConfig = null;
Encoding = null;
}

Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
If (encoding! = Null ){
Request. setCharacterEncoding (encoding); // sets the character encoding.
Chain. doFilter (request, response); // redirects requests and responses to the next link
}
}

Public void init (FilterConfig filterConfig) throws ServletException {
This. filterConfig = filterConfig;
This. encoding = this. filterConfig. getInitParameter ("encoding"); // obtain the initialization value encoding in the filter in the web. xml file.
}
}
1.2 use ActionContextCleanUp
Configuration in web. xml

Struts-cleanup

Org. apache. struts2.dispatcher. ActionContextCleanUp




Struts-cleanup
/*

2. request. setCharacterEncoding ("UTF-8 ");
3. str = new String (str. getBytes ("ISO8859-1"), "UTF-8 ");


Milestone 4:
Use ognl to obtain the property object or pass the value in the request. For example, # request. list or % {# u. id}
"#" Has three main purposes:
Access the OGNL context and Action context, # It is equivalent to ActionContext. getContext (). The following table contains several useful attributes in ActionContext: Name example
Parameters contains the Map # parameters. id [0] of the current HTTP request parameter, which is equivalent to request. getParameter ("id ")
The request contains the Map of the attribute of the current HttpServletRequest # request. userName is equivalent to request. getAttribute ("userName ")
Map # session. userName of the attribute that contains the current HttpSession is equivalent to session. getAttribute ("userName ")
Map # application. userName of the application that contains the attribute of the current application's ServletContext is equivalent to application. getAttribute ("userName ")
Attr is used to access its attributes in the order of request> session> application # attr. userName is equivalent to reading the userName attribute in the scope of the preceding three ranges in order until it is found.

Used to filter and project sets, such as books .{? # This. price <100 };
Construct a Map, such as # {'foo1': 'bar1', 'foo2': 'bar2 '}.

The purpose of the "%" symbol is to calculate the value of the OGNL expression when the flag property is of the string type.

"$" Has two main purposes:
OGNL is used to reference the OGNL expression in the international resource file. For an example, refer to international (i18n) your application in Struts 2.0.
REFERENCE The OGNL expression in the Struts 2 configuration file


Milestone 5: 2009-12-12
When using Spring to create an action, it is a singleton by default. Every time we refresh or call it, we put it in our fielderror map, so more and more errors will be displayed.
When the request range of action is prototype, Spring dynamically creates an action instance.
We can implement the validate method in the action to obtain FieldErrors and print it to find out the cause.
@ Override
@ SuppressWarnings ("unchecked ")
Public void validate (){
Map map = this. getFieldErrors ();
Set set = map. keySet ();
Iterator iterator = set. iterator ();
While (iterator. hasNext ()){
System. out. println (map. get (iterator. next ()));
}
}


Add a module-driven Verification Framework and the verification method is visitor. In this way, you can add the property verification information of the bean object to the bean object.
But there are also parameter settings,
Context is the alias in the middle of bean object verification,
AppendPrefix append prefix as the header of the output error message
Message is the additional information.

Milestone 6: 2009-12-13
Use poi to export an Excel file
Dynamically generate an Excel file using an annoying spreadsheet format of HSSF horrible spread sheet format
The key is how to obtain the file. We generally know that the file is written and written in the form of a stream.
Here, we also provide an InputStream getDownloadFile () method that supports downloading struts2.
Specifically displayed in the struts. xml file,

// Indicates the default file download output stream.
Application/vnd. ms-excel// This is the default and potential. Used to specify the format of the downloaded content.
Attachment; filename = "AllUser.xls"// Filename, which is the default value and is potentially used. Specifies the file name to download.
// Tachment indicates that the file is used as an attachment and can be downloaded rather than directly opened for viewing.
DownloadFile// Input name that receives the stream and supports download


You only need to provide a hyperlink mark on the page to download it dynamically.


Milestone 7:
Use of the struts2 dynamic download framework and the file download processing method.

The temporary file we downloaded is named dead, but data inconsistency may occur during reading and writing.
Therefore, we must make effective processing: Generating Random file names is a good way.
RandomStringUtils. randomAlphanumeric (length); this is automatically provided by Spring. Used to generate dynamic random strings.
Here we can learn from this idea, that is, the generation of registration codes.


Milestone 8: 2009-12-15
Delete the generated temporary file:
1. Use the HSSF workbook to write it to a temporary file. However, consider deleting the workbook.
1.1 set the generated temporary file to a dead name, but data inconsistency may occur during reading and writing.
1.2 obtain a random file name for the generated temporary file.
1.3. The problem of file deletion is as follows: how long can we use a thread to sleep and then delete the file.
1.4 but once the server is shut down, we have to reconsider the problem.
1.5 so we have to create a servlet that is provided to the system for call at startup. Then: do not generate servlet ing files, and set load-on-startup to delete undeleted files.

2. obtain the getBytes () byte array of HSSF. that is, if a temporary file is not generated, it is directly obtained from the memory, but part of the generated xls file data is lost. this is caused by getBytes.
3. Write Data in the memory of the HSSF workbook to a byte array output stream to obtain the byte array.

And then import it to the input stream. struts provides a dynamic download framework. For others to download.

How can I directly use the file. delete () method in the getInputStream method.
My understanding is: this temporary file is in a certain system reference. The deletion fails unless the call thread deletes the temporary file.
That is to say: in a multi-threaded environment, there may be other threads operating on this file, so it cannot be deleted. Pay attention to the synchronization of Operating Files in the program.
Conclusion: When multithreading is used, we recommend that you check the program flow to see if the file is referenced in other parts.

Please correct me for a better idea !!! O (distinct _ distinct) O ~

The file has been downloaded !!!!

Related Article

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.