A little try about Struts Internationalization

xiaoxiao2021-03-06  41

I have made a Struts internationalized applet. In fact, I have more than half a year, but I feel that I have only seen the trees and don't see Lin, and I decided to make a good time to make a lessons today. Otherwise, I really want to become a worker in the pipeline. If there is any mistake, please have an ax. 1. Modify the web.xml file, the content is as follows: Action org.apache.struts.Action.ActionServlet config /web-inf/struts-config.xml Application com.sharp.struts.ApplicationResources 1 Action *. Do The gray background part is an internationalization requires an increase in content, where com.sharp.struts.applicationResources is consistent with the directory structure under the Classes in your web-infirectory. As shown in the figure below, establish a resource file ApplicationResources.Source (Chinese source file, not Chinese resource file), ApplicationResou RCES_EN.PROPERTIES (English resource file).

Firstly ApplicationResources_en.properties file, as follows: owner.greet = Greetowner.name = Nameowner.address = Addressowner.email = Emailowner.tel = Chinese source files corresponding to the content of Tel follows: owner.greet = call owner.name = Name of owner .address = address OWNER.EMAIL = Email Owner.tel = Contact Phone then execute native2ascii -encoding gbk applicationresources.source in the CMD. The Chinese source file generates the Chinese resource file, content as owner.greet = / u79f0 / u547cowner.name = / u59d3 / u540downer.address = / u5730 / u5740owner.email = / u7535 / u5b50 / u90ae / u4ef6owner.tel = / u8054 / u7cfb / u7535 / u8bdd introduce here, native2ascii It is a tool for the JDK package. If you can't CMD directly execute the native2ascii command, check your environment variable, not specifically introduced here.

3, then introduce the contents of the struts-config.xml file, if familiar with Struts, friends also feel very simple

=================== -> < ! - ========== Action mapping definitions =============================== -> 4, start now preparing Java and JSP files Owner.jsp file content as follows: <% @ page contenttype =" text / html; charset = GB2312 "%> <% @ Taglib URI =" / Web-inf / struts-bean.tld "prefix =" bean "%> <% @ Taglib URI =" / Web-inf / struts-html.tld "prefix =" HTML "%> struts form for oownerform </ title> <script language =" javascript "></p> <p>Function ChangeLanguage (Language) {if (Language == "En") {document.all ("language"). Value = "en";} else {document.all ("language"). value = "cn";} Document.Forms [0] .Submit ();} </ script> </ head> <body> <html: form action = "/ owner"> <input type = "hidden" name = "formaction" value = "Changeiang Guanguage "> <Input type =" hidden "name =" language "> <bean: message key =" oer.greet "/>: <html: select material =" greet "> <html: option value ="> </ HTML: OPTION> <html: option value = "mr."> mr. </ html: option> <html: option value = "miss."> miss. </ html: option> <html: option value = "MRS . "> Mrs. </ Html: option> </ html: select> <html: errors proty =" greet "/> </br> <bean: message key =" oer.name "/>: <HTML: Text Property = "name" /> <html: errors proty = "name" /> </br> <bean: message key = "owner.address" />: <html: text property = "address" /> <html: Errors property = "address" /> </br> <bean: message key = "oowner.email" />: <html: text protety = "EMAI l "/> <html: errors proty =" email "/> </br> <bean: message key =" oerer.tel "/>: <html: text protety =" tel "/> <html: errors style = "tel" /> <//br> <html: submit /> <</p> <p>HTML: Cancel /> <input type = "button" name = "" value = "English" οnclick = "ChangeLanguage ('en')> <input type =" name = "language" value = " "Οnclick =" ChangeLanguage ('CN')> </ html: form> <body> </ html: html> Owneraction.java file content is as follows: // create by xslt generator for eclipse.// xsl: not found (java.io.filenotFoundException: (Bad file descriptor) // default xsl buy: easystruts.jar $ org.easystruts.xslgen.javaclass.xslpackage com.sharp.struts.Action;</p> <p>Import java.util.locale;</p> <p>Import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;</p> <p>import org.apache.struts.Globals; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action .Action mapping; import org.apache.struts.Actions.dispatchaction;</p> <p>Import com.sharp.struts.form.ownerformation;</p> <p>/ ** * Owneraction.java created by Easystruts - xsltgen. * Http://easystruts.sf.net * created on 10-09-2004 * * xdoclet definition: * @struts: action path = "/ @Name =" ownerForm "input =" / owner.jsp "validate =" true "* @struts: action-forward name =" / success.jsp "path =" / success.jsp "* / public class OwnerAction extends DispatchAction {public ActionForward changeLanguage ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {String language = request.getParameter ( "language"); if (language.equals ( "en").) {request.getSession () setAttribute (Globals.LOCALE_KEY, Locale .Ene {Request.getSession (). Setttribute (globals.locale_key, locale.chinese);} return mapping.findforward ("Owner");}} You need to pay attention to it, don't make a mistake, I am It took more than an hour, because the file name is wrong with Locale.English is a local object, and the getLanguage () method returns EN, the suffix of the resource file (ApplicationResources_en.properties) The same locale.Chinese is a Locale object, GetLanguage () Method Returns EN, with resource files (ApplicationResources_ZH _Cn.properties) The suffix is ​​also used in the same OWNERMM.JAVA file, the content is as follows: // create by xslt generator for eclipse.// xsl: not found (java.io.filenotFoundException: (Bad file descriptor) //// DEFAULT XSL USED: Easystruts.jar $ org.easystruts.xslgen.javaclass.xslpackage com.sharp.struts.form;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import org.Apache.struts.action.Actionerror; import org.apache.struts.Action.Apache.Struts.Action.Apache.struts.action.Apache.struts.Action.ActivityMapping;</p> <p>/ ** * Ownerform.java created by Easystruts - xsltgen. * Http://easystruts.sf.net * created on 10-09-2004 * * xdoclet definition: * @struts: form name = "oownerform" * / public class Ownerform extends an actionform {// -------------------------------------------- ------------- Instance Variables</p> <p>/ ** Email property * / private string email;</p> <p>/ ** Greet Property * / private string greet = "mr.";</p> <p>/ ** Address property * / private string address;</p> <p>/ ** Tel Property * / private INT TEL = 0;</p> <p>/ ** Name Property * / Private string name; // ------------------------------------- -------------------- Methods ... // getter, setter mothod, here</p> <p>} 5, summarizing such an international small example can run, but the above write is rough, and it is necessary to have a certain understanding of Java and Struts. There are other practices on internationalization on the Internet, or you can refer to. If the above is biased, please ax</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-57331.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="57331" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.034</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'dVjFHdrgXULZZRsSCUDOw8N2kkBTNVG_2BWL4RiDq96wRyzQg348x_2BKcAFC_2BToHhD3dXxfdm1V8naBzvXBQZRLnQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>