javaweb项目开发案例_java项目开发案例经典

javaweb项目开发案例_java项目开发案例经典,第1张

javaweb项目开发案例_java项目开发案例经典 spring.messages.basename=i18n.login这样就相当于把国际化资源文件让SpringBoot配置的ResourceBundleMessageSource管理了起来2021新版IDEA修改全部默认配置中的文件编码模式,解决properties配置文件乱码问题通过以上设置,我们根据浏览器语言的设置切换国际化,下面展示原理:SpringMVC的自动配置中有默认的区域信息解析器===>国际化Locale(区域信息对象),LocaleResolver(获取区域信息对象)点击链接实现国际化切换1.编写自己的区域信息解析器,并放到容器中自定义区域信息解析器:/*可以携带区域信息*/public class MyLocaleResolver implements LocaleResolver{@Overridepublic Locale resolveLocale(HttpServletRequest Request) {String l=Request.getParameter(“l”);Locale locale=Locale.getDefault();//Locale.getDefault()获取当前的语言环境— *** 作系统的语言环境if(!StringUtils.isEmpty(l)){String[] s = l.split(“_”);locale=new Locale(s[0],s[1]);//第一个参数是国家,第二个参数是语言}return locale;}@Overridepublic void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {}}SpringMVC扩展类: 负责将自定义的组件加入到容器中//使用WebMvcConfigurerAdapter可以来扩展SpringMvc的功能@Configurationpublic class myConfig extends WebMvcConfigurerAdapter{//所有的WebMvcConfigurerAdapter组件都会一起起作用@Bean//将容器注册在容器中public WebMvcConfigurerAdapter addViewControllers(){WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController(“/”).setViewName(“index”);registry.addViewController(“/index.html”).setViewName(“index”);}};return adapter;}@Bean//在SpringMVC扩展类中,将刚才写的区域信息解析器放到容器中public LocaleResolver localeResolver(){return new MyLocaleResolver();}}效果展示:登录模块===================================================================SpringMVC新特性支持的Rest风格的注解@RestController注解@RestController等常见注解@PostMapping, @GetMapping, @PutMapping, @DeleteMapping四个支持Rest风格的注解模板引擎页面修改后要时时生效==>禁用掉模板引擎的缓存+重新编译在全局配置文件中禁用掉模板引擎的缓存#禁用掉模板引擎的缓存,这样页面内容一修改,就可以看到修改后的效果spring.thymeleaf.cache=falseIDEA在项目运行期间,不会让我们对页面的修改生效,如果想让我们对页面的修改时时生效,第一步禁用缓存,第二步按住ctrl+f9重新编译当前页面Thymeleaf 内置对象和内置方法Thymeleaf 内置对象和内置方法转发到某一页面导致的表单重复提交问题解决表单重复提交问题登录成功后,要防止表单被重复提交,可以重定向到主页拦截器进行登录检查,防止不经过登录直接来到某一页面SpringBoot已经做好了静态资源的映射1.自定义登录拦截器,通过获取session中存放的数据,来判断是否已经登录过public class LoginHanlderIntercept implements HandlerInterceptor{@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Object user=request.getSession().getAttribute(“loginUser”);if(user==null){//未登陆,返回登陆页面request.setAttribute(“msg”,“没有权限请先登陆”);request.getRequestDispatcher(“/index.html”).forward(request,response);return false;}else{//已登陆,放行请求return true;}}@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 {//登陆后,将之前存储在session里面的登录凭证销毁,无论是否存在凭证,都执行销毁 *** 作request.getSession().removeAttribute(“loginUser”);}}2.如果登录成功,那么往session中存放一个username作为登录凭证@Controllerpublic class LoginController{@PostMapping(“/user/login”)public String Login(@RequestParam(“username”)String username,@RequestParam(“password”)String password, Map<String,Object> map, HttpSession session){if(username.equals(“大忽悠”)&&“123456”.equals(password)){session.setAttribute(“loginUser”,username);//登录成功return “redirect:/main.html”;}//登录失败map.put(“msg”,“用户名或密码错误”);return “index”;}}3.在springmvc扩展类中将自定义的拦截器进行注册//使用WebMvcConfigurerAdapter可以来扩展SpringMvc的功能@Configurationpublic class myConfig extends WebMvcConfigurerAdapter{//所有的WebMvcConfigurerAdapter组件都会一起起作用@Bean//将容器注册在容器中public WebMvcConfigurerAdapter addViewControllers(){WebMvcConfigurerAdapter adapter=new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController(“/”).setViewName(“index”);registry.addViewController(“/index.html”).setViewName(“index”);registry.addViewController(“/main.html”).setViewName(“success”);}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginHanlderIntercept()).addPathPatterns(“/**”)//拦截任意多层路径下的所有请求.excludePathPatterns(“/index.html”,”/”,”/user/login”);//某些请求不进行拦截}};return adapter;}@Bean//在SpringMVC扩展类中,将刚才写的区域信息解析器放到容器中public LocaleResolver localeResolver(){return new MyLocaleResolver();}}小细节:如果已经登录成功了,那么session域中就会存在已经登录的凭证,如果此时回退到登录页面,那么就可以不登录直接访问对应网页,这个的解决方法如下:使用下面这个解决方法的前提是拦截器只拦截登录页面,而不是所有请求,不然当登录成功后,点击当前页面的任何请求,都会回到登录页面@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {//登陆后,将之前存储在session里面的登录凭证销毁,无论是否存在凭证,都执行销毁 *** 作request.getSession().removeAttribute(“loginUser”);}拦截器如果拦截所有请求,静态资源也会被拦截,因此注意排除掉对应的静态资源访问路径CRUD—员工列表thymeleaf公共页面元素抽取这里模板名就是html页面的名字,即xxx(模板名).html这里的模板名会使用thymeleaf的前后缀配置规则进行解析三种引入功能片段的th属性具体使用参考下面这篇文章Thymeleaf 模板布局 th:fragment、th:replace、th:insert、th:remove如果使用了thymeleaf模板引擎,那么controller层的返回值就会由模板引擎自动拼串,因此如果我们还想转发或者重定向到某个请求,就需要加上forward或者redirect前缀加上forward或者redirect前缀后,springboot也提供了各自的视图解析处理器,底层就是原生的转发和重定向SpringMVC中的forward和redirect前缀路径问题:package com.czl.controller;@Controllerpublic class HelloController {/**forward:转发到一个页面/hello.jsp:转发当前项目下的hello;一定加上/,如果不加/就是相对路径。

容易出问题;forward:/hello.jspforward:前缀的转发,不会由我们配置的视图解析器拼串@return*/@RequestMapping(“handle01”)public String handle01(){System.out.println(“handle01…”);return “forward:/hello.jsp”;}@RequestMapping(“handle02”)public String handle02(){System.out.println(“handle02…”);return “forward:/handle01”;}/**重定向到hello.jsp页面有前缀的转发和重定向 *** 作,配置的视图解析器就不会进行拼串;转发 forward:转发的路径重定向 redirect:重定向的路径/hello.jsp:代表就是从当前项目下开始;在SpringMVC中会为路径自动的拼接上项目名原生的Servlet重定向/路径需要加上项目名才能成功,重定向的url路径是要发给浏览器让浏览器按照该url访问服务器的,而浏览器解析/ 只到站点,如 localhost:8080/,使用response.sendRedirect(“/hello.jsp”),浏览器只会解析为:localhost:8080/hello.jspresponse.sendRedirect(“/hello.jsp”)//访问不到,要加上项目名 /SpringMVC_viewResolver_06/hello.jsp@returnrd.include(requestToExpose, response);*/@RequestMapping(“handle03”)public String handle03(){System.out.println(“handle03…”);return “redirect:/hello.jsp”;}@RequestMapping(“handle04”)public String handle04(){System.out.println(“handle04…”);return “redirect:/handle03”;}}SprinBoot中的日期格式化问题SpringBoot底层日期格式化原理:默认有一个日期格式化器:![在这里插入图片描述](https://img-blog.csdnimg.cn/de16e8c47d51400989e3c05bd813b6ae.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzUzMTU3MTcz,size_16,color_FFFFFF,t_70)默认使用的日期格式是/方式,如果后台接收到前台的日期格式不是\,那么就会报错:我们可以在配置文件中进行日期格式修改,替换默认的日期格式:spring.mvc.date-format=yyyy-MM-ddThymeleaf 日期格式化处理${#dates.format(key)}${#dates.format(key, ‘yyyy-MM-dd HH:mm:ss’)}格式化传递过来的 Date 对象,如果没有指定时间格式,将使用浏览器当前使用的时间格式Thymeleaf 日期格式化处理JQuery中的submit事件来提交表单,也可以阻止表单的提交

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/662876.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-18
下一篇2022-04-18

发表评论

登录后才能评论

评论列表(0条)

    保存