一、Struts2国际化
所谓国际化是指你的web系统,在不同国家或地区被访问,其中的一些主要信息,如注册信息中字段,错误信息提示等显示结果应该与该地区或国家语言相同。这样用户很好理解你的网页。
Web系统国际化通过两步来完成。第一通过将文字内容以特定的方式存放在特定的文件中。第二,在运行时根据当前的语言环境决定从哪个文件中读取文字内容。
Java中国际化的概念是将不同国家的语言描述的相同的东西放在各自对应的属性文件中,如果这个文件的名字叫做Message,那么对应语言的文件分别为:
中文中国 message_zh_CN.properties
日文日本 message_ja_JP.properties
英文美国 message_en_US.properties
1、JSP页面上的国际化
<s:i18n name="message">
< !-- key="hello"依据读取资源文件中的hello关键字对应的内容,{0}表示第一个参数输出位置即${username}在这里输出-->
<s:text key="hello">
<s:param>${username}</s:param>
</s:text>
</s:i18n>
中英文资源如下:
message_en_US.properties文件配置:
hello=hello world,{0}
message_zh_CN.properties文件配置:
hello=你好,{0}
2、表单元素的Label国际化
未国际化:
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
国际化后:
<s:textfield name="username" key="uname"></s:textfield>
<s:textfield name="password" key="pword"></s:textfield>
中英文资源如下:
message_en_US.properties文件,配置:
uname=username
pword=password
message_zh_CN.properties文件,配置:
uname=用户名
pword=密码
3、Action中的国际化
未国际化:
this.addFieldError("username", "the username error!");
this.addFieldError("password", "the password error!");
国际化后:
this.addFieldError("username", "username.error");
this.addFieldError("password", "password.error");
中英文资源如下:
message_en_US.properties文件配置:
username.error = the username error !
password.error = the password error!
message_zh_CN.properties文件配置:
username.error=用户名错误!
username.error=密码错误!
4、配置文件中的国际化。以输入校验的LoginAction-validation.xml为例:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="username.empty"></message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message key="username.size"></message>
</field-validator>
</field>
二、Struts2上传下载
在Java领域中,有两个常用的文件上传项目:一个是Apache组织Jakarta的Common-FileUpload组件(http://commons.apache.org/fileupload/),另一个是Oreilly组织的COS框架(http://www.servlets.com/cos/)。利用这两个框架都能很方便的实现文件的上传。
上传文件主要是通过读写二进制流进行操作的。Form表单元素的enctype属性指定的是表单数据的编码方式。
multipart/form-data。这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里。
Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来。但Struts2在原有的上传解析器基础上做了进一步封装,更进一步简化了文件上传。
Struts2默认使用的是Jakarta的Common-FileUpload框架来上传文件,因此,要在web应用中增加两个Jar文件:commons- fileupload-1.2.jar和commons-io-1.3.1.jar。
1、上传文件JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<form action="fileUpload " method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
2、上传文件Action:
package com.action;
import java.io.*;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private static final int BUFFER_SIZE = 16 * 1024;
private String title; // 文件标题
private File upload; // 上传文件域对象 =上传文件的内容所对应的属性名,本例取:upload--前端file组件的参数名
private String uploadFileName; // 上传文件名= upload+"FileName"
private String uploadContentType; // 上传文件类型= upload+"ContentType"
/*
文件上传Action中,与file组件相关的属性的名字:
1)File组件直接对应的属性:类型为File,名字为表单组件中的参数名,本例
2)上传文件名=file属性名(同表单参数名)+“FileName” :file+FileName
3)上传文件类型= file属性名(同表单参数名)+“ContentType”
*/
private String savePath; // 通过参数封装进来的,就是struts中的param中读取出来,保存文件的目录路径(通过依赖注入)
private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String execute() throws Exception {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.getServletContext().getRealPath(
this.getSavePath())
+ "/" + this.getUploadFileName();
System.out.println("上传的文件的类型:" + this.getUploadContentType());
File dstFile = new File(dstPath);
copy(this.upload, dstFile);
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
3、下载文件JSP:
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<a href="down">下载文件</a>
</body>
</html>
4、下载文件Action:
package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
public class DownAction extends ActionSupport{
private InputStream downloadFile;
public InputStream getDownloadFile() {
File file = new File("d:/a/abc.xls");
InputStream in = null;
try{
in = new FileInputStream(file);
}catch (Exception e) {
}
return in;
}
@Override
public String execute() throws Exception {
return "success";
}
}