通過button將form表單的數據提交到action層


 

form表單中不需要寫action的路徑,需要給form表單一個唯一的id,將你要提交的信息的表單中的標簽name="action中的javabean對象.javabean屬性"。給button按鈕添加一個onclick()點擊事件,並實現該點擊事件,在該onclick()方法中通過ajax將form表單中的數據提交給action層

 

JSP頁面中的代碼:
1
<form id="handleform"> 2 <!-- 根據學生id修改學生信息 --> 3 <input type="hidden" name="student.stuid"/><!-- 隱藏學生id --> 4 <div class="input-group el_modellist" role="toolbar"> 5 <span class="el_spans">要修改的班級:</span> 6 <select class="selectpicker form-control" name="student.className" id="fmchechunit" title="請選擇"> 7 <option value="0">--請選擇班級--</option> 8 <option value="1">軟件一班</option> 9 <option value="2">軟件二班</option> 10 </select> 11 </div> 12 <span class="el_spans">學生姓名:</span> 13 <input type="text" id="student.name"/> 14 <div class="input-group el_modellist" role="toolbar"> 15 <span class="el_spans">學生詳細信息:</span> 16 <textarea id="studentMsg" class="form-control texta" rows="10" name="student.msg"></textarea> 17 </div> 18 19 <div class="modal-footer"> 20 <button id="submitButton" onclick="saveButton()" type="button" class="btn btn-primary">更新</button> 21 </div> 22 </form> 23 <script type="text/javascript"> 24 function saveButton(){ 25 //通過ajax異步將數據發送給action層 26 $.ajax({ 27 url : '${pageContext.request.contextPath}/stu/stu_upstudent.action',//這里寫上你的action路徑 28 data : $("#handleform").serialize(),//將你在form表單上提交的數據序列化 29 type : 'POST', //提交方式 30 dataType : 'json', //提交的數據類型 31 async:true, //是否異步 32 success : function(data) {//這是個回調函數 data表示從action中傳過來的json數據 33 //彈出從action層傳過來的json格式的數據(用來顯示是否更新成功) 34 alert(data.result); 35 } 36 }); 37 } 38 </script>

 

action層中的代碼:
1
@Controller 2 @Scope("prototype") 3 // 控制層,多例模式 4 public class DangerAction extends ActionSupport { 5 6 private Student student; 7 public void setStudent(Student student){ 8 this.student = student; 9 } 10 public Student getStudent(){ 11 return this.student; 12 } 13 14 @Resource 15 private StudentService studentService; 16 public StudentService getStudentService() { 17 return studentService; 18 } 19 public void setStudentService(StudentService studentService) { 20 this.studentService = studentService; 21 } 22 public String updateStudent throws Exception{ 23 24 boolean flag = studentService.update(student); 25 HttpServletResponse response = ServletActionContext.getResponse(); 26 27      //通過json對象將修改反饋信息響應給jsp 28 JSONObject json = new JSONObject(); 29 if (flag) { 30 System.out.println(flag); 31 json.put("result", "修改成功"); 32 } else { 33 System.out.println(flag); 34 json.put("result", "修改失敗"); 35 } 36 System.out.println(json.toString()); 37 response.setContentType("text/html;charset=UTF-8"); 38 response.getWriter().write(json.toString()); 39 return null;//如果不需要跳轉頁面就寫上null,如果要跳轉頁面就自己另外寫上 40 } 41 }

 

 

javabean代碼: 
1
public class Student{ 2 private int stuid; 3 private int className; 4 private int name; 5 private String studentMsg; 6 public int getStuid() { 7 return stuid; 8 } 9 public void setStuid(int stuid) { 10 this.stuid = stuid; 11 } 12 public int getClassName() { 13 return className; 14 } 15 public void setClassName(int className) { 16 this.className = className; 17 } 18 public int getName() { 19 return name; 20 } 21 public void setName(int name) { 22 this.name = name; 23 } 24 public String getStudentMsg() { 25 return studentMsg; 26 } 27 public void setStudentMsg(String studentMsg) { 28 this.studentMsg = studentMsg; 29 } 30 31 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM