SpringMVC归纳-2(Session会话、拦截器)
程序员文章站
2023-10-17 09:01:48
要点: 1.HttpSession:一个session的建立是从一个用户向服务器发第一个请求开始,而以用户显式结束或session超时为结束,借助session能在一定时间内记录用户状态。 2.ModelAndView:既可以设置URL地址,又可以渲染视图。 3.HandlerInterceptor ......
要点:
1.httpsession:一个session的建立是从一个用户向服务器发第一个请求开始,而以用户显式结束或session超时为结束,借助session能在一定时间内记录用户状态。
2.modelandview:既可以设置url地址,又可以渲染视图。
3.handlerinterceptor:拦截器接口,通过实现改接口的三个方法(prehandle、posthandle、aftercompletion)可以自定义一个拦截器。
实例:
实现登录、购物车功能。
login.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>乐淘登录</title> </head> <body> <form action="http://localhost:8888/login-commit" method="post"> <input type="submit" value="login"> </form> </body> </html>
mall.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>乐淘商场</title> </head> <body> <form action="http://localhost:8888/shopcar" method="post"> <label>请选择要购买的书籍:<br> <input type="checkbox" name="bookname" value="c prime plus">c prime plus<br> <input type="checkbox" name="bookname" value="c prime plus2">c prime plus2<br> <input type="checkbox" name="bookname" value="c prime plus3">c prime plus3<br> <input type="checkbox" name="bookname" value="c prime plus4">c prime plus4<br> </label> <input type="submit" value="加入购物车"> </form> <form action="http://localhost:8888/quit" method="post"> <input value="log out" type="submit"> </form> </body> </html>
myshoppingcar.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>乐淘购物车</title> </head> <body> <h2>我的购物车:</h2> <!--存在购买时显示已购书籍--> <ul th:if="${books.size()} != 0"> <li th:each="bookcount:${books}" th:text="${bookcount}"></li> </ul> <!--无购买--> <div th:if="${books.size()} == 0"> 您未购买任何书籍! </div> <form action="http://localhost:8888/mall" method="get"> <input type="submit" value="继续购买"> </form> </body> </html>
books.java
package com.example.demo.controller.login; import java.util.hashmap; import java.util.map.entry; import java.util.set; public class books { private hashmap<string,integer> books = new hashmap<string,integer>(); //根据请求url中获得的表单数组,更新已有书本 public void refreshbooks(string[] newbooks) { for (string book:newbooks) { if (books.containskey(book)) { books.put(book, books.get(book)+1); }else { books.put(book, 1); } } } //获取books集合,送往页面 public set<entry<string, integer>> getbooks(){ return books.entryset(); } }
logincontroller.java
package com.example.demo.controller.login; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpsession; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; @controller public class logincontroller { //登录界面 @getmapping("login") public string getlogin(httpservletrequest req) { httpsession session = req.getsession(); //已登录直接进入到商场 if (session.getattribute("login")!=null) { return "redirect:mall"; } return "login"; } //登录请求处理 @postmapping("login-commit") public string postlogin(httpservletrequest req) { httpsession session = req.getsession(); //session标记登录状态 session.setattribute("login",true); return "redirect:mall"; } //退出登录,保留购物记录 @postmapping("quit") public string quit(httpservletrequest req) { httpsession session = req.getsession(); session.removeattribute("login"); return "redirect:login"; } }
shoppingcontroller.java
package com.example.demo.controller.login; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpsession; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.postmapping; import org.springframework.web.servlet.modelandview; @controller public class shoppingcontroller { //商场 @getmapping("mall") public string getmall() { return "mall"; } //我的购物车 @postmapping("shopcar") public modelandview getshopcar(modelandview view,httpservletrequest req) { httpsession session = req.getsession(); books books = (books) session.getattribute("shopcar");//session会话获取已购书籍 string[] newbooks = req.getparametervalues("bookname");//新的购物请求 //从未购买 if (books==null){ books = new books(); } //刷新session会话 if (newbooks!=null) { books.refreshbooks(newbooks); session.setattribute("shopcar", books); } //刷新model,渲染视图 view.addobject("books",books.getbooks()); view.setviewname("myshoppingcar"); return view; } }
logininterceptor.java
package com.example.demo.controller.login; import java.io.ioexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; //登录拦截器 public class logininterceptor implements handlerinterceptor { @override //在controller方法之前调用 public boolean prehandle(httpservletrequest req,httpservletresponse res,object handler) throws ioexception { httpsession session = req.getsession(); if (session.getattribute("login") != null) { system.out.println("login!"); return true; } res.sendredirect("login"); return false; } @override public void posthandle(httpservletrequest req,httpservletresponse res,object handler,modelandview modelandview) throws exception { //controller方法处理完,但html页面并未送出显示时调用 } @override public void aftercompletion(httpservletrequest req,httpservletresponse res,object handler,exception ex) { //页面渲染完毕后调用,通常用来回收资源,类似于finally } }
springwebconfig.java
package com.example.demo.controller.login; import org.springframework.boot.springbootconfiguration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; @springbootconfiguration public class springwebconfig implements webmvcconfigurer{ //增加拦截器,可以对特定url设定拦截以检查是否登录 public void addinterceptors(interceptorregistry registry) { //装载一个拦截器,可以使用通配符,或字符串数组 registry.addinterceptor(new logininterceptor()).addpathpatterns("/mall","/quit"); } }
下一篇: linux每日命令(32):gzip命令