下面小編就為大家帶來一篇透過button將form表單的資料提交給action層的實例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
form表單中不需要寫action的路徑,需要給form表單一個唯一的id,將你要提交的資訊的表單中的標籤name="action中的javabean物件.javabean屬性"。為button按鈕新增一個onclick()點擊事件,並實作該點擊事件,在該onclick()方法中透過ajax將form表單中的資料提交給action層
##JSP頁面中的程式碼:
<form id="handleform"> <!-- 根据学生id修改学生信息 --> <input type="hidden" name="student.stuid"/><!-- 隐藏学生id --> <p class="input-group el_modellist" role="toolbar"> <span class="el_spans">要修改的班级:</span> <select class="selectpicker form-control" name="student.className" id="fmchechunit" title="请选择"> <option value="0">--请选择班级--</option> <option value="1">软件一班</option> <option value="2">软件二班</option> </select> </p> <span class="el_spans">学生姓名:</span> <input type="text" id="student.name"/> <p class="input-group el_modellist" role="toolbar"> <span class="el_spans">学生详细信息:</span> <textarea id="studentMsg" class="form-control texta" rows="10" name="student.msg"></textarea> </p> <p class="modal-footer"> <button id="submitButton" onclick="saveButton()" type="button" class="btn btn-primary">更新</button> </p> </form> <script type="text/javascript"> function saveButton(){ //通过ajax异步将数据发送给action层 $.ajax({ url : '${pageContext.request.contextPath}/stu/stu_upstudent.action',//这里写上你的action路径 data : $("#handleform").serialize(),//将你在form表单上提交的数据序列化 type : 'POST', //提交方式 dataType : 'json', //提交的数据类型 async:true, //是否异步 success : function(data) {//这是个回调函数 data表示从action中传过来的json数据 //弹出从action层传过来的json格式的数据(用来显示是否更新成功) 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(); //通过json对象将修改反馈信息响应给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;//如果不需要跳转页面就写上null,如果要跳转页面就自己另外写上 } }
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; } }
以上是透過button將form表單的資料提交到action層的實例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!