ajax动态加载公共模块,Maven多模块项目搭建+SSM框架整合(四、Ajax异步获取数据,jq动态添加)...
最近有點小忙,但是還是在晚上抽出來點時間更新文章,希望對初學者有幫助(都是從那時候過來的,哈哈)一起努力。
開始正題~~~~
封裝類ResultVo
在與前臺頁面交互的過程中我們一般會用到一個封裝類,來傳遞數據,我在這里寫了一個,見圖(通過本文你就會知道它的用處了)。
package com.songci.mytest_one.model.utils;
/**
* Created by songl on 2017/8/10.
*/
public class ResultVo {
private boolean success = false;
private String message = null;
private T result = null;
public void isSuccess(boolean b) {
this.success=b;
}
public void setMessage(String message) {
this.message = message;
}
public void setResult(T result) {
this.result = result;
}
public boolean getSuccess(){
return success;
}
public String getMessage() {
return message;
}
public T getResult() {
return result;
}
}
修改StudentService接口(返回結果用上ResultVo)
StudentService
package com.songci.mytest_one.service;
import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.model.utils.ResultVo;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
public interface StudentService {
/**
* 添加學生
* @param student
* @return
*/
Boolean addStudent(Student student);
/**
* 根據ID刪除學生
* @param id
* @return
*/
Boolean deleteStudentById(Integer id);
/**
* 根據ID修改學生信息
* @param student
* @return
*/
Boolean updateStudentById(Student student);
/**
* 按條件查找所有學生
* @param student
* @return
*/
//修改之處
ResultVo findAllStudent(Student student);
}
StudentServiceImpl
package com.songci.mytest_one.service.impl;
import com.songci.mytest_one.dao.StudentDao;
import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.model.utils.ResultVo;
import com.songci.mytest_one.service.StudentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by songl on 2017/8/8.
*/
@Service("StudentService")
public class StudentServiceImpl implements StudentService{
@Resource
private StudentDao studentDao;
public Boolean addStudent(Student student) {
return studentDao.insert(student);
}
public Boolean deleteStudentById(Integer id) {
Student student=new Student();
student.setId(id);
return studentDao.delete(student);
}
public Boolean updateStudentById(Student student) {
return studentDao.update(student);
}
//修改之處start
public ResultVo findAllStudent(Student student) {
ResultVo resultVo=new ResultVo();
List list= studentDao.select(student);
if (list.size()>0){
resultVo.setResult(list);
resultVo.isSuccess(true);
}else {resultVo.setMessage("沒有找到相關信息");}
return resultVo;
}
//修改之處end
}
完成StudentController
package com.songci.mytest_one.controller;
import com.songci.mytest_one.model.Student;
import com.songci.mytest_one.model.utils.ResultVo;
import com.songci.mytest_one.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* Created by songl on 2017/8/10.
*/
@Controller
@RequestMapping("/studentApi/")
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("findAllStudentInfo")
public @ResponseBody //添加@ResponseBody直接返回json數據
ResultVo findAllStudentInfo (@RequestParam("id") String id){
Student student=new Student();
//在此我就不做過多驗證判斷
if ("0".equals(id)){student=null;}
else {student.setId(new Integer(id));}
return studentService.findAllStudent(student);
}
}
web.xml引用配置文件
/p>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
contextConfigLocation
classpath:config/applicationContext.xml
log4jConfigLocation
classpath:config/log4j.properties
log4jRefreshInterval
6000
SpringEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
SpringEncodingFilter
/*
org.springframework.web.util.Log4jConfigListener
org.springframework.web.context.ContextLoaderListener
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
2
spring
*.do
直接就在歡迎頁index.jsp添加代碼(悄悄告訴你,里面用到了Ajax與jquery,滿滿知識點)
jquery.js已上傳GitHub
開發中......Hello World!
用異步請求獲取學生信息
請輸入學生學號,如果是0則查詢所有學生信息
點我查詢
function findStudentInfo() {
var studentid=$("#studentid").val();
// alert("獲取到的studentid:" + studentid);
$.ajax({
type:"POST",
url:"/studentApi/findAllStudentInfo.do",
data:{id:studentid},
dataType:"json",
success : function (data) {
if(data.success){
$("#showMessageDiv").empty();
$("#showMessageDiv").append("
");$("#table1").append("
學生ID姓名性別地址");$.each(data.result,function (i,result) {
var sex="女"
if (result.sex){sex="男"}
var item="
"+result.id+""+result.name+""+sex+""+result.address+"";$("#table1").append(item);
});
}else {
$("#showMessageDiv").empty();
$("#showMessageDiv").append(data.message);
}
}
});
}
測試結果
有什么問題可以在下方留言,我們一起討論,一起進步。
將持續更新 ~~~
未完待續~~~
如果感覺文章不錯記得點贊哦,謝謝支持。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的ajax动态加载公共模块,Maven多模块项目搭建+SSM框架整合(四、Ajax异步获取数据,jq动态添加)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js写css3过渡前端,web前端vue
- 下一篇: web页面到ajax,页面使用ajax加