分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

SSM框架以json格式传给前端页面

发布时间:2023-09-06 01:37责任编辑:顾先生关键词:jsjson前端

配置文件和由Mybatis逆向工程生成了相关的代码见  登录实现

关键的是在Controller层的方法上加入@ResponseBody注解实现json格式

在控制台输出的json格式数据

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> ?<head> ???<base href="<%=basePath%>"> ???????<title>My JSP ‘test.jsp‘ starting page</title> ????????<% ???????pageContext.setAttribute("APP_PATH",request.getContextPath()); ????%> ???<script type="text/javascript" src="${APP_PATH}/static/js/jquery-3.2.1.min.js"></script> ???<link rel="stylesheet" type="text/css" href="${APP_PATH}/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> ???<script type="text/javascript" src="${APP_PATH}/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script> ?</head> ???<body> ???<button id="button">获取数据</button> ????</body> ?<script> ?var name=$("#name").val(); ?var password=$("#password").val(); ???????????$("#button").click(function(){ ?????????????$.ajax({ ?????????????????url:"${APP_PATH}/depts", ?????????????????success:function(result){ ?????????????????????console.log(result); ?????????????????????console.log(result.length); ?????????????????????} ??????????????????}); ?????????}); ???????????????????</script></html>

DepartmentController.java

package com.ssm.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.ssm.bean.Department;import com.ssm.service.DepartmentService;@Controllerpublic class DepartmentController { ???????????????@Autowired ???????DepartmentService departmentService; ???????????????@RequestMapping("/depts") ???????@ResponseBody ???????public List<Department> getAllDept(){ ???????????????????????List<Department> list = departmentService.getDepts(); ???????????return list; ???????????????????} ???????}

DepartmentService.java

package com.ssm.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.ssm.bean.Department;import com.ssm.dao.DepartmentMapper;@Servicepublic class DepartmentService { ???????@Autowired ???DepartmentMapper departmentMapper; ???????public List<Department> getDepts() { ???????????????return departmentMapper.selectByExample(null); ???}}

SSM框架以json格式传给前端页面

原文地址:https://www.cnblogs.com/liurg/p/8302811.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved