Use the button to submit the form data to the action layer, buttonaction

Source: Internet
Author: User

Use the button to submit the form data to the action layer, buttonaction

 

You do not need to write the action path in the form. You need to give the form a unique id to add the javabean object in the form with the tag name = "action" of the information you want to submit. javabean attribute ". Add an onclick () event to the button and implement this event. In this onclick () method, submit the data in the form to the action layer through ajax.

 

Code on the JSP page:
1 <form id = "handleform"> 2 <! -- Modify student information based on student id --> 3 <input type = "hidden" name = "student. stuid"/> <! -- Hide student id --> 4 <div class = "input-group el_modellist" role = "toolbar"> 5 <span class = "el_spans"> class to be modified: </span> 6 <select class = "selectpicker form-control" name = "student. className "id =" fmchechunit "title =" select "> 7 <option value =" 0 "> -- select a class -- </option> 8 <option value =" 1"> Software class 1 </option> 9 <option value = "2"> Software class 2 </option> 10 </select> 11 </div> 12 <span class = "el_spans "> Student name: </span> 13 <input type = "text" id = "student. name "/> 14 <div class =" input-group el_modellist "role =" toolbar "> 15 <span class =" el_spans "> Student details: </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-primary "> Update </button> 21 </div> 22 </form> 23 <script type =" text/javascript"> 24 function saveButton () {25 // send data asynchronously to action layer 26 $. ajax ({27 url: '$ {pageContext. request. contextPath}/stu/stu_upstudent.action ', // enter your action path 28 data: $ ("# handleform "). serialize (), // serialize the data submitted on the form 29 type: 'post', // submit Method 30 dataType: 'json ', // submitted data type 31 async: true, // asynchronous 32 success: function (data) {// This is a callback function. data indicates the json data transmitted from the action. 33 // The json data transmitted from the action layer is displayed (to show whether the update is successful) 34 alert (data. result); 35} 36}); 37} 38 </script>

 

Code in the action layer:
1 @ Controller 2 @ Scope ("prototype") 3 // control layer, multi-sample mode 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 @ Resource15 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 // respond to jsp28 JSONObject json = new JSONObject (); 29 if (flag) {30 System. out. println (flag); 31 json. put ("result", "modified successfully"); 32} else {33 System. out. println (flag); 34 json. put ("result", "modification failed"); 35} 36 System. out. println (json. toString (); 37 response. setContentType ("text/html; charset = UTF-8"); 38 response. getWriter (). write (json. toString (); 39 return null; // if you do not need to jump to the page, write null. If you want to jump to the page, write another 40} 41}

 

 

Javabean code: 
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}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.