通过button将form表单的数据提交到action层的实例
form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性"。给button按钮添加一个onclick()点击事件,并实现该点击事件,在该onclick()方法中通过ajax将form表单中的数据提交给action层
JSP页面中的代码:
要修改的班级: --请选择班级-- 软件一班 软件二班
form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性"。给button按钮添加一个onclick()点击事件,并实现该点击事件,在该onclick()方法中通过ajax将form表单中的数据提交给action层
JSP页面中的代码:
要修改的班级: --请选择班级-- 软件一班 软件二班
action层中的代码:
@Controller
@Scope("prototype")
//控制层,多例模式
publicclassDangerActionextendsActionSupport{
privateStudentstudent;
publicvoidsetStudent(Studentstudent){
this.student=student;
}
publicStudentgetStudent(){
returnthis.student;
}
@Resource
privateStudentServicestudentService;
publicStudentServicegetStudentService(){
returnstudentService;
}
publicvoidsetStudentService(StudentServicestudentService){
this.studentService=studentService;
}
publicStringupdateStudentthrowsException{
booleanflag=studentService.update(student);
HttpServletResponseresponse=ServletActionContext.getResponse();
//通过json对象将修改反馈信息响应给jsp
JSONObjectjson=newJSONObject();
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());
returnnull;//如果不需要跳转页面就写上null,如果要跳转页面就自己另外写上
}
}
javabean代码:
publicclassStudent{
privateintstuid;
privateintclassName;
privateintname;
privateStringstudentMsg;
publicintgetStuid(){
returnstuid;
}
publicvoidsetStuid(intstuid){
this.stuid=stuid;
}
publicintgetClassName(){
returnclassName;
}
publicvoidsetClassName(intclassName){
this.className=className;
}
publicintgetName(){
returnname;
}
publicvoidsetName(intname){
this.name=name;
}
publicStringgetStudentMsg(){
returnstudentMsg;
}
publicvoidsetStudentMsg(StringstudentMsg){
this.studentMsg=studentMsg;
}
}
以上这篇通过button将form表单的数据提交到action层的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。