做个网站需要多少钱?有没有旧装修要拆,wordpress seo设置,黄山学院教务管理系统,下载全网搜❣博主主页: 33的博客❣ ▶️文章专栏分类:JavaEE◀️ #x1f69a;我的代码仓库: 33的代码仓库#x1f69a; #x1faf5;#x1faf5;#x1faf5;关注我带你了解更多进阶知识 目录 1.前言2.响应2.1返回静态界面2.2返回数据2.3返回HTML代码 3.综合练习3.1计算器3.2用户登… ❣博主主页: 33的博客❣ ▶️文章专栏分类:JavaEE◀️ 我的代码仓库: 33的代码仓库 关注我带你了解更多进阶知识 目录 1.前言2.响应2.1返回静态界面2.2返回数据2.3返回HTML代码 3.综合练习3.1计算器3.2用户登录4.3留言板 5.应用分层6.企业规范7.总结 1.前言 上一篇文章我们已经讲了Spring MVC请求部分的内容这篇文章继续讲解。 2.响应
2.1返回静态界面
在之前的代码中我们都是以RestController来注解一个类通过这个注解那么就表明返回的内容都为数据所以前⾯使⽤的RestController 其实是返回的数据如果我们想返回一个界面可以用Controller如果要返回数据ControllerResponseBody RestController。
Controller
public class demo3 {RequestMapping(/index)public String INDEX(){return /index.html;}
} index.html内容
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
h1我是谁??/h1
/body
/html2.2返回数据
Controller
public class demo3 {ResponseBodyRequestMapping(/date)public String DATA(){return /index.html;}
} 2.3返回HTML代码
Controller
public class demo3 {ResponseBodyRequestMapping(/date2)public String DATA2(){return h1我是谁??/h1;}
}3.综合练习
3.1计算器
package com.example.test1;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/calc)
public class Caculation {RequestMapping(/sum)public String CAL(Integer num1,Integer num2){Integer sumnum1num2;return 计算结果sum;}
}HTML
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
body
form actioncalc/sum methodposth1计算器/h1数字1input namenum1 typetextbr数字2input namenum2 typetextbrinput typesubmit value 点击相加
/form
/body
/html结果
3.2用户登录
login界面
!DOCTYPE html
html langen
headmeta charsetUTF-8title登录页面/title
/head
body
h1用户登录/h1
用户名input nameuserName typetext iduserNamebr
密码input namepassword typepassword idpasswordbr
input typebutton value登录 onclicklogin()
script srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js/script
scriptfunction login() {$.ajax({url:/user/login,type: post,data:{username: $(#userName).val(),password: $(#password).val()},// http响应成功success:function(result){console.log(result)if(resulttrue){//页面跳转location.href index.html;// location.assign(index.html);}else{alert(密码错误);}}});}
/script
/body
/htmlindex界面
!doctype html
html langenheadmeta charsetUTF-8meta nameviewportcontentwidthdevice-width, user-scalableno, initial-scale1.0, maximum-scale1.0, minimum-scale1.0meta http-equivX-UA-Compatible contentieedgetitle用户登录首页/title
/headbody
登录人: span idloginUser/spanscript srchttps://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js/script
script$.ajax({url: /user/index,type: get,success:function(loginName){$(#loginUser).text(loginName);}});
/script
/body
/html后端代码
package com.example.test1;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
RestController
RequestMapping(/user)
public class UserController {RequestMapping(/login)public boolean login(String username, String password, HttpSession session){ if(!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){return false;}if(admin.equals(username)admin.equals(password)){session.setAttribute(username,username);return true;}return false;}RequestMapping(/index)public String getusername(SessionAttribute(username) String username){return username;}
} 4.3留言板
package com.example.test1;import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;RequestMapping(/message)
RestController
public class MessageController {private ListMessageInfo messageInfosnew ArrayList();RequestMapping(/publish)public Boolean publish(MessageInfo messageInfo){System.out.println(接收到参数messageInfo);//参数检验if(!StringUtils.hasLength(messageInfo.getFrom())||!StringUtils.hasLength(messageInfo.getTo())||!StringUtils.hasLength(messageInfo.getSay())){return false;}messageInfos.add(messageInfo);return true;}RequestMapping(/getlist)public ListMessageInfo getList(){return messageInfos;}
}package com.example.test1;
import lombok.Data;
Data
public class MessageInfo {private String from;private String to;private String say;
}HTML主要代码
script$.ajax({url: /message/getlist,type: get,success: function (messageInfos) {var finalHtml ;for (var message of messageInfos) {finalHtml div message.from 对 message.to 说: message.message /div;}$(.container).append(finalHtml);}});function submit() {console.log(发布留言);//1. 获取留言的内容var from $(#from).val();var to $(#to).val();var say $(#say).val();if (from || to || say ) {return;}//发送ajax请求$.ajax({url: /message/publish,type: post,data: {from: $(#from).val(),to: $(#to).val(),say: $(#say).val()},success: function (result) {if (result) {console.log(result)//2. 构造节点var divE div from 对 to 说: say /div;//3. 把节点添加到页面上$(.container).append(divE);//4. 清空输入框的值$(#from).val();$(#to).val();$(#say).val();}else{alert(输入不合法);}}});5.应用分层
通过上⾯的练习,我们学习了SpringMVC简单功能的开发,但是我们也发现了⼀些问题 ⽬前我们程序的代码有点杂乱,然⽽当前只是⼀点点功能的开发.如果我们把整个项⽬功能完成呢?代码会更加的杂乱⽆章。 在之前我们学过MVC把整体的系统分成了model模型、View(视图)和Controller控制器三个层次也就是将⽤⼾视图和业务处理隔离开并且通过控制器连接起来很好地实现了表现和逻辑的解耦是⼀种标准的软件分层架构。 目前现在更主流的开发方式是前后端分离“的⽅式,后端开发⼯程师不再需要关注前端的实现, 所以对于Java后端开发者,⼜有了⼀种新的分层架构:把整体架构分为表现层、业务逻辑层和数据层,这种方式使代码高内聚低耦合。这种分层⽅式也称之为三层架构”。 表现层就是展⽰数据结果和接受⽤⼾指令的是最靠近⽤⼾的⼀层 业务逻辑层:负责处理业务逻辑, ⾥⾯有复杂业务的具体实现 数据层: 负责存储和管理与应⽤程序相关的数据 我们一般把表现层命名为Controller业务逻辑层命名为Service数据层命名为Dao
6.企业规范
1.类名使用大驼峰的形式 2.⽅法名、参数名、成员变量、局部变量统使⽤⼩驼峰⻛格 3. 包名统⼀使⽤⼩写点分隔符之间有且仅有一个⾃然语义的英语单词
7.总结 学习SpringMVC,其实就是学习各种Web开发需要⽤的到注解RequestMapping:路由映射 RequestParam:后端参数重命名 RequestBody:接收JSON类型的参数 PathVariable: 接收路径参数RequestPart: 上传⽂件ResponseBody:返回数据 等等… 下期预告Spring IOCDI