通过button将form表单的数据提交到action层

 

form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性"。给button按钮添加一个onclick()点击事件,并实现该点击事件,在该onclick()方法中通过ajax将form表单中的数据提交给action层

 

JSP页面中的代码:
1
2 3 4
class="input-group el_modellist" role="toolbar"> 5 class="el_spans">要修改的班级: 6 11
12 class="el_spans">学生姓名: 13 14
class="input-group el_modellist" role="toolbar"> 15 class="el_spans">学生详细信息: 16 17
18 19
class="modal-footer"> 20 21
22
23

 

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 }

 

转载于:https://www.cnblogs.com/lixianyuan-org/p/7492156.html

你可能感兴趣的:(通过button将form表单的数据提交到action层)