怎么分辨网站是不是h5,企业网站可信度建设,wordpress忘记密码怎么办,市场营销策划课程承接上篇文章——Spring Web MVC探秘#xff0c;在了解Spring Web MVC背后的工作机制之后#xff0c;我们接下来通过三个实战项目#xff0c;来进一步巩固一下前面的知识。 一、计算器 效果展示#xff1a;访问路径#xff1a;http://127.0.0.1:8080/calc.html 前端代码在了解Spring Web MVC背后的工作机制之后我们接下来通过三个实战项目来进一步巩固一下前面的知识。 一、计算器 效果展示访问路径http://127.0.0.1:8080/calc.html 前端代码文件名calc.html !DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
body!--点击“按钮”后页面会跳转到calc/sum页面--form actioncalc/sum methodposth1计算器/h1数字1input namenum1 typetextbr数字2input namenum2 typetextbr!-- 只有input标签且具有name属性的内容才会被作为表单参数,传递给后端--input typesubmit value 点击相加 /form
/body/html 后端代码  package com.example.mvc_test1;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/calc)
public class CalcController {RequestMapping(/sum)//访问路径类路径方法路径/calc/sumpublic String sum(Integer num1,Integer num2){Integer sum num1num2;return 计算的结果是sum;}
} 代码解析 前端部分 当用户在num1和num2输入框中输入数字后点击 “点击相加” 按钮浏览器会根据form标签的action属性calc/sum和method属性post将表单数据以POST请求的方式发送到后端对应的/calc/sum路径。 后端部分 1、后端的CalcController类被RestController注解标记表明它是一个处理 RESTful 请求的控制器并且被RequestMapping(/calc)映射到/calc路径下。 2、sum方法被RequestMapping(/sum)注解意味着它处理/calc/sum路径的请求。 3、当后端发送请求到calc/sum路径时后端代码从前端参数表单中获取参数num1和num2赋值给sum方法的两个名称对应的参数num1、num2 4、Integer sum  num1  num2;在后端接收到这两个参数后将它们相加得到结果sum。 return 计算的结果是  sum;最后将计算结果以字符串的形式返回给前端前端会根据响应进行相应的显示或处理。 二、用户登录界面 效果展示访问路径http://127.0.0.1:8080/login.html 用户名错误登录  用户名和密码正确登录   后端代码 我们先来分析一下这个”登录界面”的需求当用户输入用户名和密码之后后端服务器需要做两件事 
1、判断用户是否进行合法的输入 
2、校验用户名和密码是否正确 
3、将用户名存入Session中 
4、返回用户名给前端 我们将这四件事封装到两个方法中login方法和getUser方法 RestController
RequestMapping(/user)
public class UserController {//login方法负责1、2、3件事务的完成RequestMapping(/login) //访问路径/user/loginpublic boolean login(String userName,String password,HttpServletRequest request){//当用户输入空字符或者没有输入返回falseif(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){return false;}else if(admin.equals(userName)admin.equals(password)){//校验用户名和密码是否正确//用户名和密码这里都设置为admin//登录成功将用户名设置到Session中HttpSession sessionrequest.getSession();session.setAttribute(userName,userName);return true;}//其他情况返回falsereturn false;}//getUser方法,负责第4个事务的完成RequestMapping(/getUser) //访问路径user/getUserpublic String  getUserName(HttpServletRequest request){HttpSession sessionrequest.getSession();//从请求报文中获取SessionString userName(String) session.getAttribute(userName);//从Session中获取userNamereturn userName;}
}注StringUtils.hasLength()是一个工具方法用于检查字符串是否有长度即不为null且长度大于0。 前端代码登录页面login.html) 和登录成功的页面index.html) 由于这两个页面的实现需要引入一个js文件我们先来讲一下如何有引入 1、js下载文件网址 https://code.jquery.com/jquery-3.7.1.min.js 2、将刚刚下载的js文件赋值粘贴到static文件夹下面 登录页面login.html !DOCTYPE html
html langenheadmeta charsetUTF-8title登录页面/title
/headbodyh1用户登录/h1用户名input nameuserName typetext iduserNamebr密码input namepassword typepassword idpasswordbrinput typebutton value登录 onclicklogin()script srcjquery-3.7.1.min.js/script  !--  引入js文件--scriptfunction login() {$.ajax({type:post,//请求类型url:/user/login,//访问后端服务器的user/logindata:{//获取标签的值赋值给后端指定的参数//左边的usrName表示后端/user/login 方法中的参数userName//右边userName表示id为userName的标签userName:$(#userName).val(),//val()获取标签的值password:$(#password).val()},success:function (result){//访问成功后端返回一个结果作为function的参数result//访问/user/login时后端会将这个请求交给login方法处理处理结果返回true或false表示登录成功与否if(resulttrue){//用户名和密码正确后端返回truelocation.hrefindex.html;}else{alert(用户名或者密码错误);}}});}/script
/body/html 登录成功的页面index.html !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 srcjquery-3.7.1.min.js/script !--  引入js文件--script$.ajax({type:get,url:/user/getUser,success:function (userName){//访问成功后端返回的结果作为function方法的参数//访问user/getUser时后端会将这个请求交给getUser方法处理结果返回用户名$(#loginUser).text(userName);}});/script
/body/html 前端代码解析 或许你有一个疑问用户在输入用户名和密码后校验工作是在后端中的login方法完成的那么校验完成后前端是如何获取后端检验后的结果呢这其实借助了一个技术——Ajax。 AjaxAsynchronous JavaScript and XML即异步 JavaScript 和 XML是一种创建快速动态网页的技术。 这里截取了login.html前端代码中使用到Ajax的部分代码省略注释版 $.ajax({type: post, url: /user/login, data: {userName: $(#userName).val(), password: $(#password).val()},success: function (result) {if (result  true) {location.href  index.html;} else {alert(用户名或者密码错误);}}
}); 1、  $.ajax({...})这是 jQuery 的 ajax 函数用于发起HTTP 请求。 2、type: post指定请求的类型为 POST。这意味着会向服务器发送一个 POST 请求。 3、url: /user/login指定请求的 URL即要发送请求的后端服务地址这里会将请求发送到 /user/login 路径。 4、data这是要发送给服务器的数据是一个对象。userName: $(#userName).val()使用 jQuery 的 $(#userName) 选择器选中 id 为 userName 的元素并通过 val() 方法获取其值将其赋值给 userName 属性。 5、password: $(#password).val()同理获取 id 为 password 的元素的值并将其赋值给 password 属性。 6、success: function (result) {...}定义请求成功时的回调函数。result当请求成功后服务器返回的数据将作为 result 参数传递给该回调函数。 7、if (result  true) {...} else {...}根据服务器返回的结果进行不同的操作。location.href  index.html;如果结果为 true说明登录成功将页面重定向到 index.html。alert(用户名或者密码错误);如果结果不为 true弹出一个警告框提示用户用户名或密码错误。 三、留言板 效果展示访问路径http://127.0.0.1:8080/messagewall.html 后端代码 让我们来分析一下这个“留言板”首先在打开这个留言板的时候留言板会加载历史的留言记录然后在用户添加新留言之后点击“提交”新的留言会添加到页面上。 根据功能后端代码需要完成两件事 1、在前端打开这个页面的时候返回历史留言内容给前端用于前端加载历史留言内容 
2、在用户添加新的留言的时候将这条新留言存储在后端服务器中 MessageInfo类 
Data//会自动生成该类的成员的Getter和Setter等方法,需要在pom.xml引入lombok依赖
public class MessageInfo {String from;String to;String say;
}在pom.xml中引入lombok依赖Data注解使用需要  MessageController类 
RestController
RequestMapping(/message)
public class MessageController {ListMessageInfo listnew ArrayList();//存放留言内容//getList方法返回历史留言内容给前端(访问路径/message/getList)RequestMapping(/getList)public ListMessageInfo getList(){return list;}//publish方法将用户新添加的留言存在后端服务器中(访问路径/message/publish)//获取用户输入的内容添加到list返回一个json字符串RequestMapping(value  /publish,produces  application/json)//指定该方法返回的数据类型为application/json表明此方法返回的内容是 JSON 格式。告知前端返回的数据是 JSON 格式。public String publish(RequestBody MessageInfo message){if(StringUtils.hasLength(message.from)StringUtils.hasLength(message.to)StringUtils.hasLength(message.say)){list.add(message);return {\ok\:1};//1表示添加成功}return {\ok\:0};//0表示添加失败}
}前端代码 messagewall.html !DOCTYPE html
html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title留言板/titlestyle.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
/headbodydiv classcontainerh1留言板/h1p classgrey输入后点击提交, 会将信息显示下方空白处/pdiv classrowspan谁:/span input typetext name idfrom/divdiv classrowspan对谁:/span input typetext name idto/divdiv classrowspan说什么:/span input typetext name idsay/divinput typebutton value提交 idsubmit onclicksubmit()!-- divA 对 B 说: hello/div --/divscript srcjquery-3.7.1.min.js/script !--  引入js文件--script//加载历史留言记录$.ajax({type:get,url:/message/getList,success:function (list){for(let msg of list){console.log(1);let divE  divmsg.from 对 msg.to  说:  msg.say/div;//拼接留言$(.container).append(divE);//把留言内容添加到页面上}}});function submit(){//获取留言的内容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,say:say}),success:function (result){if(result.ok1){alert(添加成功)//构造节点let divE  divfrom 对  to  说:  say/div;//把节点添加到页面上$(.container).append(divE);//清空输入框的值$(#from).val();$(#to).val();$(#say).val();}else{alert(添加失败)}}});}/script
/body/html 前端代码解析 1、注释“传递留言内容给前端部分”代码 contentType: application/json告诉后端我要传给你一个Json字符串的数据。    data: JSON.stringify({...})将包含from、to、say的对象转换为 JSON 字符串作为请求数据发送给后端让后端的publish方法中的message接收。 successfunctionresult当后端返回一个json字符串给前端接收时前端会自动将这个json字符串转换为一个对象例如这里的publish方法返回的如果是 {“ok”1} 的json字符串将其作为参数传送给function时此时的result就成为了一个包含“ok”属性的对象其属性值为1。 文章转载自: http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.rdkt.cn.gov.cn.rdkt.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.sxtdh.com.gov.cn.sxtdh.com http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.yxbdl.cn.gov.cn.yxbdl.cn http://www.morning.dbrnl.cn.gov.cn.dbrnl.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.gwdmj.cn.gov.cn.gwdmj.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.jfsbs.cn.gov.cn.jfsbs.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.ryfqj.cn.gov.cn.ryfqj.cn http://www.morning.ngkng.cn.gov.cn.ngkng.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.ygztf.cn.gov.cn.ygztf.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.qnlbb.cn.gov.cn.qnlbb.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.xcjbk.cn.gov.cn.xcjbk.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.llsrg.cn.gov.cn.llsrg.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.trqhd.cn.gov.cn.trqhd.cn http://www.morning.mjats.com.gov.cn.mjats.com http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.ckctj.cn.gov.cn.ckctj.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.kqpsj.cn.gov.cn.kqpsj.cn http://www.morning.qlsbz.cn.gov.cn.qlsbz.cn http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.csjps.cn.gov.cn.csjps.cn