OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据
oa项目学习笔记: 里程碑1:2009-12-11 搭配环境ssh 1.创建web工程 2.将工程上下文加到server.xml文件中。也就是为项目提供上下文的重加载与访问。 Context path=/oa docBase=D:/Program Files/MyEclipse 6.0/eclipse/workspace/oa/WebRoot reloadable=true/
oa项目学习笔记:
里程碑1: 2009-12-11
搭配环境ssh
1.创建web工程
2.将工程上下文加到server.xml文件中。也就是为项目提供上下文的重加载与访问。
3.首先将hibernate3.1的包到oa工程中。
4.然后将spring2.0的包到oa工程中。
5.接着将struts2的包到oa工程中。
6.将struts2注入web环境,配置struts2的映射,设置访问路径模式。并为ssh框架整合提供上下文加载监听。
7.添加业务逻辑,与数据库的链接。
里程碑2: 2009-12-11
1.添加struts.xml国际化。
2.添加服务接口及实现。在进行List泛型强转时,在方法前添加@SuppressWarnings("unchecked"),去掉警告。
3.方法:我们可以点击一下某个接口的方法,可以直接进入他的实现类,而不是接口类。
里程碑3: 2009-12-11
解决乱码问题的最佳解决方式:
1.将数据库的编码方式设成UTF-8。
2.struts2默认的编码方式为UTF-8。即struts.i18n.encoding=UTF-8。
3.将jsp页面的编码方式也设为UTF-8。
这样就省去了每次将字符编码转化或过滤的方式了:
1.使用字符编码的过滤器.
1.1 自定义实现过滤器的方式设置字符编码
web.xml 中配置
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;
/**
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
*/
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);//设置字符编码
chain.doFilter(request, response);//将请求和响应转向下一个链接
}
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = this.filterConfig.getInitParameter("encoding");// 获得web.xml文件中的过滤器中的初始化值encoding
}
}
1.2 使用ActionContextCleanUp
web.xml中配置
org.apache.struts2.dispatcher.ActionContextCleanUp
2.request.setCharacterEncoding("UTF-8");
3.str = new String(str.getBytes("ISO8859-1"),"UTF-8");
里程碑4: 2009-12-12
使用ognl获得request中的属性对象或者传递值。如#request.list 或者 %{#u.id}
“#”主要有三种用途:
访问OGNL上下文和Action上下文,#相当于ActionContext.getContext();下表有几个ActionContext中有用的属性: 名称 作用 例子
parameters 包含当前HTTP请求参数的Map #parameters.id[0]作用相当于request.getParameter("id")
request 包含当前HttpServletRequest的属性(attribute)的Map #request.userName相当于request.getAttribute("userName")
session 包含当前HttpSession的属性(attribute)的Map #session.userName相当于session.getAttribute("userName")
application 包含当前应用的ServletContext的属性(attribute)的Map #application.userName相当于application.getAttribute("userName")
attr 用于按request > session > application顺序访问其属性(attribute) #attr.userName相当于按顺序在以上三个范围(scope)内读取userName属性,直到找到为止
用于过滤和投影(projecting)集合,如books.{?#this.price 构造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。
“%”符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值
“$”有两个主要的用途
用于在国际化资源文件中,引用OGNL表达式,例子请参考《在Struts 2.0中国际化(i18n)您的应用程序》
在Struts 2配置文件中,引用OGNL表达式
里程碑5: 2009-12-12
使用在Spring创建action时默认为单例,而当我们每次刷新或者调用的时候,都放到我们的fielderror的map中所以显示的错误会越来越多。
而将action的请求范围是prototype的方式时,Spring动态创建action实例。
我们可以在action里实现validate方法,获得FieldErrors,并打印,就知道其中的原因了。
@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()));
}
}
添加模块驱动的验证框架,验证方式为visitor,这样就可以在bean对象这添加bean对象的属性验证信息。
但是其中还有参数的设置,
context 也就是bean对象验证的中间的那个别名,
appendPrefix 附加前缀 作为输出错误信息的头
message 就是附加的信息
里程碑6: 2009-12-13
使用 poi 导出 Excel 文件
使用HSSF horrible spread sheet format 讨厌的电子表格格式 动态生成 Excel 文件
其中的关键就是文件的如何获得,一般我们都知道文件是以流的形式,进行写入写出的。
那么在这里,同样也是,提供一个InputStream getDownloadFile()是struts2支持下载的方式。
具体呈现在struts.xml文件中,
application/vnd.ms-excel//这是默认的,潜在的。用以指明文件下载的内容格式.
attachment;filename="AllUser.xls"//filename 这是默认的,潜在的。用以指明文件下载的文件名.
//tachment指明文件是作为附件,支持下载,而不是直接打开查看的。
downloadFile//接收流并支持下载的输入名
只要在页面提供一个超链接的标记就可以动态下载了。
里程碑7: 2009-12-14
struts2动态下载框架的使用,文件下载的文件处理方式。
我们下载生成的临时文件定死的名称,但是在读写的时候会导致数据不一致。
所以我们必须做有效的处理:生成随机的文件名便是一种好的方式。
RandomStringUtils.randomAlphanumeric(length);这是Spring自动提供的。用以生成动态的随机的字符串。
在这里我们可以借鉴下这个思想,也就是注册码的生成。
里程碑8: 2009-12-15
将生成的临时文件删除策略:
1.通过HSSF的工作簿写入到临时文件中.但是要考虑删除问题.
1.1将生成的临时文件定死的名称,但是在读写的时候会导致数据不一致。
1.2将生成的临时文件取个随机文件名。
1.3那么接下来时删除文件的问题了,我们可以通过一个线程睡眠多长时间,然后进行删除。
1.4但是服务器一旦关闭,我们又得重新考虑问题了。
1.5所以我们得创建一个servlet其提供给系统启动时调用。则:不生成servlet映射文件,同时设置load-on-startup删除未删除的文件。
2.通过获得HSSF的getBytes()字节数组.也就是不生成临时文件,直接从内存中获得,但是生成的xls文件数据会丢失一部分.这是getBytes()造成的。
3.通过HSSF的工作簿在内存中的数据写入到一个字节数组输出流获得字节数组。
之后导入到输入流中,struts提供动态下载的框架中。供他人下载。
怎么在getInputStream方法中直接使用file.delete()方法不管用.
我的理解是:该临时文件就在一定的系统引用里,除非调用线程删除临时文件,否则删除失败.
也就是说:在多线程环境中,可能会有其他线程操作此文件,所以删不掉,注意程序中操作文件的同步问题。
总结:多线程时候 清理一下思路和环境 保证该文件没有被其他线程 以及系统调用 建议检查一下程序流程 看看该文件是否在其他部位被引用。
有错误的地方,有更好的想法,请指正!!!O(∩_∩)O~
文件下载的地方 已上传!!!!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

