公司网站建设服务,要写网站建设方案,优化公司怎么优化网站的,wap百度文章目录1. 默认情况下返回静态页面2. 返回一个非静态页面的数据2.1 ResponseBody 返回页面内容2.2 RestController ResponseBody Controller3. 实现登录功能#xff0c;返回 JSON 对象3.1 前端使⽤ ajax#xff0c;后端返回 json 给前端3.2 前端发送 JSON 的标准格式4. 请…
文章目录1. 默认情况下返回静态页面2. 返回一个非静态页面的数据2.1 ResponseBody 返回页面内容2.2 RestController ResponseBody Controller3. 实现登录功能返回 JSON 对象3.1 前端使⽤ ajax后端返回 json 给前端3.2 前端发送 JSON 的标准格式4. 请求转发(forward)或请求重定向(redirect)4.1 请求转发forward4.2 请求重定向redirect5. 请求转发和请求重定向的区别1. 默认情况下返回静态页面
直接在这里返回的那么返回的就是静态页面的名称比如
Controller
public class TestController {RequestMapping(/sayhi)public String sayHi() {return hello.html;}
}
!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
bodyh1 stylecolor: red我是 hello.html/h1
/body
/html可以看到前面返回的内容是 hello.html这里通过一级目录 sayhi也可以访问到这个页面说明返回的虽然是 String 类型但实际上是一个页面
2. 返回一个非静态页面的数据
2.1 ResponseBody 返回页面内容
ResponseBody 可以修饰类也可以修饰方法 修饰类 表示当前类中的所有方法都会返回一个非静态页面的数据 修饰方法表示当前方法返回的是一个非静态页面的数据
Controller
ResponseBody
public class TestController {RequestMapping(/sayhi)public String sayHi() {return hello.html;}
}2.2 RestController ResponseBody Controller
直接使用 RestController代替ResponseBody 和 Controller 两个注解返回页面内容
RestController
public class TestController {RequestMapping(/sayhi)public String sayHi() {return hello.html;}
}示例实现简单计算的功能
!doctype html
html langen
headmeta charsetUTF-8meta nameviewportcontentwidthdevice-width, user-scalableno, initial-scale1.0, maximum-scale1.0, minimum-scale1.0meta http-equivX-UA-Compatible contentieedgetitle计算器示例/title
/head
body
form action/calch1计算器/h1数字1: input namenum1 typetextbr数字2: input namenum2 typetextbrinput typesubmit value 点击相加
/form
/body
/htmlpackage com.example.demo.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class CalcController {RequestMapping(/calc)public String calc(Integer num1, Integer num2) {if (num1 null || num2 null) return h1参数错误/h1;return h1结果 (num1 num2) /h1a hrefjavascript:history.go(-1)返回/a;}} 3. 实现登录功能返回 JSON 对象
3.1 前端使⽤ ajax后端返回 json 给前端
前端代码
!doctype html
html langen
headmeta charsetUTF-8meta nameviewportcontentwidthdevice-width, user-scalableno, initial-scale1.
0, maximum-scale1.0, minimum-scale1.0meta http-equivX-UA-Compatible contentieedgescript srcjs/jquery-2.1.4.min.js/scripttitleDocument/titlescript// ajax 提交function mysub() {// 判空// var username $(#username);//这里注意如果在js中有多个 jquery 那么这里的$是不合适的$相当于小名jQuery相当于大名最好使用大名var username jQuery(#username);var password jQuery(#password);if(jQuery.trim(username.val()) ) {alert(请先输入用户名!);username.focus; // 让光标重置到此元素return;}if(jQuery.trim(password.val()) ) {alert(请先输入密码!);password.focus();return;}jQuery.ajax({url:/user/login2,type:POST,data:{username:username.val(),password:password.val()},success:function(result) {alert(JSON.stringify(result));}});}/script
/head
body
div styletext-align: center;h1登录/h1⽤户input idusernamebr密码input idpassword typepasswordbrinput typebutton value 提交 onclickmysub() stylemargin-top: 20px;margin-left: 50px;
/div
/body
/html后端代码
Slf4j
Controller
ResponseBody
RequestMapping(/user)
public class UserController {RequestMapping(/login2)public HashMapString, Object login2(String username, String password) {HashMapString, Object result new HashMap();int state 200;int data -1; // 等于1登录成功否则登录失败String msg 未知错误;if(StringUtils.hasLength(username) StringUtils.hasLength(password)) {if(username.equals(admin) password.equals(admin)) {data 1;msg ;} else {msg 用户名或密码错误!;}} else { //说明参数为空msg 非法参数;}result.put(state, state);result.put(data, data);result.put(msg,msg);return result;}
}3.2 前端发送 JSON 的标准格式 后端接收 JSON 数据的标准格式
4. 请求转发(forward)或请求重定向(redirect)
通过 return 可以实现跳转有两种方式
forward 请求转发redirect 请求重定向
4.1 请求转发forward 请求转发是服务器端帮用户实现的 Controller
public class TestController {//请求转发方式1RequestMapping(/fw)public String myForward() {return forward:/hello.html;}//请求转发方式2RequestMapping(/fw2)public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.getRequestDispatcher(/hello.html).forward(request,response);}
}4.2 请求重定向redirect 重定向的请求发生在客户端浏览器端 Controller
public class TestController {//请求重定向方式1RequestMapping(/rd)public String myRedirect() {return redirect:/hello.html;}//请求重定向方式2RequestMapping(/rd2)public void myRedirect2(HttpServletResponse response) throws IOException {response.sendRedirect(/hello.html);}
}5. 请求转发和请求重定向的区别
跳转的实现方式有两种请求转发Forward和请求重定向Redirect
请求转发就好比张三客户端找李四帮忙服务器端而李四说我没空但我可以让王五帮你的忙在这个过程中张三只找了一个人剩下的事情都是李四完成的这个就是请求转发
请求重定向就好比张三客户端找李四帮忙服务器端而李四说我没空你去要王五帮你的忙吧于是张三就是找王五帮忙了这就是请求重定向 具体的区别有下面几点
定义 请求转发 发生在服务器端的程序中当服务器收到客户请求后会先将请求转发给目标地址然后再将地址返回的结果转发给客户端 请求重定向服务器端接收到客户端的请求后会给客户端返回一个临时响应头这个临时响应头中记录了客户端需要再次发送请求重定向的URL地址客户端收到地址后会将请求发送到新的地址中 数据共享 请求转发是服务器端实现的整个执行过程客户端只需要发送一次请求所以整个交互过程中使用的都是同一个 Request 请求对象和 Response 响应对象因此请求和返回的数据是共享的 请求重定向客户端发送两次完全不同的请求所以两次请求中的数据是不同的 请求方 请求转发是服务器端的行为 请求重定向是客户端的行为浏览器 代码实现不同 最终的 URL地址 请求转发是服务器端代为请求所以整个请求过程中 URL 地址是不变的 请求重定向是服务器端告诉客户端访问另一个地址所以浏览器会重新再发送一个请求因此 URL 地址放生了改变