最近有点小忙,但是还是在晚上抽出来点时间更新文章,希望对初学者有帮助(都是从那时候过来的,哈哈)一起努力。

开始正题~~~~

封装类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);

}

}

});

}

测试结果

有什么问题可以在下方留言,我们一起讨论,一起进步。

将持续更新 ~~~

未完待续~~~

如果感觉文章不错记得点赞哦,谢谢支持。

ajax动态加载公共模块,Maven多模块项目搭建+SSM框架整合(四、Ajax异步获取数据,jq动态添加)...相关推荐

  1. MyEclipse使用Maven创建web项目+搭建SSM框架教程

    MyEclipse使用Maven创建web项目+搭建SSM框架教程 博文中的代码:链接:http://pan.baidu.com/s/1o8tqIMI 密码:m3si 安装maven和配置 1.下载m ...

  2. MyEclipse使用Maven创建web项目+搭建SSM框架教

     使用maven已经有一段时间了,但项目是别人搭建好的,因此一直想着自己要学习搭建一下.网上找了些资料后,结合自己实验,花了点时间就搞好,老样子,写在博客上,免得日后忘记. 博文中的代码:链接:h ...

  3. ajax异步获取数据后动态向表格中添加数据的页面

    因为某些原因,项目中突然需要做自己做个ajax异步获取数据后动态向表格中添加数据的页面,网上找了半天都没有 看到现成的,决定自己写个例子 1.HTML页面 [html] view plaincopyp ...

  4. ajax异步获取数据后动态向表格中添加数据(行)

    因为某些原因,项目中突然需要做自己做个ajax异步获取数据后动态向表格中添加数据的页面,网上找了半天都没有 看到现成的,决定自己写个例子 1.HTML页面 [html] view plaincopyp ...

  5. 动态加载子节点_微信小游戏开发之场景切换和常驻节点传递数据

    主题 场景切换 场景间数据传递方式 小游戏全局背景音效 特别说明 CocosCreator微信小游戏开发系列文章,是我在逐步开发过程中,基于官方文档之上,记录一些重点内容,以及对官方文档中有些知识点的 ...

  6. android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...

    Android-Skin-Loader 更新日志 导入到Android Studio,使用gradle构建皮肤包(见7. 皮肤包是什么?如何生成?)(2015-12-02) 解决Fragment换肤在 ...

  7. vue dplayer 加载失败_最新vue脚手架项目搭建,并解决一些折腾人的问题

    话不多说,跟好lz的操作!!! 2020/8/1 第一步: ~~~~质问三连: ~~~~~~~~1.node.js安装了吗?:ht tp://nodejs.cn/download/ ~~~~~~~~2 ...

  8. OSGI动态加载删除Service bundle

    OSGi模块化框架是很早就出来的一个插件化框架,最早Eclipse用它而出名,但这些年也没有大热虽然OSGi已经发布了版本1到版本5.现在用的最多的,也是本文讲述基于的是Equinox的OSGi实现, ...

  9. php动态加载js,动态加载script文件的两种方法_javascript技巧

    动态加载script到页面大约有俩方法 第一种就是利用ajax方式,把script文件代码从后台加载到前台,然后对加载到的内容通过eval()执行代码.第二种是,动态创建一个script标签,设置其s ...

最新文章

  1. 开源中国git关联xcode操作步骤
  2. vbsedit无法创建空文档_vue文档里你没捡起来的宝藏
  3. python legb_理解 Python 的 LEGB.
  4. dataframe填充到指定的行数
  5. 全网最全Spring面试题之基础篇整理总结(共69题,附超详细解答)
  6. html5声音播放音乐,HTML5 煽情的音乐播放器和音频可视化
  7. 高速串行总线走线难点在哪?重要线信号的处理经验分享
  8. 小米手机各种检测代码
  9. 抗滑桩初始弹性系数计算_抗滑桩设计验算1
  10. 图像匹配论文总结(一)
  11. HP如何装linux系统启动顺序,linux几种系统的启动顺序
  12. Django教程 —— 站点后台管理
  13. 男人养肾按摩运动更可取
  14. php scws自定义词库,scws分词 自定义词库的方法
  15. Android 系统(213)---如何内置多张静态壁纸(图片)到系统中
  16. 归并排序 (递归 非递归)
  17. c# Npoi导出Excel并合并行列
  18. 百度网盘PC端缓存文件夹
  19. 四狂神战记2部分攻略
  20. Android 编程好书推荐

热门文章

  1. 设置 Confluence 6 日志
  2. 设计模式(结构型模式)——桥接模式(Bridge)
  3. JavaWeb--数据库添加
  4. 在Exchange 2013 OWA登录页面中修改密码
  5. 来自damon的zencart二次开发教程-3.2复制模板(仿站)操作教程
  6. UITableView实现划动删除
  7. oracle网络公开课《存储技术》课件和视频共享下载
  8. 财务报销人员是公司亲信的弊端
  9. 【Python学习系列二十一】pandas库基本操作
  10. Leetcode 106. 从中序与后序遍历序列构造二叉树 解题思路及C++实现