美文网首页
springBoot实现CRUD操作1

springBoot实现CRUD操作1

作者: coderymy | 来源:发表于2019-04-30 13:44 被阅读0次

知识点:

对springMVC增加自己的配置信息时,可以将信息这样写

1556601474351.png
  1. 使用静态自愿的时候,推荐使用webjars引入其对应的maven,然后页面中引用时可以使用

    <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n11" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> th:href="@{/webjars/XXX/XXX/XXX}"</pre>

    使用@的好处是,以后无论是项目变了还是其他的什么路径变了,@都可以自动匹配配置中的项目名,来获取对应的静态资源

  2. 登录页面的国际化

    1. 基本原理--->

      1. 编写国际化配置文件

      2. 使用ResourceBundleMessageSource管理国际化资源文件

      3. jsp时可以在页面中使用fmt:message取出国际化内容

    2. 使用springboot后的完整操作

      1. 编写国际化配置文件,抽取页面需要显示的国际化消息
    3. 具体使用方式请查看别的markdown国际化

  3. 登录拦截器前置

    1. Controller中处理请求对应不同的请求方式可以使用

      1. @PostMapping("/user/login")

      2. @GetMapping("/user/login")

      3. @PutMapping("/user/login")

    2. 使用@RequestParam标注前端传入的参数,这样之哟啊使用@RequestParam标注的请求一旦没有提交就会报错

      <pre mdtype="fences" cid="n83" lang="" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> @PostMapping("/user/login")
      public String login(@RequestParam("username") String username,@RequestParam("password") String password)</pre>

    3. 如果需要封装消息,可以在传入参数时加上一个"Map<String Obiect> map",然后使用map.put("","")键值对的形式传入参数

    4. 在前端可以使用th:if来做判断,当需要判断为空时可以这样写

      <pre mdtype="fences" cid="n104" lang="" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> <p style="color:red" th:text="{msg}" th:if="{not #string.isEmpty(msg)}"></p></pre>

      这样就可以实现对信息存在的判断,存在就显示,不存在就不显示

    5. 为了防止登录到主界面之后,防止表单重复提交,最好的办法就是重定向,可以使其重定向到成功页面,这个时候就需要将用户的信息封装在session对象中,然后使用重定向到之前可以直接进行页面跳转的类中进行页面跳转

    6. 重定向,return "redirect:/XXX"XXX代表当前项目下的Controller的注解的value

  4. 实现拦截器

    1. 为了防止用户直接通过页面的url访问主界面从而跳过登录页面,实现登录检查

    2. 在component中创建一个LoginHanderInterceptor,实现HanderInterceptor接口,实现方法

    3. 首先,将登陆的正确信息放在HttpSession的对象中(setAttribute键值对的方式传值)

    4. 然后在拦截器的preHandle(目标方法执行之前)中进行预检查,逻辑是拿到session对象,然后检查域对象中时候有正确登陆的信息,如果没有就应该拦截,使其返回登陆界面,使用request.getRequestDistcher("").forward()转发到对应的页面,并且return false,最好还加上对应的错误消息,这个时候可以使用之前定义账号密码错误是使用的msg,直接给该msg加上信息,这样就可以实现返回页面显示“没有权限,请登录信息”

    5. 如果是登陆成功,就直接return true,进行放行

    6. 注意注意注意,拦截器写完之后一定要在springMVC中配置出来,也就是在之前说可以半接管(继承WebMvcConfigurationSupport的类)的类中重写addInterceptors(InterceptorRegistry registry)方法

    7. 此时要注意的是,需要以下内容

      <pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n152" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit;"> registry.addInterceptor(new LoginHanderInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");
      这句话的意思是,对LoginHanderInterceptor这个拦截器生效,并且所有种类的请求,但是不拦截后面三个(前面两个是访问登录界面的,第三个是点击登录的),对于静态资源的访问,springboot已经封装好了,不需要添加不拦截
      ​</pre>


  5. 实现RestfulCRUD

  6. 页面修改之后前端实时显示

  7. 开发的时候需要禁用模板引擎的缓存,在properties中的写法是“spring.thymeleaf.cache=false”,并且在更新页面之后在Idea中按住“Ctrl+F9”进行页面刷新,这样就可以在前端重新刷新页面

相关文章

网友评论

      本文标题:springBoot实现CRUD操作1

      本文链接:https://www.haomeiwen.com/subject/jizmnqtx.html