当前位置: 首页 > news >正文

Spring基础创建

1.下载安装idea

社区版和专业版是可以共存的

社区版

2.安装插件

3.安装插件之后,重启idea


Spring 两个含义:一个是Spring 家族,全家桶

                           另一个指Spring Core,也就是Spring Framewor

包下载不下来

1.网络问题

2.确认是否设置了国内资源

3.确认包是否存在

4.查看本地仓库,删除下载了一半的jar包

5.多次刷新,或者尝试换网络

6.不使用国内资源

4.Spring MVC

Spring Web  MVC

1.建立连接

2.传递参数

3.结果返回


1.建立连接

@RequestMapping 路由映射

@RequestParam  参数绑定(后端参数重命名,传递集合等)

@RequestBody  传递JSON

@PathVariable 获取URL中的参数

@GetMapping

@PostMapping

@RequestPart    上传文件

5.获取Cookie

 @RequestMapping("/getCookie")public String getCookie(HttpServletRequest request) {
//        String userName = request.getParameter("userName");//String age = request.getParameter("age");//Integer age1 = Integer.parseInt(age);Cookie[] cookies = request.getCookies();System.out.println("获取Cookie信息:");if (cookies != null) {for (Cookie cookie : cookies) {System.out.println(cookie.getName() + ":" + cookie.getValue());}}return "获取Cookie成功";}

6.设置Session

7.获取Header

8.返回页面

@Controller 默认返回视图,表示这个对象交给了Spring管理

@RestController = @ResponseBody + @Controller

1.需求分析

2.接口定义

   接口名称,URL,传递参数,返回的数据

表现形式:接口文档

3.实现接口(开发)

接口定义:

1.站在前端的角度:前端需要  计算的结果

2.站在后端的角度: 后端需要  输入数据

接口说明:根据输入的信息,返回数字相加的结果

接口URL :   /cacl/sum

参数: num1(Integer)    num2(Integer)

返回: 两数之和(num1 + num2)


前后端交互时遇到的问题:

1.有日志看日志

2.确认后端接口是否有问题(postman或者浏览器等方式都可以)

3.先保证后端接口没问题

4.前端请求时,Fiddler抓包或者后端Debug,观察参数/url等是否有问题

(url看请求是否进入到后端)

9.留言板


页面没有反应时:

1.按F12,看前端是否报错

2.看请求是否到达后端

3.后端正常响应,看前端接收到数据后,是否未进行任何处理

经常遇到的问题:

1.参数不对

2.缓存问题

3.代码问题

4.JDK

5.路径相关

留言板:

MessageController

MessageInfo

messagewall.html

MessageController

package com.bite.springmvc;import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;@RequestMapping("/message")
@RestController
public class MessageController {private List<MessageInfo> messageInfoList = new ArrayList<>();@RequestMapping(value = "/publish",method = RequestMethod.POST)public Boolean publish(@RequestBody MessageInfo messageInfo) {for (MessageInfo messageInfo1 : messageInfoList) {}messageInfoList.add(messageInfo);return true;}@RequestMapping("/getList")public List<MessageInfo> getList() {return messageInfoList;}}

MessageInfo

package com.bite.springmvc;
public class MessageInfo {private Integer id;private String from;private String to;private String message;private Integer deleteFlag;}

messagewall.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>留言板</title><style>.container {width: 350px;height: 300px;margin: 0 auto;/* border: 1px black solid; */text-align: center;}.grey {color: grey;}.container .row {width: 350px;height: 40px;display: flex;justify-content: space-between;align-items: center;}.container .row input {width: 260px;height: 30px;}#submit {width: 350px;height: 40px;background-color: orange;color: white;border: none;margin: 10px;border-radius: 5px;font-size: 20px;}</style>
</head><body>
<div class="container"><h1>留言板</h1><p class="grey">输入后点击提交, 会将信息显示下方空白处</p><div class="row"><span>谁:</span> <input type="text" name="" id="from"></div><div class="row"><span>对谁:</span> <input type="text" name="" id="to"></div><div class="row"><span>说什么:</span> <input type="text" name="" id="say"></div><input type="button" value="提交" id="submit" onclick="submitBtn()"><!-- <div>A 对 B 说: hello</div> -->
</div>
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script><script>$.ajax({type: "get",url: "/message/getList",success: function (messages) {if (messages != null) {for (var message of messages) {var divE = "<div>" + message.from + "对" + message.to + "说:" + message.message + "</div>";$(".container").append(divE);}}}});function submitBtn() {//1. 获取留言的内容var from = $('#from').val();var to = $('#to').val();var say = $('#say').val();if (from == '' || to == '' || say == '') {return;}//调用后端接口, 发表留言$.ajax({type: "post",url: "/message/publish",contentType: "application/json",data: JSON.stringify({"from": from,"to": to,"message": say}),success: function (result) {if (result) {//成功//2. 构造节点var divE = "<div>" + from + "对" + to + "说:" + say + "</div>";//3. 把节点添加到页面上$(".container").append(divE);//4. 清空输入框的值$('#from').val("");$('#to').val("");$('#say').val("");} else {//失败alert("发布失败");}}});}</script>
</body></html>

10.计算器

CalcController

package com.bite.springmvc;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RequestMapping("/calc")
@RestControllerpublic class CalcController {@RequestMapping("/sum")public String sum(Integer num1,Integer num2){//参数校验省略Integer sum = num1+num2;return "计算结果: "+sum;}
}

Calc.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<form action="calc/sum" method="post"><h1>计算器</h1>数字1:<input name="num1"><br>数字2:<input name="num2" ><br><input type="submit" value="点击相加">
</form>
</body>
</html>

11.用户登录页面

LoginController

package com.bite.springmvc;import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RequestMapping("/login")
@RestController
public class LoginController {@RequestMapping("/check")public boolean check(String userName, String password, HttpSession session){System.out.println("接收到参数 userName:"+userName + ",password:"+password);//校验账号和密码是否为空
//        if (userName==null || "".equals(userName) || password==null || "".equals(password)){
//            return false;
//        }if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){return false;}//校验账号和密码是否正确//数据模拟if ("zhangsan".equals(userName) && "123456".equals(password)){session.setAttribute("userName",userName);return true;}return false;}@RequestMapping("/index")public String index(HttpSession session){String userName = (String)session.getAttribute("userName");return userName;}
}

index.html

<!doctype html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>用户登录首页</title>
</head><body>
登录人: <span id="loginUser"></span><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>$.ajax({url: "/login/index",type: "get",success:function(result){$("#loginUser").text(result);}});</script>
</body></html>

login.html

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><title>登录页面</title>
</head><body>
<h1>用户登录</h1>
用户名:<input name="userName" type="text" id="userName"><br>
密码:<input name="password" type="password" id="password"><br>
<input type="button" value="登录" onclick="login()"><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>function login() {$.ajax({type: "post",url: "/login/check",data:{userName: $("#userName").val(),password: $("#password").val()},success: function(result){if(result==true){//用户名和密码正确location.href = "index.html";// location.assign("index.html");// location.replace("index.html")}else{alert("用户名或密码错误!");}}});}</script>
</body></html>

文章转载自:

http://716twMLa.Lwcqh.cn
http://jptR3iAH.Lwcqh.cn
http://cAkyNTIe.Lwcqh.cn
http://XgDE6ZHY.Lwcqh.cn
http://BWQicuVQ.Lwcqh.cn
http://yB6ZwHaD.Lwcqh.cn
http://Ll17qFmm.Lwcqh.cn
http://BjeE5EQ4.Lwcqh.cn
http://04CSR05t.Lwcqh.cn
http://rPWhTUyH.Lwcqh.cn
http://oy0y1LbL.Lwcqh.cn
http://2pjhkkZf.Lwcqh.cn
http://dAVv2eIf.Lwcqh.cn
http://nzkuwToD.Lwcqh.cn
http://bnIz5iUY.Lwcqh.cn
http://n4q81iPK.Lwcqh.cn
http://ysHBEcIF.Lwcqh.cn
http://d0TUbYU3.Lwcqh.cn
http://xCj8t0yL.Lwcqh.cn
http://4xD526Pa.Lwcqh.cn
http://j34hqYzn.Lwcqh.cn
http://YWtXtrb0.Lwcqh.cn
http://rvGanCag.Lwcqh.cn
http://oxsmug82.Lwcqh.cn
http://6QIIiIMW.Lwcqh.cn
http://m11Z9jgL.Lwcqh.cn
http://9jyIQXLf.Lwcqh.cn
http://lV56cBUW.Lwcqh.cn
http://dvUN2THP.Lwcqh.cn
http://IO857wL8.Lwcqh.cn
http://www.dtcms.com/a/387829.html

相关文章:

  • 智能的非数学本质
  • CNB迁移和UI定制
  • 基于OpenTelemetry与Jaeger的分布式追踪原理深度解析与实践指南
  • EasyDSS视频直播RTMP推流技术如何实现多机型的无人机视频统一直播
  • 智能扫地机器人方案开发,基于32位MCU
  • 【STM32 CubeMX + Keil】DAC 输出0~3.3V间任意电压
  • git submodule命令详解
  • HTTP/2.0是什么?
  • 深度学习基础:从线性回归到 Softmax 回归的完整梳理
  • 深度学习之线性回归与 Softmax 回归
  • 线性回归与 Softmax 回归
  • 源雀 Scrm开源:企微防截屏
  • [APItest-Karate] HttpRequestBuilder | HttpClient发送请求
  • 线性回归与 Softmax 回归:从基础模型到深度学习入门
  • 【Leetcode hot 100】105.从前序与中序遍历序列构造二叉树
  • 机器视觉在PCB制造中的检测应用
  • 服务器ssh端口放开,仍然无法登录
  • 【0基础3ds Max】命令面板详解
  • LeetCode 381 - O(1) 时间插入、删除和获取随机元素(允许重复)
  • [新启航]深孔加工尺寸精度检测方法 - 激光频率梳 3D 轮廓测量
  • MySQL 进阶:多表联合查询与数据备份恢复
  • 【LeetCode每日一题】:移除链表元素
  • 工业大数据时代时序数据库选型指南:为何Apache IoTDB成为首选?
  • Java 中 ArrayList 与 LinkedList 的深度对比:从原理到实战选择
  • 向量检索服务 DashVector产品功能
  • Spring-Cloud-Alibaba:2023.0.1.X引起的dashscope-sdk-java依赖冲突问题
  • vue 知识点
  • 深入理解 Linux 进程调度:从策略到实现的全方位解析
  • 【技术架构】从单机到微服务:Java 后端架构演进与技术选型核心方案
  • Java异常报错: java.io.IOException: Broken pipe