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 @ 2022-10-09 11:25  zzRh_5479  阅读(124)  评论(0编辑  收藏  举报