通過(guò)button將form表單的數(shù)據(jù)提交到action層的實(shí)例
form表單中不需要寫(xiě)action的路徑,需要給form表單一個(gè)唯一的id,將你要提交的信息的表單中的標(biāo)簽name="action中的javabean對(duì)象.javabean屬性"。給button按鈕添加一個(gè)onclick()點(diǎn)擊事件,并實(shí)現(xiàn)該點(diǎn)擊事件,在該onclick()方法中通過(guò)ajax將form表單中的數(shù)據(jù)提交給action層
JSP頁(yè)面中的代碼:
<form id="handleform"> <!-- 根據(jù)學(xué)生id修改學(xué)生信息 --> <input type="hidden" name="student.stuid"/><!-- 隱藏學(xué)生id --> <div class="input-group el_modellist" role="toolbar"> <span class="el_spans">要修改的班級(jí):</span> <select class="selectpicker form-control" name="student.className" id="fmchechunit" title="請(qǐng)選擇"> <option value="0">--請(qǐng)選擇班級(jí)--</option> <option value="1">軟件一班</option> <option value="2">軟件二班</option> </select> </div> <span class="el_spans">學(xué)生姓名:</span> <input type="text" id="student.name"/> <div class="input-group el_modellist" role="toolbar"> <span class="el_spans">學(xué)生詳細(xì)信息:</span> <textarea id="studentMsg" class="form-control texta" rows="10" name="student.msg"></textarea> </div> <div class="modal-footer"> <button id="submitButton" onclick="saveButton()" type="button" class="btn btn-primary">更新</button> </div> </form> <script type="text/javascript"> function saveButton(){ //通過(guò)ajax異步將數(shù)據(jù)發(fā)送給action層 $.ajax({ url : '${pageContext.request.contextPath}/stu/stu_upstudent.action',//這里寫(xiě)上你的action路徑 data : $("#handleform").serialize(),//將你在form表單上提交的數(shù)據(jù)序列化 type : 'POST', //提交方式 dataType : 'json', //提交的數(shù)據(jù)類(lèi)型 async:true, //是否異步 success : function(data) {//這是個(gè)回調(diào)函數(shù) data表示從action中傳過(guò)來(lái)的json數(shù)據(jù) //彈出從action層傳過(guò)來(lái)的json格式的數(shù)據(jù)(用來(lái)顯示是否更新成功) alert(data.result); } }); } </script>
action層中的代碼:
@Controller @Scope("prototype") // 控制層,多例模式 public class DangerAction extends ActionSupport { private Student student; public void setStudent(Student student){ this.student = student; } public Student getStudent(){ return this.student; } @Resource private StudentService studentService; public StudentService getStudentService() { return studentService; } public void setStudentService(StudentService studentService) { this.studentService = studentService; } public String updateStudent throws Exception{ boolean flag = studentService.update(student); HttpServletResponse response = ServletActionContext.getResponse(); //通過(guò)json對(duì)象將修改反饋信息響應(yīng)給jsp JSONObject json = new JSONObject(); if (flag) { System.out.println(flag); json.put("result", "修改成功"); } else { System.out.println(flag); json.put("result", "修改失敗"); } System.out.println(json.toString()); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(json.toString()); return null;//如果不需要跳轉(zhuǎn)頁(yè)面就寫(xiě)上null,如果要跳轉(zhuǎn)頁(yè)面就自己另外寫(xiě)上 } }
javabean代碼:
public class Student{ private int stuid; private int className; private int name; private String studentMsg; public int getStuid() { return stuid; } public void setStuid(int stuid) { this.stuid = stuid; } public int getClassName() { return className; } public void setClassName(int className) { this.className = className; } public int getName() { return name; } public void setName(int name) { this.name = name; } public String getStudentMsg() { return studentMsg; } public void setStudentMsg(String studentMsg) { this.studentMsg = studentMsg; } }
以上這篇通過(guò)button將form表單的數(shù)據(jù)提交到action層的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建CSS樣式規(guī)則方案
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建CSS樣式規(guī)則方案,本文包含獲取樣式表、創(chuàng)建樣式表、插入規(guī)則、添加規(guī)則等內(nèi)容,需要的朋友可以參考下2014-09-09基于JS實(shí)現(xiàn)一個(gè)隨機(jī)生成驗(yàn)證碼功能
這篇文章主要介紹了基于JS實(shí)現(xiàn)一個(gè)隨機(jī)生成驗(yàn)證碼功能,隨機(jī)生成一個(gè)四位數(shù)的驗(yàn)證碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05詳解解決小程序中webview頁(yè)面多層history返回問(wèn)題
這篇文章主要介紹了詳解解決小程序中webview頁(yè)面多層history返回問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08微信小程序地圖繪制線段并且測(cè)量(實(shí)例代碼)
這篇文章主要介紹了微信小程序地圖繪制線段并且測(cè)量,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01JavaScript Event學(xué)習(xí)第九章 鼠標(biāo)事件
鼠標(biāo)事件是到目前為止最重要的事件。在這一章我將介紹一些鼠標(biāo)事件的最常見(jiàn)的問(wèn)題和技巧。2010-02-02JavaScript中關(guān)于數(shù)組的調(diào)用方式
這篇文章主要介紹了JavaScript中關(guān)于數(shù)組的調(diào)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02javascript 模擬坦克大戰(zhàn)游戲(html5版)附源碼下載
這篇文章主要介紹了javascript 模擬坦克大戰(zhàn)游戲關(guān)鍵點(diǎn)和遇到的問(wèn)題及實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-04-04