1:首先创建一个项目如:(说明:此项目是在eclipse创建的)

2.在创建相对应的包如:

3.创建写好相对应的配置文件如:

applicationContext.xml具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
">
<!--自动注入processor解释器(此行不写)-->
<context:annotation-config></context:annotation-config>
<!--自动扫描包-->
<context:component-scan base-package="com.nf"></context:component-scan>

<!--加载JDBC的配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!--几个个性化的信息-->
<!--每300秒检查所有连接池中空闲的连接-->
<property name="idleConnectionTestPeriod" value="300"></property>
<!--最大的空闲时间-->
<property name="maxIdleTime" value="2000"></property>
<!--最大连接数-->
<property name="maxPoolSize" value="5"></property>
</bean>

<!--构造SessionFactory,需要3项内容:1.连接 2.配置 3.实体类映射关系-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--1.数据库连接池-->
<property name="dataSource" ref="myDataSource"></property>
<!--2.相关hibernate的配置信息-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--3.实体类映射关系-->
<property name="packagesToScan" value="com.nf"></property>
</bean>

<!--事务管理器配置,Hibernate单数据源事务-->
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--使用注解annotation定义事务-->
<tx:annotation-driven transaction-manager="defaultTransactionManager" ></tx:annotation-driven>

</beans>

具体内容如下:(这是与MySQL数据库连接的配置)

#database information
driverClass=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/lib?serverTimezone=UTC
user=root
password=

struts.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="myPackage" extends="struts-default,json-default">
<action name="bookAction_*" class="bookAction" method="{1}">
<result type="json" name="jsonOK">
<param name="root">jsonMap</param>
</result>
<allowed-methods>getAllBook</allowed-methods>
</action>

</package>
</struts>

配置web.xml过滤文件

web.xml具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--2个struts的过滤器-->
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--1个spring的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

4.在web目录下创建个js文件夹来存放这俩个js文件(注:这俩个文件是第三方的js文件):

在webContext目录下建个jsp文件,如:test.jsp

(具体内容如下:)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px" id="myView">
<tr>
<th>ID</th>
<th>书名</th>
<th>价格</th>
</tr>
<tr v-for="book in bookList">
<td>{{book.id}}</td>
<td>{{book.name}}</td>
<td>{{book.price}}</td>
</tr>
</table>
</body>
<script src="${pageContext.request.contextPath}/js/vue.js"></script>
<script src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var myModel = {bookList:[]};
var myViewModel = new Vue({
el:"#myView" ,
data:myModel

}) ;
/*
var myViewModel = new Vue({
el:'#myView',
data:myModel
});
*/
//写成函数的目的,为了【复用】
function getData(){
$.ajax({
url:"bookAction_getAllBook", //后端的API地址
type:'GET', //http:POST/GET
//data:postData, //指客户端提交给后台的参数
dataType:'json', //服务端返回类型text,json
timeout:3000,
success:function(result){
//alert(result);
//$.extend(true, result, myModel);
//失败
//myViewModel.data = result;
//失败
//myModel = result;
myModel.bookList = result.bookList ;

},
error:function(){
alert('服务器忙,请不要说脏话,理论上大家都是文明人');
}
});
}
getData();

</script>
</html>

6.开始在src目录下写后台代码了

先从开始:

创建Book.java文件

具体内容如下:

package com.nf.entity;

import javax.persistence.*;

@Entity
@Table(name = "book")
public class Book {
private Integer id;
private String name;
private Integer price;

@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}

@Column(name = "name",length = 50,nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Column(name = "price",nullable = false)
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}

}

然后在创建dao模层,

先创建个BookDao接口,具体内容如下:

package com.nf.dao;

import com.nf.entity.Book;

import java.util.List;

public interface BookDao {

public List<Book> getAllBook();

}

在创建个BookDaoImpl类并实现BookDao接口,内容如下:

package com.nf.dao;

import com.nf.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Scope("prototype")
public class BookDaoImpl implements BookDao {

@Autowired
private SessionFactory sessionFactory;

public List<Book> getAllBook() {
Session session = sessionFactory.getCurrentSession();
Query<Book> query = session.createQuery("from Book", Book.class);
List<Book> bookList = query.getResultList();

return bookList;
}
}

最后写模块

(具体内容如下:)

package com.nf.action;

import com.nf.entity.Book;
import com.nf.service.BookService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class BookAction extends ActionSupport {

//姝sonMap鐢ㄤ簬瀛樺偍JSON鏍煎紡鐨勬暟鎹�
private Map<String,Object> jsonMap = new HashMap();
public Map<String, Object> getJsonMap() {
return jsonMap;
}
public void setJsonMap(Map<String, Object> jsonMap) {
this.jsonMap = jsonMap;
}

@Autowired
private BookService bookService;

public String getAllBook(){
List<Book> bookList = bookService.getAllBook();
jsonMap.put("bookList", bookList);
return "jsonOK";
}
}

好了,到这里整个案例已经完成了,一来可以作为我以后复习的笔记,二来希望可以帮助到在这方面学习的朋友,做的不够精细,请多多包含,勿喷!!

 
 
 
好文要顶

