国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:27:03
字體:
來源:轉載
供稿:網友

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 -->    <div 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>    </div>    <span class="el_spans">學生姓名:</span>    <input type="text" id="student.name"/>     <div 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>     </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(){      //通過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,如果要跳轉頁面就自己另外寫上 }}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 和林格尔县| 池州市| 永吉县| 凌海市| 涟源市| 祁门县| 珲春市| 涡阳县| 随州市| 商河县| 镇原县| 邛崃市| 元朗区| 东安县| 瑞金市| 湛江市| 保山市| 黄陵县| 蒙阴县| 句容市| 博湖县| 定南县| 波密县| 察雅县| 临沂市| 桂林市| 陵川县| 大安市| 沁水县| 西峡县| 乐都县| 盐城市| 荔波县| 馆陶县| 宝清县| 泾阳县| 津南区| 新绛县| 大邑县| 沾益县| 道孚县|