struts 国际化
国际化:i18n:internationalization,能够实现读取不同的资源文件显示不同的字符信息。以适应不同的地区和语言。
1:处理汉字显示:
使用命令:native2ascii 原文件名 新文件名
把汉字转化为ascii码
对于中文,资源文件查找的顺序是:
ApplicationResources_zh_cn.properties-->ApplicationResources_zh.properties-->ApplicationResources.properties
其他语言也是找带相应后缀的文件再找ApplicationResources.properties
2:jsp页面
方法1: Set charset in your JSP page: 在所有JSP页面里设定编码方式
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
方法2: Use filter to set charEncoding to get input from client 用filter设定获得用户输入内容的编码方式
3:在Internet 选项里,语言-->语言首选项设置语言
Tag Libraries -- Unit 5
HTML Tags
Bean Tags
Logic Tags
nested Tags
********************************************************************************
1. HTML Tags:
html:html html:base html:link
html:rewrite html:img html:form
html:text html:cancel html:reset
html:submit html:hidden html:checkbox
html:multibox html:radio html:select
html:file html:errors html:messages
******************************************
用于生成基本HTML元素的标签
******************************************
<html:link forward href page paramId paramName paramProperty name/>
生成 <a href /> 标签
will rewrite URL, add SessionID in URL if client turn off Cookie
1:外部完整的url连接
<html:link href="http://www.yahoo.com" > yahoo </html:link>
--> <a href="http://www.yahoo.com"> // !!!!note: full URL name
2:相对url连接
<html:link page="/my.do" > my page </html:link> //!!!! same web application
--> <a href="/yourWebapp/my.do"> my page </a>
3:相对url连接带参数
<html:link page="/your.do?para1=hello¶2=world" > Your page </html:link>
--> <a href="/yourWebapp/your.do?para1=hello¶2=world"> Your page </a>
4: 全局转发url连接
<html:link forward="success" > Success </html:link>
// only in <global> <forward> tag, !!!not in <action> <forward> tag
--> <a href="yourWebApp/success.jsp">
在struts-config.xml文件里:
<global-forward>
<forward name="success" path="/success.jsp" >
</global-forward>
5:带有页面变量的url连接
<% String s="hello";
request.setAttribute("stringBeanName", s);
%>
<html:link page="/his.do" paramId="param1" paramName="stringBeanName">
his page
</html:link>
--> <a href="/yourWebapp/his.do?param1=hello"> his page </a>
6: 带有javabean属性的url连接
<% Student st=new Student("zhang", 25);
request.setAttribute("student", st);
%>
<html:link page="/her.do" paramId="param1" paramName="student" paramProperty="age">
her page
</html:link>
--> <a href="/yourWebapp/her.do?param1=25"> her page </a>
7: 传递多个参数的url连接
<% HashMap map=new HashMap(); //URL has many parameters
map.put("name1", "Hello");
map.put("name2", 'world");
request.setAttribute("mapBean", map);
%>
<html:link page="/our.do" name="mapBean"> our page </html:link>
--> <a href="/our.do?name1=hello&name2=world"> our page </a>
-----------------------
在表单中上传文件的标签
<html:file property=" fileName" /> 生成<input type="file" name="fileName"/> 标签
//property for FormFile attribute of ActionForm
jsp:
<html:form action="myfile.do" method="post" enctype="multipart/form-data">
please select the file you would like to upload:
<html:file property="fileName" />
<html:submit />
</html:form>
Form:
public class MyForm extends ActionForm{
private FormFile fileName;
....
}
Action:
FormFile file = uploadfileForm.getFile();//得到上传文件
if(file==null){
return mapping.getInputForward();
}
String filename= file.getFileName();//得到文件名
uploadfileForm.setFilename(filename);
String size = Integer.toString(file.getFileSize())+"byte";//得到文件大小
uploadfileForm.setSize(size);
try {
InputStream is = file.getInputStream();//获得文件的输入流
String storepath = servlet.getServletContext().getRealPath("/filepuload");//获得存放路径
System.out.print(storepath);
OutputStream out = new FileOutputStream(storepath+"/"+filename);//获得文件输出流
int bytes=0;
byte [] buffer = new byte[8192];
while((bytes = is.read(buffer,0,8192))!=-1){
out.write(buffer,0,bytes);
}
out.close();
is.close();
file.destroy();//释放文件连接
------------------------------------------------
<html:errors property /> 输出错误信息
in jsp page: <html:errors property="propertyName1" />
--------------------
<html:messages property/> 输出信息
in page: <html:messages property="propertyName1" />
********************************************************************************
********************************************************************************
Bean Tags:
bean:cookie bean:header bean:parameter bean:page
*bean:message bean:resource bean:struts bean:include
*bean:define *bean:size *bean:write
*************************************************
检索HTTP请求信息或JSP隐含对象
*************************************************
----------------------------------------------------
<bean:parameter id* multiple name* value />
读取HTTP请求参数
Example1. <bean:parameter id="arg" name="fname" value="default" /> //!!!value -> default value
<bean:write name="arg" />
request: http://localhost:8080/mywebapp/mypage.jsp?fname=zhang
-----> zhang
Example2: <bean:parameter id="arg" multiple="yes" name="options" value="noarg" />
similar to
<% String[] arg=request.getParameterValues("options);
for (int i=0; i<arg.length;i++) {
out.writeln(arg[i]);
}
%>
http://localhost:8080/mywebapp/mypage.jsp?options=zhang&options=wang&options=chen
-----> zhang
wang
chen
------------------------------------------
**************************************************
访问WEB应用资源
***************************************************
<bean:message bundle key name property arg0, arg1/ >
Example1. key value from message-resources file
* label.hello=Hello, welcome to my page // in message-resources file
<bean:message bundle="special" key="label.hello" /> //in special resource bundle
-----> Hello, welcome to my page
* hello=Hello, {0} // in message-resources file
<bean:message bundle="special" key="hello" arg0="how are you"/>
-------> Hello, how are you
Example2. value of key is from name: String bean
<% request.setAttribute("myString", "label.hello"); %>
<bean:message name="myString" />
-----> Hello, welcome to my page
Example3. value of key is from name: Java bean
<% Mybean bean=new Mybean();
bean.setValue("label.hello);
request.setAttribute("mybean", bean);
%>
<bean:message bundle="special" name="mybean" property="value" />
-----> Hello, welcome to my page
---------------------------------
<bean:include id forward page href/>
把其他WEB资源的内容存放在一个变量里, 而不是直接显示在页面上(可以通过<bean:write> 显示)
Example1: <bean:include id="test" forward="banner" /> // !!!in <global-forwards> tag
<bean:write name="test" filter="false"/>
Example2: <bean:include id="test" page="/banner.jsp" />
<bean:write name="test" filter="false"/>
Example2: <bean:include id="test" href="yourWebApp/banner.jsp" />
<bean:write name="test" filter="false"/>
*******************************************
定义或输出JavaBean
*******************************************
<bean:define id* toScope name property type scope value />
定义一个变量
Example1: <bean:define id="myId" value="hello, world" toScope="session"/>
<bean:write name="myId" scope="session" />
-----> hello, world
<% session.setAttribute("myId", "hello, world");
out.println(session.getAttribute("myId");
%>
-----------------------
<bean:size id name scope />
获得Map, Collection或数组的长度
Example1: <% List l=new Vector();
l.add("hello");
l.add("world");
request.setAttribute("list", l);
%>
<bean:size id="length" name="list" scope="request"/>
<bean:write name="length" />
-> 2
--------------------------------------------
<bean:write name property format filter scope />
输出某个Bean或它的属性的内容, format 用于设置输出数据的格式, filter=true把输出内容中的特殊HTML符号作为普通字符来显示.
Example1: <% request.setAttribute("pi", Float.valueOf("3.14159"));%>
<bean:write format="#.##" name="pi" scope="request" />
-> 3.14
<% request.setAttribute("str", "<h3> hello,world </h3> "); %>
<bean:write name="str" scope="request" filter=true/>
-> <h3> hello,world </h3>
*************************************************************************************************
*************************************************************************************************
Logic tags:
logic:equal logic:notEqual logic:lessThan
logic:lessEqual logic:greaterThan logic:greaterEqual
logic:match logic:notmMatch
logic:empty logic:notEmpty
logic:present logic:notPresent
logic:messagesPresent logic:messagesNotPresent
logic:iterate
logic:forward logic:redirect
******************************************
进行循环遍历的logic标签
在默认情况下, 将依此在page, request, session, application范围里寻找name属性指定的变量. 此外, 也可以通过scope属性来指定变量的存在范围.
******************************************
<logic:iterate id indexId name offset length > offset: 开始位置
<%
Vector v = (Vector)request.getAttribute("alluser");
request.setAttribute("alluser",v);
%>
<TABLE>
<TR>
<TD>no</TD>
<td>id</td>
<TD>name</TD>
<TD>password</TD>
<TD>select</TD>
</TR>
<logic:iterate id ="alluser" indexId = "ind" name="alluser">
<tr>
<TD>
<bean:write name="ind"/>
</TD>
<TD>
<bean:write name="alluser" property="id"/>
<input type="hidden" name="id" value='<bean:write name="alluser" property="id"/>'/>
</TD>
<TD>
<bean:write name="alluser" property="name"/>
</TD>
<TD>
<bean:write name="alluser" property="password"/>
</TD>
<TD>
<html:submit>delete</html:submit>
</TD>
</tr>
</logic:iterate>
</TABLE>
</body>
国际化:i18n:internationalization,能够实现读取不同的资源文件显示不同的字符信息。以适应不同的地区和语言。
1:处理汉字显示:
使用命令:native2ascii 原文件名 新文件名
把汉字转化为ascii码
对于中文,资源文件查找的顺序是:
ApplicationResources_zh_cn.properties-->ApplicationResources_zh.properties-->ApplicationResources.properties
其他语言也是找带相应后缀的文件再找ApplicationResources.properties
2:jsp页面
方法1: Set charset in your JSP page: 在所有JSP页面里设定编码方式
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
方法2: Use filter to set charEncoding to get input from client 用filter设定获得用户输入内容的编码方式
3:在Internet 选项里,语言-->语言首选项设置语言
Tag Libraries -- Unit 5
HTML Tags
Bean Tags
Logic Tags
nested Tags
********************************************************************************
1. HTML Tags:
html:html html:base html:link
html:rewrite html:img html:form
html:text html:cancel html:reset
html:submit html:hidden html:checkbox
html:multibox html:radio html:select
html:file html:errors html:messages
******************************************
用于生成基本HTML元素的标签
******************************************
<html:link forward href page paramId paramName paramProperty name/>
生成 <a href /> 标签
will rewrite URL, add SessionID in URL if client turn off Cookie
1:外部完整的url连接
<html:link href="http://www.yahoo.com" > yahoo </html:link>
--> <a href="http://www.yahoo.com"> // !!!!note: full URL name
2:相对url连接
<html:link page="/my.do" > my page </html:link> //!!!! same web application
--> <a href="/yourWebapp/my.do"> my page </a>
3:相对url连接带参数
<html:link page="/your.do?para1=hello¶2=world" > Your page </html:link>
--> <a href="/yourWebapp/your.do?para1=hello¶2=world"> Your page </a>
4: 全局转发url连接
<html:link forward="success" > Success </html:link>
// only in <global> <forward> tag, !!!not in <action> <forward> tag
--> <a href="yourWebApp/success.jsp">
在struts-config.xml文件里:
<global-forward>
<forward name="success" path="/success.jsp" >
</global-forward>
5:带有页面变量的url连接
<% String s="hello";
request.setAttribute("stringBeanName", s);
%>
<html:link page="/his.do" paramId="param1" paramName="stringBeanName">
his page
</html:link>
--> <a href="/yourWebapp/his.do?param1=hello"> his page </a>
6: 带有javabean属性的url连接
<% Student st=new Student("zhang", 25);
request.setAttribute("student", st);
%>
<html:link page="/her.do" paramId="param1" paramName="student" paramProperty="age">
her page
</html:link>
--> <a href="/yourWebapp/her.do?param1=25"> her page </a>
7: 传递多个参数的url连接
<% HashMap map=new HashMap(); //URL has many parameters
map.put("name1", "Hello");
map.put("name2", 'world");
request.setAttribute("mapBean", map);
%>
<html:link page="/our.do" name="mapBean"> our page </html:link>
--> <a href="/our.do?name1=hello&name2=world"> our page </a>
-----------------------
在表单中上传文件的标签
<html:file property=" fileName" /> 生成<input type="file" name="fileName"/> 标签
//property for FormFile attribute of ActionForm
jsp:
<html:form action="myfile.do" method="post" enctype="multipart/form-data">
please select the file you would like to upload:
<html:file property="fileName" />
<html:submit />
</html:form>
Form:
public class MyForm extends ActionForm{
private FormFile fileName;
....
}
Action:
FormFile file = uploadfileForm.getFile();//得到上传文件
if(file==null){
return mapping.getInputForward();
}
String filename= file.getFileName();//得到文件名
uploadfileForm.setFilename(filename);
String size = Integer.toString(file.getFileSize())+"byte";//得到文件大小
uploadfileForm.setSize(size);
try {
InputStream is = file.getInputStream();//获得文件的输入流
String storepath = servlet.getServletContext().getRealPath("/filepuload");//获得存放路径
System.out.print(storepath);
OutputStream out = new FileOutputStream(storepath+"/"+filename);//获得文件输出流
int bytes=0;
byte [] buffer = new byte[8192];
while((bytes = is.read(buffer,0,8192))!=-1){
out.write(buffer,0,bytes);
}
out.close();
is.close();
file.destroy();//释放文件连接
------------------------------------------------
<html:errors property /> 输出错误信息
in jsp page: <html:errors property="propertyName1" />
--------------------
<html:messages property/> 输出信息
in page: <html:messages property="propertyName1" />
********************************************************************************
********************************************************************************
Bean Tags:
bean:cookie bean:header bean:parameter bean:page
*bean:message bean:resource bean:struts bean:include
*bean:define *bean:size *bean:write
*************************************************
检索HTTP请求信息或JSP隐含对象
*************************************************
----------------------------------------------------
<bean:parameter id* multiple name* value />
读取HTTP请求参数
Example1. <bean:parameter id="arg" name="fname" value="default" /> //!!!value -> default value
<bean:write name="arg" />
request: http://localhost:8080/mywebapp/mypage.jsp?fname=zhang
-----> zhang
Example2: <bean:parameter id="arg" multiple="yes" name="options" value="noarg" />
similar to
<% String[] arg=request.getParameterValues("options);
for (int i=0; i<arg.length;i++) {
out.writeln(arg[i]);
}
%>
http://localhost:8080/mywebapp/mypage.jsp?options=zhang&options=wang&options=chen
-----> zhang
wang
chen
------------------------------------------
**************************************************
访问WEB应用资源
***************************************************
<bean:message bundle key name property arg0, arg1/ >
Example1. key value from message-resources file
* label.hello=Hello, welcome to my page // in message-resources file
<bean:message bundle="special" key="label.hello" /> //in special resource bundle
-----> Hello, welcome to my page
* hello=Hello, {0} // in message-resources file
<bean:message bundle="special" key="hello" arg0="how are you"/>
-------> Hello, how are you
Example2. value of key is from name: String bean
<% request.setAttribute("myString", "label.hello"); %>
<bean:message name="myString" />
-----> Hello, welcome to my page
Example3. value of key is from name: Java bean
<% Mybean bean=new Mybean();
bean.setValue("label.hello);
request.setAttribute("mybean", bean);
%>
<bean:message bundle="special" name="mybean" property="value" />
-----> Hello, welcome to my page
---------------------------------
<bean:include id forward page href/>
把其他WEB资源的内容存放在一个变量里, 而不是直接显示在页面上(可以通过<bean:write> 显示)
Example1: <bean:include id="test" forward="banner" /> // !!!in <global-forwards> tag
<bean:write name="test" filter="false"/>
Example2: <bean:include id="test" page="/banner.jsp" />
<bean:write name="test" filter="false"/>
Example2: <bean:include id="test" href="yourWebApp/banner.jsp" />
<bean:write name="test" filter="false"/>
*******************************************
定义或输出JavaBean
*******************************************
<bean:define id* toScope name property type scope value />
定义一个变量
Example1: <bean:define id="myId" value="hello, world" toScope="session"/>
<bean:write name="myId" scope="session" />
-----> hello, world
<% session.setAttribute("myId", "hello, world");
out.println(session.getAttribute("myId");
%>
-----------------------
<bean:size id name scope />
获得Map, Collection或数组的长度
Example1: <% List l=new Vector();
l.add("hello");
l.add("world");
request.setAttribute("list", l);
%>
<bean:size id="length" name="list" scope="request"/>
<bean:write name="length" />
-> 2
--------------------------------------------
<bean:write name property format filter scope />
输出某个Bean或它的属性的内容, format 用于设置输出数据的格式, filter=true把输出内容中的特殊HTML符号作为普通字符来显示.
Example1: <% request.setAttribute("pi", Float.valueOf("3.14159"));%>
<bean:write format="#.##" name="pi" scope="request" />
-> 3.14
<% request.setAttribute("str", "<h3> hello,world </h3> "); %>
<bean:write name="str" scope="request" filter=true/>
-> <h3> hello,world </h3>
*************************************************************************************************
*************************************************************************************************
Logic tags:
logic:equal logic:notEqual logic:lessThan
logic:lessEqual logic:greaterThan logic:greaterEqual
logic:match logic:notmMatch
logic:empty logic:notEmpty
logic:present logic:notPresent
logic:messagesPresent logic:messagesNotPresent
logic:iterate
logic:forward logic:redirect
******************************************
进行循环遍历的logic标签
在默认情况下, 将依此在page, request, session, application范围里寻找name属性指定的变量. 此外, 也可以通过scope属性来指定变量的存在范围.
******************************************
<logic:iterate id indexId name offset length > offset: 开始位置
<%
Vector v = (Vector)request.getAttribute("alluser");
request.setAttribute("alluser",v);
%>
<TABLE>
<TR>
<TD>no</TD>
<td>id</td>
<TD>name</TD>
<TD>password</TD>
<TD>select</TD>
</TR>
<logic:iterate id ="alluser" indexId = "ind" name="alluser">
<tr>
<TD>
<bean:write name="ind"/>
</TD>
<TD>
<bean:write name="alluser" property="id"/>
<input type="hidden" name="id" value='<bean:write name="alluser" property="id"/>'/>
</TD>
<TD>
<bean:write name="alluser" property="name"/>
</TD>
<TD>
<bean:write name="alluser" property="password"/>
</TD>
<TD>
<html:submit>delete</html:submit>
</TD>
</tr>
</logic:iterate>
</TABLE>
</body>