笔记(json)实现前后端交互案例的更多相关文章

  1. ajax学习----json,前后端交互,ajax

    json <script> var obj = {"name": "xiaopo","age": 18,"gender ...

  2. SpringMvc采用 http+json 实现前后端交互

    演示列表 报文表示 一.Json请求和Json响应 实现:Spring4.1.1.RELEASE + jackson2.4.4+JQuery1.10.2 1.pom.xml <propertie ...

  3. JSON(及其在ajax前后端交互的过程)小识

    一. json介绍 json是一种轻量级的数据交换格式,规则很简单: 并列的数据之间用逗号(,)分隔: 映射用冒号(:)表示: 并列数据的集合(数组)用方括号([])表示: 映射的集合(对象)用大括号 ...

  4. 前后端交互实现(nginx,json,以及datatable的问题相关)

    1.同源问题解决 首先,在同一个域下搭建网络域名访问,需要nginx软件,下载之后修改部分配置 然后再终端下cmd  nginx.exe命令,或者打开nginx.exe文件,会运行nginx一闪而过, ...

  5. Node之简单的前后端交互

    node是前端必学的一门技能,我们都知道node是用的js做后端,在学习node之前我们有必要明白node是如何实现前后端交互的. 这里写了一个简单的通过原生ajax与node实现的一个交互,刚刚学n ...

  6. Django之META与前后端交互

    Django之META与前后端交互 1 提交表单之GET 前端提交数据与发送 1)提交表单数据 2)提交JSON数据 后端的数据接收与响应 1)接收GET请求数据 2)接收POST请求数据 3)响应请 ...

  7. 百度ueditor的图片上传,前后端交互使用

    百度ueditor的使用 一个文本编辑器,看了网上很多文档写的很乱,这里拾人牙慧,整理下怎么使用. 这个东西如果不涉及到图片附件上传,其实很简单,就是几个前端文件,直接引用,然后配置下ueditor. ...

  8. 【开源.NET】 轻量级内容管理框架Grissom.CMS(第二篇前后端交互数据结构分析)

    这是 CMS 框架系列文章的第二篇,第一篇开源了该框架的代码和简要介绍了框架的目的.作用和思想,这篇主要解析如何把sql 转成标准 xml 配置文件和把前端post的增删改数据规范成方便后台解析的结构 ...

  9. thinkphp+jquery+ajax前后端交互注册验证

    thinkphp+jquery+ajax前后端交互注册验证,界面如下 register.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...

随机推荐

  1. Traefik实现Kubernetes集群服务外部https访问

    转载请注明出处:http://www.cnblogs.com/wayneiscoming/p/7707942.html traefik 是一个前端http反向代理服务器以及负载均衡器,支持多种微服务后 ...

  2. 用node.js实现ORM的一种思路

    ORM是O和R的映射.O代表面向对象,R代表关系型数据库.二者有相似之处同时也各有特色.就是因为这种即是又非的情况,才需要做映射的. 理想情况是,根据关系型数据库(含业务需求)的特点来设计数据库.同时 ...

  3. 容器与Docker简介(四)Docker容器,镜像与 Registries——微软微服务电子书翻译系列

    当使用Docker时,开发人员创建一个应用程序或服务,并将其和其依赖关系打包到容器镜像中. 镜像是应用程序或服务及其配置和依赖的静态表示形式. 要运行应用程序或服务,应用程序的镜像将被实例化以创建一个 ...

  4. 双向链表--Java实现

    /*双向链表特点: *1.每个节点含有两个引用,previos和next,支持向前或向后的遍历(除头节点) *2.缺点插入或删除的时候涉及到引用修改的比较多 *注意:下面的双向链表其实也实现了双端链表 ...

  5. LAMP 实现全过程及wordpress的搭建

    一.介绍 1. LAM(M)P: L:linux A:apache (httpd) M:mysql, mariadb M:memcached 缓存 P:php, perl, python WEB 资源 ...

  6. 一个fork短码的扩展版本

    原本代码: 链接 int skip = !!fork() + 2*(!!fork()); for (uint32_t i=skip;i!=INT_MAX;i+=4) { } 这个是多进程加速循环的代码 ...

  7. bug:论用例健壮性的重要

    最近出了2个类似问题,此处写下,以作为警醒 问题1: 背景:电商类网站,为了增加用户回流,增加用户购买力度,做了一个和用户等级相关活动 需求:用户等级为g0 -g5,现在有一批代金券有等级领取限制.用 ...

  8. 搭建yeoman自动化构建工具

    yeoman可以快速的搭建一个项目的手脚架,初次接触yeoman,在搭建的过程中遇到了很多的问题. yeoman需要node.js(http://nodejs.org)和git(http://git- ...

  9. HTML超连接的使用

    1,基本语法:<a href="" target="打开方式" name ="页面锚点名称">连接文字或者图片</a> ...

  10. 利用win10自带的系统配置禁止开机启动项和程序

    一.利用win10自带的系统配置禁止开机启动项和程序     首先打开"运行"对话框,可以通过开始菜单打开运行,也可以按下快捷键WIN+R打开"运行".如下图. ...