通過button將form表單的數(shù)據(jù)提交到action層的實例
form表單中不需要寫action的路徑,需要給form表單一個唯一的id,將你要提交的信息的表單中的標簽name="action中的javabean對象.javabean屬性"。給button按鈕添加一個onclick()點擊事件,并實現(xiàn)該點擊事件,在該onclick()方法中通過ajax將form表單中的數(shù)據(jù)提交給action層
JSP頁面中的代碼:
<form id="handleform">
<!-- 根據(jù)學(xué)生id修改學(xué)生信息 -->
<input type="hidden" name="student.stuid"/><!-- 隱藏學(xué)生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">學(xué)生姓名:</span>
<input type="text" id="student.name"/>
<div class="input-group el_modellist" role="toolbar">
<span class="el_spans">學(xué)生詳細信息:</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異步將數(shù)據(jù)發(fā)送給action層
$.ajax({
url : '${pageContext.request.contextPath}/stu/stu_upstudent.action',//這里寫上你的action路徑
data : $("#handleform").serialize(),//將你在form表單上提交的數(shù)據(jù)序列化
type : 'POST', //提交方式
dataType : 'json', //提交的數(shù)據(jù)類型
async:true, //是否異步
success : function(data) {//這是個回調(diào)函數(shù) data表示從action中傳過來的json數(shù)據(jù)
//彈出從action層傳過來的json格式的數(shù)據(jù)(用來顯示是否更新成功)
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對象將修改反饋信息響應(yīng)給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;//如果不需要跳轉(zhuǎn)頁面就寫上null,如果要跳轉(zhuǎn)頁面就自己另外寫上
}
}
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表單的數(shù)據(jù)提交到action層的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實現(xiàn)動態(tài)創(chuàng)建CSS樣式規(guī)則方案
這篇文章主要介紹了JavaScript實現(xiàn)動態(tài)創(chuàng)建CSS樣式規(guī)則方案,本文包含獲取樣式表、創(chuàng)建樣式表、插入規(guī)則、添加規(guī)則等內(nèi)容,需要的朋友可以參考下2014-09-09
詳解解決小程序中webview頁面多層history返回問題
這篇文章主要介紹了詳解解決小程序中webview頁面多層history返回問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-08-08
JavaScript Event學(xué)習第九章 鼠標事件
鼠標事件是到目前為止最重要的事件。在這一章我將介紹一些鼠標事件的最常見的問題和技巧。2010-02-02
JavaScript中關(guān)于數(shù)組的調(diào)用方式
這篇文章主要介紹了JavaScript中關(guān)于數(shù)組的調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
javascript 模擬坦克大戰(zhàn)游戲(html5版)附源碼下載
這篇文章主要介紹了javascript 模擬坦克大戰(zhàn)游戲關(guān)鍵點和遇到的問題及實現(xiàn)代碼,需要的朋友可以參考下2014-04-04

