您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用SpringMvc實現簡易計算器功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用SpringMvc實現簡易計算器功能”吧!
package ssm1.domain; public class JiSuan { private int numOne; private int numTwo; private String yunSuan; private int result; public int getNumOne() { return numOne; } public void setNumOne(int numOne) { this.numOne = numOne; } public int getNumTwo() { return numTwo; } public void setNumTwo(int numTwo) { this.numTwo = numTwo; } public String getYunSuan() { return yunSuan; } public void setYunSuan(String yunSuan) { this.yunSuan = yunSuan; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } @Override public String toString() { return "JiSuan{" + "numOne=" + numOne + ", numTwo=" + numTwo + ", yunSuan='" + yunSuan + '\'' + ", result=" + result + '}'; } }
package ssm1.Controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import ssm1.domain.JiSuan; @Controller @RequestMapping(path="/calculatorController") public class CalculatorController { @RequestMapping(path="/calculate") @ResponseBody public JiSuan suan( Model model,@RequestBody JiSuan jiSuan) { System.out.println(jiSuan); int numThree=0; if(jiSuan.getYunSuan().equals("+")) { numThree=jiSuan.getNumOne()+jiSuan.getNumTwo(); } else if(jiSuan.getYunSuan().equals("-")) { numThree=jiSuan.getNumOne()-jiSuan.getNumTwo(); } else if(jiSuan.getYunSuan().equals("*")) { numThree=jiSuan.getNumOne()*jiSuan.getNumTwo(); }else { numThree=jiSuan.getNumOne()/jiSuan.getNumTwo(); } jiSuan.setResult(numThree); model.addAttribute("jiSuan",jiSuan); return jiSuan; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js" type="text/javascript"></script> <html> <head> <title>Title</title> <script language="JavaScript"> $(function () { $("#btn").click(function(){ let num1=$("#numOne").val(); let num2=$("#numTwo").val(); let yun=$("#yunsuan").val(); let param='{"numOne":'+'"'+num1+'"'+',"numTwo":'+'"'+num2+'"'+',"yunSuan":'+'"'+yun+'"'+'}'; // let array=new String(); $.ajax( { url:"${pageContext.request.contextPath}/calculatorController/calculate", contentType:"application/json;charset=utf-8", data:param, dataType:"Json", type:"POST", success:function (data) { var dataObj = data.result; $("#result").html("結果="+dataObj); $("#test2").innerHTML="success"; } } ) }) }) function checkNum1(num) { let num1 = num.value; if (num1 == '') { alert("不能輸入為空"); return false; } if (Math.round(num1) != num1) { alert("這個數不是一個數字"); num.value = ''; num.onfocus; return false; } return true; } </script> </head> <body> <form name="form1" action="" method="post"> 請輸入第一個數:<input type="text" name="numOne" id="numOne" onblur="checkNum1(this)"> <select name="yunSuan" id="yunsuan"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> 請輸入第二個數:<input type="text" name="numTwo" id="numTwo" onblur="checkNum1(this)"> <input type="button" value="計算" id="btn"> <label id="result"></label> <label id="test2"></label> </form> </body> </html>
js包,calculator.jsp,放在webapp下。
運行截圖。
感謝各位的閱讀,以上就是“怎么用SpringMvc實現簡易計算器功能”的內容了,經過本文的學習后,相信大家對怎么用SpringMvc實現簡易計算器功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。