咱们在我的做业1中,用各类语言实现了一个命令行的四则运算小程序。进一步,本次要求把这个程序作成GUI(能够是Windows PC 上的,也能够是Mac、Linux,web,手机上的),成为一个有基本功能、必定价值的程序。在下面的功能需求中实现两个:javascript
最后封装成一种替代第一次做业中生成题目的方法(也就是从数据库中取)。html
对于这题,我画了一个简单的表面流程图,描述了这个切换语言的过程。(无技术基础看)前端
关于具体实现过程参考下面的执行过程:(有技术基础看)java
流程图分解说明:jquery
/* firstPage1:表示第一个页面从上往下第一个文字语言须要动态变化的位置 firstPage2:表示第一个页面从上往下第二个文字语言须要动态变化的位置 result.firstPage1:表示从后台获取的对应语言对应位置的配置文件内容 result.firstPage2:表示从后台获取的对应语言对应位置的配置文件内容 */ <script type="text/javascript"> $(function(){ var lanaguage1=$('input:radio:checked').val(); $.ajax({ dataType:"json", type:"post", data : { lanaguage : lanaguage1 }, url : "LanguageServlet?page=firstPage", success : function(result) { $("#firstPage1").html(result.firstPage1); $("#firstPage2").val(result.firstPage2); } }); }); </script>
因为涉及三种语言,因此设置了三个配置文件,分别是simpleChinese。properties,traditionalChinese.properties,English.properties.大概的内容以下图
c++
/* 加载语言配置文件代码 */ if(lanaguage.trim().equals("simpleChinese")){ InputStream inputStream= Test.class.getClassLoader().getResourceAsStream("simpleChinese.properties"); prop.load(inputStream); ///加载属性列表 } else if(lanaguage.trim().equals("traditionalChinese")){ InputStream inputStream= Test.class.getClassLoader().getResourceAsStream("traditionalChinese.properties"); prop.load(inputStream); ///加载属性列表 } else{ InputStream inputStream= Test.class.getClassLoader().getResourceAsStream("English.properties"); prop.load(inputStream); ///加载属性列表 } String firstPage1=prop.getProperty("firstPage1"); String firstPage2=prop.getProperty("firstPage2");
/* PrintWriter pw = response.getWriter(); JSON用到的包是阿里巴巴的fastJSON.jar */ Map<String,String>map=new HashMap<>(); map.put("firstPage1", firstPage1); map.put("firstPage2", firstPage2); String json=JSON.toJSONString(map); pw.print(json);//返回数据
以上就是功能一的所有描述。git
(该功能没有使用数据库,利用cookie功能实现存储,存储时间为一年)github
①设置了一个TimerReturn类来表示(便于数据处理)web
②前端js主要时间代码(百度而来,修改了一点点,本应附上连接后面没找到(PS:若有侵权告知删除))ajax
/* timeShow为显示时间的位置。 document.getElementById("time").value=two_char(h) + ":" + two_char(m) + ":" + two_char(s); 因为时间不在答案提交的form中,因此使用form中添加<input type="hidden" id="time" name="time">进行所需的时间提交 */ <script> function two_char(n) { return n >= 10 ? n : "0" + n; } function time_fun() { var sec=0; setInterval(function () { sec++; var date = new Date(0, 0) date.setSeconds(sec); var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds(); document.getElementById("timeShow").innerText = two_char(h) + ":" + two_char(m) + ":" + two_char(s); document.getElementById("time").value=two_char(h) + ":" + two_char(m) + ":" + two_char(s); }, 1000); } </script>
③后台进行返回结果的处理,把结果session而且把结果存储到cookie中。
String time=(String)request.getParameter("time"); List<TimerReturn>timeList=(List<TimerReturn>) request.getSession().getAttribute("timeList"); if(timeList==null){ timeList=new ArrayList<>(); } TimerReturn timeReturn=new TimerReturn(list.size()+"",time,list.size()-count+"",count*1.0/list.size()*100+"%"); timeList.add(timeReturn); request.getSession().setAttribute("flag", "true");//控制前端时间记录的表头是否显示,若是没有获取到flag则不显示 request.getSession().setAttribute("timeList", timeList);
/* 写入Cookie */ boolean flag=true;//用于断定是不是第一存入cookie,true表示第一次存 for(int i=0;i<c.length;i++){ if(c[i].getName().equals("timeList")){ String totalTimeList=c[i].getValue()+"M"+perTimeList;//用M拼接每次的结果 Cookie c1=new Cookie("timeList", totalTimeList); c1.setMaxAge(3600*24*30*12);//设置cookie存在时间单位s response.addCookie(c1); flag=false; } } if(flag==true){ Cookie c1=new Cookie("timeList", perTimeList); c1.setMaxAge(3600*24*30*12); response.addCookie(c1); }
④前端用c标签获取
<c:forEach var="Result" items="${timeList}"> <c:if test="${!empty Result.time}"> <tr> <td>${Result.totalCount}</td> <td>${Result.time}</td> <td>${Result.errorCount}</td> <td>${Result.correctRate}</td> </tr> </c:if> </c:forEach>
附上具体的网页连接:我也本身试试
事前讨论的代码规则
代码共同提交记录