JavaWeb -- Struts2 ResultType细化, 国际化
1. ResultType细化
<result-types> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> </result-types>
示例action
/** * ResultTypeAction */ public class ResultTypeAction extends ActionSupport { private static final long serialVersionUID = -6933309304624396640L; private String name = "中文" ; private String saveFileName ; /** * 只能请求转发到jsp或者是servlet. */ public String dispatcher() { System.out.println("dispatcher"); return "dispatcher"; } /** * 重定向到任意的url. */ public String redirect() { System.out.println("redirect"); return "redirect"; } /** * 重定向action的好处在于不需要硬编码url地址,即不需要绑定url模式.只能重定向到当前应用中的 * 某个action. */ public String redirectAction() { System.out.println("redirectAction"); return "redirectAction"; } /** * 得到输入流 * @throws FileNotFoundException */ public InputStream getIs() throws FileNotFoundException{ return new FileInputStream("d:/sunset.jpg"); } /** * 下载 */ public String stream(){ try { saveFileName = URLEncoder.encode("日落", "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return "stream" ; } /** * 链化action,请求转发到另外一个action,在同一个线程中,数据共享 */ public String chain(){ System.out.println("chain"); return "chain" ; } /** * 输出纯文本 */ public String plainText(){ System.out.println("plainText"); return "plainText" ; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSaveFileName() { return saveFileName; } public void setSaveFileName(String saveFileName) { this.saveFileName = saveFileName; } }
struts配置文档 rt.xml
<?xml version="1.0"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="RtPkg" namespace="/rt" extends="struts-default"> <action name="ResultTypeAction_*" class="cn.itcast.struts2.resulttype.ResultTypeAction" method="{1}"> <result name="dispatcher" type="dispatcher">/login.jsp</result> <!--调用action中的数据 用 ${...}--> <result name="redirect" type="redirect">/ui/UiAction_saveData.do?name=${name}</result> <result name="redirectAction" type="redirectAction"> <param name="actionName">UiAction_saveData</param> <param name="namespace">/ui</param> <param name="name">${name}</param> <param name="age">20</param> </result> <result name="stream" type="stream"> <param name="contentType">image/jpeg</param> <param name="contentDisposition"> attachment;filename="${@java.net.URLEncoder@encode('日落', 'UTF-8')}.jpg" </param> <param name="inputName">is</param> </result> <result name="chain" type="chain"> <param name="actionName">UiAction_saveData</param> <param name="namespace">/ui</param> </result> <result name="plainText" type="plainText"> <param name="location">/ipmsg.log</param> <param name="charSet">GBK</param> </result> </action> </package> </struts>
2. 国际化
显示页面
<%@ page language="java" contentType="text/html; charset=utf-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>reg.jsp</title> </head> <body> <s:form namespace="/i18n" action="I18nAction_toRegView" method="post" theme="xhtml"> <!--国际化显示语言选择--> <s:select name="request_locale" list="#{@java.util.Locale@CHINA:@java.util.Locale@CHINA.getDisplayName(@java.util.Locale@CHINA), @java.util.Locale@TAIWAN:@java.util.Locale@TAIWAN.getDisplayName(@java.util.Locale@TAIWAN), @java.util.Locale@US:@java.util.Locale@US.getDisplayName(@java.util.Locale@US), @java.util.Locale@UK:@java.util.Locale@UK.getDisplayName(@java.util.Locale@UK), @java.util.Locale@JAPAN:@java.util.Locale@JAPAN.getDisplayName(@java.util.Locale@JAPAN)}" listKey="key" listValue="value" onchange="this.form.submit();" value="locale"> </s:select> <!--国际化显示 提示信息--> <s:textfield name="name" key="label.name" /> <s:submit /> </s:form> </body> </html>
语言资源文件放在action同目录下, 命名格式 action同名 + 国家代码 + 语言代码
I18nAction_zh_CN.properties
label.name=userName_zh_CN
I18nAction_en_US.properties
label.name=userName_en_US