JAVA Spring学习笔记------MVC

SpingMVC

首先我没接触过页面开发

这里只简单的介绍如何通过spring 利用注解的形式搭建一个简单的页面

首先我们配置pom文件

先加入两个依赖

复制代码
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.10.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
复制代码

请注意spring-webmvc的版本和JDK的兼容性

我原来使用的是spring-webmvc 5.3.x和JDK18的组合

结果寄了,好像是版本不兼容的问题

后来成功的版本组合为spring-webmvc 5.2.10 和 JDK1.8的组合

配置完pom之后

再创建对应的Controller类

复制代码
//利用controller 象声明为Bean
@Controller
public class UserController {
    //设置当前的访问路径
    @RequestMapping("/test")
    @ResponseBody
    //设置当前操作的返回值类型
    public String save(){
        System.out.println("save test");
        return "hello";
    }
}
复制代码

首先我们要交给Spring管理,那么肯定要声明Bean

其次就是Request和Response请求

然后在创建Sping配置类和Servlet配置类

Sping配置类和以前一样

@Configuration
@ComponentScan("com.demo")
public class SpringMvcConfig {
}

然后就是Servlet配置类

复制代码
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {

    //加载SpirngMVC容器配置
    protected WebApplicationContext createServletApplicationContext() {
        System.out.println("ok捏");
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(SpringMvcConfig.class);
        return applicationContext;
    }
    //设置哪些请求归属SpringMVC处理
    protected String[] getServletMappings() {
return new String[]{"/"};
    }
    //加载Spring容器配置
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}
复制代码

Servlet一定要继承AbstractDispatcherServletInitializer类

最后还要在pom中配置Tomcat插件

复制代码
<build>
    <finalName>SpringMVC_Test3</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <port>5476</port>
          <path>/</path>
          <packaging>war</packaging>
        </configuration>
      </plugin>
    </plugins>
  </build>
复制代码

然后启动tomcat服务器

利用开启的端口以及设置好的访问路径就可以访问了

 

更进一步

在一个项目中SpringMVC应该只负责Control层的业务,其余层的结构应该交给Spring来管理

那么servelt配置中

我们有一个方法createRootApplicationContext()返回了null

这个方法就是用来配置spring的

protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(SpringConfig.class);
        return applicationContext;
    }

更进一步简化

我们直接继承 AbstractAnnotationConfigDispatcherServletInitializer然后实现里边的方法

复制代码
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    //加载SpirngMVC容器配置
//    protected WebApplicationContext createServletApplicationContext() {
//        System.out.println("ok捏");
//        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//        applicationContext.register(SpringMvcConfig.class);
//        return applicationContext;
//    }

    @Override
    //加载spring容器配置
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    //加载SpirngMVC容器配置
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    //设置哪些请求归属SpringMVC处理
    protected String[] getServletMappings() {

        System.out.println("拦截?");
        return new String[]{"/"};
    }
    //加载Spring容器配置
//    protected WebApplicationContext createRootApplicationContext() {
//        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//        applicationContext.register(SpringConfig.class);
//        return applicationContext;
//    }
}
复制代码

 

posted @   zzRh_5479  阅读(130)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示