springboot-web开发
文章目录
- 抽取公共页面
- 遍历数据
- 拦截器
- 文件上传
- 原生servlet注入
抽取公共页面
表单提交
<form class="form-signin" action="index.html" method="post" th:action="@{/login}">
IndexController
//去登录页@GetMapping(value = {"/","/login"})public String loginPage(){return "login";}@PostMapping("/login")public String main(User user, HttpSession session, Model model){if(StringUtils.hasLength(user.getUsername()) && "12345".equals(user.getPassword())){session.setAttribute("loginUser",user);//登录成功重定向到main.html,防止表单重复提交return "redirect:/main.html";}else {model.addAttribute("msg","账号密码错误");return "login";}}/*跳转页面,去main页面*/@GetMapping("/main.html")public String mainPage(HttpSession session,Model model){Object loginUser = session.getAttribute("loginUser");if(loginUser != null){return "main";}else {model.addAttribute("msg","请重新登录");return "login";}}
登录时错误提示
<label style="color: red" th:text="${msg}"></label>
显示登录用户
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><img src="data:images/photos/user-avatar.png" alt="" />[[${session.loginUser.username}]]<span class="caret"></span></a>
遍历数据
@GetMapping("/dynamic_table")public String dynamic_table(Model model){List<User> users = Arrays.asList(new User("zhangsan","123435"),new User("lisi","1234567"),new User("wangwu","12345678"),new User("jahui","93749237"));model.addAttribute("users",users);return "table/dynamic_table";}
<thead><tr><th>#</th><th>用户名</th><th>密码</th></tr></thead><tbody><tr class="gradeX" th:each="user,status:${users}"><td th:text="${status.count}">aaa</td><td th:text="${user.username}">bbb</td><td>[[${user.password}]]</td></tr></tbody></table>
拦截器
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/","/login","/css/**","/fonts/**","/img/**","/js/**");}
}
/*目标方法执行之前*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//登录检查HttpSession session = request.getSession();Object loginUser = session.getAttribute("loginUser");if(loginUser != null){return true;}// session.setAttribute("msg","先登录");
// response.sendRedirect("/");request.setAttribute("msg","请先登录");request.getRequestDispatcher("/").forward(request,response);return false;}/*目标方法执行完成以后*/@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}/*页面渲染之后*/@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
文件上传
@Controller
public class FormTestController {@GetMapping("/form_layouts")public String from_layouts() {return "form/form_layouts";}/*** 自动封装上传过来的文件* @param email* @param username* @param headerImg* @param photos* @return*/@PostMapping("/upload")public String upload(@RequestParam("email") String email,@RequestParam("username") String username,@RequestParam("headerImg") MultipartFile headerImg,@RequestPart("photos") MultipartFile[] photos) throws IOException {if(!headerImg.isEmpty()){String originalFilename = headerImg.getOriginalFilename();headerImg.transferTo(new File("D:\\abc\\"+originalFilename));}if(photos.length>0){for(MultipartFile photo:photos){if(!photo.isEmpty()){String originalFilename = photo.getOriginalFilename();photo.transferTo(new File("D:\\abc\\"+originalFilename));}}}return "main";}}
<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data"><div class="form-group"><label for="exampleInputEmail1">邮箱</label><input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email"></div><div class="form-group"><label for="exampleInputPassword1">名字</label><input type="password" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password"></div><div class="form-group"><label for="exampleInputFile">头像</label><input type="file" name="headerImg" id="exampleInputFile"></div><div class="form-group"><label for="exampleInputFile">生活照</label><input type="file" name="photos" multiple></div><div class="checkbox"><label><input type="checkbox"> 检查</label></div><button type="submit" class="btn btn-primary">提交</button></form>
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
原生servlet注入
@WebServlet(urlPatterns = "/my")
public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.getWriter().write("66666");}
}
@ServletComponentScan(basePackages = "com.atjh.boot05webadmin")
@SpringBootApplication
public class Boot05WebAdminApplication {public static void main(String[] args) {SpringApplication.run(Boot05WebAdminApplication.class, args);}}
springboot-web开发相关推荐
- springboot:web开发-Thymeleaf
1.thymeleaf依赖 <dependency><groupId>org.thymeleaf</groupId><artifactId>thymel ...
- Spring Boot与Web开发简介||SpringBoot对静态资源的映射规则
Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运 ...
- Web开发静态资源处理---SpringBoot
Web开发静态资源处理 使用SpringBoot的步骤: 1.创建一个SpringBoot应用,选择我们需要的模块,SpringBoot就会默认将我们的需要的模块自动配置好 2.手动在配置文件中配置部 ...
- Springboot的web开发-静态资源
1.web开发简介 SpringMVC自动配置概览 Spring Boot provides auto-configuration for Spring MVC that works well wit ...
- springboot thymeleaf配置_【程序源代码】Spring Boot 开发笔记web开发实战1
关键字:<Spring Boot 开发笔记>系列文章 各位亲爱的小伙伴:大家好! <Spring Boot 开发笔记>系列文章 这套笔记和源码是我自己在学习springboot ...
- 史上最全SpringBoot教程,从零开始带你深入♂学习(四)——web开发
Springboot(四)--web开发 静态资源 四个目录存放的静态资源可以被我们识别,用来存放我们的html.css.js.图片等文件 "classpath:/META-INF/reso ...
- SpringBoot 之 Web开发
2.Web开发 2.1.SpringMVC自动化配置概述 Spring Boot provides auto-configuration for Spring MVC that works well ...
- springboot系列课程笔记-第四章-WEB开发
四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...
- Springboot学习笔记(二)Web开发
前言: 学习B站UP主狂神说视频笔记整理视频链接 狂神笔记链接 上篇笔记链接-Springboot学习笔记(一)快速上手 Web开发 静态资源 在以往的SpringMVC中所有静态资源或者页面应该放在 ...
- SpringBoot的Web开发入门案例1
SpringBoot的Web开发入门案例1-登录和页面数据遍历读取 新建maven项目:logintest pom.xml文件: <project xmlns="http://mave ...
最新文章
- 只出现一次的数字—leetcode136
- 210129阶段三调试、进程间通信-共享内存
- 使用spring @Scheduled注解执行定时任务
- WTM框架使用技巧之:Layui版本嫁接Vue+ElementUI
- 连续内存分区式内存管理
- 咨询笔记:麦肯锡7步成诗
- 实作 ASP.NET 多笔数据离线编辑
- NAT和代理服务器的调研
- 因增强导致BDC录屏执行异常的梗
- 力扣周赛 239 题解
- 计算机二级考试office
- Excel使用攻略(1)
- android 9 手机硬件性能,一加9系列系统评测:功能丰富+稳定流畅,或是目前最佳安卓系统...
- icode 三级训练场if入门1-10关
- AE基础教程(21)——第21章 层的属性简介
- java跳出pages为空,itext生成PDF出错java.io.IOException: The document has no pages
- 直板何时用推挡,何时用反面横打
- 变压器绕组变形试验的重要性
- 弹弹堂服务器如何修改,弹弹堂端游服务端+手工弹弹堂游戏客户端+GM管理后台+附安裝构建实例教程...
- 手机直播助手连接到服务器,5步解决多开抖音直播伴侣的提示:服务器终点无法运行操作或者创建视频源失败请重试解决方法视频教程...
热门文章
- Dynamic Web Module 3.0 requires Java 1.6 or newer.
- 每天看一片代码系列(二):WebSocket-Node
- DELPHI 7 动态链接库DLL断点调试
- THANATOS数据库(自噬调节相关蛋白及其翻译后修饰信息数据库)使用指南
- 2021-11-16数据结构
- ISE简介及其下载 安装 和谐 与 卸载
- Verilog功能模块——串行数据转并行数据
- ArcGIS Server 10.2 安装教程
- 音频处理九:(参数估计)
- bootstrap 右对齐样式_Bootstrap的文本处理