扩展SpringMVC(SpringBoot)

MVC配置原理

//如果我们要扩展Springmvc,官方建议我们这样去做
@Configuration//配置类
//@EnableWebMvc  //导入了一个类  DelegatingWebMvcConfiguration.class:从容器中获取所有的webmvcconfig;
public class MyConfig_02 implements WebMvcConfigurer {
    //视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/kuang").setViewName("index");
    }
}

扩展springmvc

1.首页配置

1. 所有静态页面都需要thymeleaf接管
2. url:@{}

2.页面国际化

  1. 需要配置i18n文件

在这里插入图片描述

  1. 如果需要在项目中进行按钮自动转换,我们需要自定义一个组件LocaleResolver

    public class MyLocaleResolver implements LocaleResolver {
    
        //解析请求
        @Override
        public Locale resolveLocale(HttpServletRequest httpServletRequest) {
            //获取请求中的语言参数
            String language = httpServletRequest.getParameter("l");
    
            Locale locale = Locale.getDefault();//如果没有就使用默认的
            //如果请求的链接携带了国际化参数
            if(!StringUtils.isEmpty(language)){
                //zh_CN
                String[] s = language.split("_");
                //国家  地区
               locale= new Locale(s[0],s[1]);
            }
            return locale;
        }
    
        @Override
        public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
    
        }
    }
    
  2. 记得将自己写的组件配置到spring容器中@Bean

    @Configuration
    public class MyConfig implements WebMvcConfigurer {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("login");
            registry.addViewController("/login.html").setViewName("login");
        }
    
        //自定义的国际化组件就生效了
        @Bean
        public LocaleResolver localeResolver(){
            return new MyLocaleResolver();
        }
    
    
    }
    
  3. #{}

在这里插入图片描述

3.登录+拦截器

登录Controller

@Controller
public class LoginController {
    @RequestMapping("/user/login")
    //@ResponseBody
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password")String password,
            Model model, HttpSession httpSession){

        //具体的业务
        if(!StringUtils.isEmpty(username)&& "123456".equals(password)){
           httpSession.setAttribute("loginUser",username);
            // return "index";
            return "redirect:/main.html";
        }else{
            model.addAttribute("msg","登录失败!!!");
            return "redirect:/login.html";
        }
    }
}

拦截器

public class LoginHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录成功后,应该有用户的session
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            request.setAttribute("msg", "没有权限,请先登录");
            request.getRequestDispatcher("/login.html").forward(request, response);
            return false;
        } else {
            return true;
        }
    }
}

4.员工列表展示

  1. 提取公共页面

    1. th:fragment=“top” (这个top可以是任意的参数XXX)

    2. th:replace="~{commons/commons::top}"

    3. 如果要使用参数可以使用()传参,接收判断即可

      传参:(index.html)

      <div th:replace="~{commons/commons::xxx(active='list.html')}"></div>
      

      接收判断:(commons.html)

      <li>
          <a th:class="${active=='list.html'?'nav-link active':'nav-link'}" th:href="@{/emps}">
              <span>员工管理</span>
          </a>
      </li>
      
  2. 列表循环展示

    <tr th:each="emp:${emps}">
        <td th:text="${emp.getId()}"></td>
        <td >[[${emp.getLastName()}]]</td>
        <td th:text="${emp.getEmail()}"></td>
        <td th:text="${emp.getGender()==0?'女':'男'}"></td>
        <td th:text="${emp.department.getDepartmentName()}"></td>
        <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
    
        <td>
            <button class="btn btn-sm btn-primary">编辑</button>
            <button class="btn btn-sm btn-danger">删除</button>
        </td>
    </tr>
    

    5.添加员工

    6.修改员工

    7.删除员工

Spring MVC、Spring Boot和Spring Cloud是Spring Framework下的三个重要项目,它们各自有不同的用途和功能。 1. Spring MVC: - Spring MVC是基于Spring Framework的一个MVC(Model-View-Controller)框架,用于构建Web应用程序。 - 它提供了一个灵活的、可扩展的架构,用于处理用户请求和生成响应。 - Spring MVC提供了很多用于处理请求映射、数据绑定、视图渲染等的注解和类。 2. Spring Boot: - Spring Boot是用于简化Spring应用程序开发的框架。 - 它提供了自动配置、快速启动和约定优于配置的特性,使得开发人员能够快速搭建和部署基于Spring的应用程序。 - Spring Boot还集成了嵌入式Web服务器(如Tomcat、Jetty),简化了Web应用程序的部署过程。 - 它还提供了诸如安全性、监控、健康检查等常见功能的开箱即用解决方案。 3. Spring Cloud: - Spring Cloud是用于构建分布式系统和微服务架构的框架。 - 它基于Spring Boot,提供了一系列工具和库,用于解决分布式系统中的常见问题,如服务注册与发现、负载均衡、服务调用、配置管理等。 - Spring Cloud还集成了一些流行的分布式系统组件,如Netflix的Eureka、Ribbon、Hystrix等。 总结: Spring MVC用于构建Web应用程序,Spring Boot用于简化Spring应用程序的开发和部署,Spring Cloud用于构建分布式系统和微服务架构。它们各自有不同的定位和功能,但可以相互配合使用,提供更强大的开发能力和更高效的系统架构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值