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

首頁 > 開發 > JS > 正文

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

2024-05-06 16:39:35
字體:
來源:轉載
供稿:網友

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"      <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,如果要跳轉頁面就自己另外寫上 }}

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層的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 晋中市| 兴义市| 山丹县| 巴彦淖尔市| 阳东县| 高陵县| 东辽县| 马公市| 连山| 望城县| 冀州市| 博乐市| 武穴市| 东宁县| 略阳县| 崇阳县| 汕头市| 兴义市| 漯河市| 慈利县| 渭南市| 南华县| 凤庆县| 东乌珠穆沁旗| 阿拉善右旗| 万全县| 景谷| 南投县| 淄博市| 鄂伦春自治旗| 汉中市| 巴马| 芦山县| 增城市| 临漳县| 大竹县| 南雄市| 抚州市| 鞍山市| 措勤县| 浦城县|