Struts的国际化
本地化和国际化的概念
Java对国际化的支持
Struts框架对国际化的支持
本地化和国际化的概念
在过去,软件开发者在开发应用程序时,将注意力集中于实现具体的业务逻辑。软件面向的用户群是固定的,软件只需要支持一种语言。如今,随着跨国业务的迅猛发展,需要同一个软件同时支持多种语言和国家。
国际化[Internationalization](简称为I18N)指的是在软件设计阶段,就应该使软件具有支持多种语言和地区的功能。软件的本地化与国际化的区别如下图:
Java对国际化的支持
Locale类
java.util.Locale类是最重要的Java国际化类,在Java语言中,几乎所有对国际化和本地化的支持都依赖于这个类。Locale类的实例代表一个特定的语言和地区。
ResourceBundle类
java.util.ResourceBundle类提供存放和管理与Locale相关的资源的功能。这些资源包括按钮上的文字、状态信息、图片名、错误信息和网页标题等。
ApplicationResources_en.properties
hello=Hello World!
ApplicationResources_zh_CN.properties
hello=你好,世界!
<html:form action="/selectLocale">
<html:select property="locale">
<html:option value="en">英文</html:option>
<html:option value="zh_CN">中文</html:option>
</html:select>
<html:submit>go</html:submit>
</html:form><hr>
<bean:message key="hello" />
SelectLocaleForm selectLocaleForm = (SelectLocaleForm) form;
setLocale(request, new Locale(selectLocaleForm.getLocale()));
return mapping.getInputForward();
Struts实现文件上传
举例:
ActionForm
package form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class UploadForm extends ActionForm {
private FormFile file;
private int size;
private String name;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
……
}
Action里要写的
UploadForm uploadForm = (UploadForm) form;
String dir=servlet.getServletContext().getRealPath("/file");
FormFile file=uploadForm.getFile();
if(file==null){
return mapping.getInputForward();
}
uploadForm.setName(file.getFileName());
uploadForm.setSize(file.getFileSize());
InputStream in=file.getInputStream();
OutputStream out=new FileOutputStream(dir+"/"+uploadForm.getName());
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
file.destroy();
return mapping.findForward("ok");
JSP
upload.jsp
<html:form action="/upload" enctype="multipart/form-data">
选择文件:<html:file property="file"/><br/>
<html:submit>上传</html:submit>
</html:form>
up_ok.jsp
<logic:notEmpty name="uploadForm" property="name">
<h1>文件上传成功</h1><hr>
文件名称:<bean:write name="uploadForm" property="name"/><br>
文件大小:<bean:write name="uploadForm" property="size"/>字节<br>
<hr>
</logic:notEmpty>
<html:link href="index.jsp">返回</html:link>
struts-config.xml
<struts-config>
<form-beans>
<form-bean name="uploadForm" type="form.UploadForm" />
</form-beans>
<action-mappings>
<action attribute="uploadForm"
input="/upload.jsp"
name="uploadForm"
path="/upload"
scope="request"
type="action.UploadAction"
validate="false">
<forward name="ok" path="/up_ok.jsp" />
</action>
</action-mappings>
</struts-config>
Struts实现文件下载
download.jsp
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.io.File"%>
<html>
<head><title></title></head>
<body>
<%
File file = new File(application.getRealPath("/file"));
String[] fileNames = file.list();
for (int i = 0; i < fileNames.length; i++) {
String fileName=java.net.URLEncoder.encode(fileNames[i],"gbk");
%>
<a href="download.do?fileName=<%=fileName%>"><%=fileNames[i]%></a>
<br>
<%}%>
</body>
</html>
DownloadAction.java
String filename = request.getParameter("fileName");
filename = new String(filename.getBytes("iso_8859_1"), "gbk");
String path = servlet.getServletContext().getRealPath("/") + "file/" + filename;
response.reset();
response.setContentType("application/x-msdownload;charset=gbk");
filename = java.net.URLEncoder.encode(filename, "UTF-8");
response.setHeader("Content-Disposition", "filename=" + filename);
InputStream in = new FileInputStream(path);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
return null;
struts-config.xml
<struts-config>
<action-mappings>
<action path="/download" type="action.DownloadAction" />
</action-mappings>
</struts-config>