最近有点小忙,但是还是在晚上抽出来点时间更新文章,希望对初学者有帮助(都是从那时候过来的,哈哈)一起努力。
开始正题~~~~
封装类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);
}
}
});
}
测试结果
有什么问题可以在下方留言,我们一起讨论,一起进步。
将持续更新 ~~~
未完待续~~~
如果感觉文章不错记得点赞哦,谢谢支持。