If when opening a file that needs to be printed, we will find that the table frame line has disappeared for some reason in the print preview. When encountering such a situation, we must deal with it in time. If this also appears in your print file If you have questions like this, then join the editor to learn the following course: What should I do if the frame line disappears when printing a table in Excel? 1. Open a file that needs to be printed, as shown in the figure below. 2. Select all required content areas, as shown in the figure below. 3. Right-click the mouse and select the "Format Cells" option, as shown in the figure below. 4. Click the “Border” option at the top of the window, as shown in the figure below. 5. Select the thin solid line pattern in the line style on the left, as shown in the figure below. 6. Select "Outer Border"

Excel is often used to process data in daily office work, and it is often necessary to use the "filter" function. When we choose to perform "filtering" in Excel, we can only filter up to two conditions for the same column. So, do you know how to filter more than 3 keywords at the same time in Excel? Next, let me demonstrate it to you. The first method is to gradually add the conditions to the filter. If you want to filter out three qualifying details at the same time, you first need to filter out one of them step by step. At the beginning, you can first filter out employees with the surname "Wang" based on the conditions. Then click [OK], and then check [Add current selection to filter] in the filter results. The steps are as follows. Similarly, perform filtering separately again

In our daily work and study, we copy Excel files from others, open them to add content or re-edit them, and then save them. Sometimes a compatibility check dialog box will appear, which is very troublesome. I don’t know Excel software. , can it be changed to normal mode? So below, the editor will bring you detailed steps to solve this problem, let us learn together. Finally, be sure to remember to save it. 1. Open a worksheet and display an additional compatibility mode in the name of the worksheet, as shown in the figure. 2. In this worksheet, after modifying the content and saving it, the dialog box of the compatibility checker always pops up. It is very troublesome to see this page, as shown in the figure. 3. Click the Office button, click Save As, and then

When processing data, sometimes we encounter data that contains various symbols such as multiples, temperatures, etc. Do you know how to set superscripts in Excel? When we use Excel to process data, if we do not set superscripts, it will make it more troublesome to enter a lot of our data. Today, the editor will bring you the specific setting method of excel superscript. 1. First, let us open the Microsoft Office Excel document on the desktop and select the text that needs to be modified into superscript, as shown in the figure. 2. Then, right-click and select the "Format Cells" option in the menu that appears after clicking, as shown in the figure. 3. Next, in the “Format Cells” dialog box that pops up automatically

Most users use Excel to process table data. In fact, Excel also has a VBA program. Apart from experts, not many users have used this function. The iif function is often used when writing in VBA. It is actually the same as if The functions of the functions are similar. Let me introduce to you the usage of the iif function. There are iif functions in SQL statements and VBA code in Excel. The iif function is similar to the IF function in the excel worksheet. It performs true and false value judgment and returns different results based on the logically calculated true and false values. IF function usage is (condition, yes, no). IF statement and IIF function in VBA. The former IF statement is a control statement that can execute different statements according to conditions. The latter

In the study of software, we are accustomed to using excel, not only because it is convenient, but also because it can meet a variety of formats needed in actual work, and excel is very flexible to use, and there is a mode that is convenient for reading. Today I brought For everyone: where to set the excel reading mode. 1. Turn on the computer, then open the Excel application and find the target data. 2. There are two ways to set the reading mode in Excel. The first one: In Excel, there are a large number of convenient processing methods distributed in the Excel layout. In the lower right corner of Excel, there is a shortcut to set the reading mode. Find the pattern of the cross mark and click it to enter the reading mode. There is a small three-dimensional mark on the right side of the cross mark.

1. Open the PPT and turn the page to the page where you need to insert the excel icon. Click the Insert tab. 2. Click [Object]. 3. The following dialog box will pop up. 4. Click [Create from file] and click [Browse]. 5. Select the excel table to be inserted. 6. Click OK and the following page will pop up. 7. Check [Show as icon]. 8. Click OK.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously
