springmvc通过注解和配置类搭建

一、springmvc项目搭建

1、手动部署webapp项目

<groupId>org.example</groupId>
<artifactId>spring-transaction</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>主要是这一行

2、引入jar包

<!-- mvc核心包 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.18</version>
</dependency>
<!-- 日志slf4j的门面实现包 -->
 <dependency>
     <groupId>ch.qos.logback</groupId>
     <artifactId>logback-classic</artifactId>
     <version>1.3.0-alpha14</version>
 </dependency>
<!-- 日志slf4j的门面实现包 -->
servlet使用api包
<dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>4.0.1</version>
     <scope>provided</scope>
</dependency>
<!-- 日志slf4j的门面实现包 -->
thymelea和spring5的整合包
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.12.RELEASE</version>
</dependency>
<!--导入转化返回值为json字符串的jar包-->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

3.创建Controller类

@Controller //使用@Controller将该类标记为控制层bean
public class UserController {

    @RequestMapping("/save.do") //定义访问标识符,类似Servlet中@WebServlet()的功能
    @ResponseBody //确认返回给浏览器的值为该方法的返回的内容
    public String save(){
        System.out.println("use...save");
        return "{'model','springmvc'}";
    }
}

4.创建springMVC的配置类

@Configuration
@ComponentScan("com.frt.control")
@EnableWebMvc
public class SpringMvcConfig {
}

5.定义spring容器类,在其中加载spring的配置

/*
此过程最主要的操作就是将自己创建的类继承 AbstractDispatcherServletInitializer类,
然后重写createServletApplicationContext()方法
并在其中创建AnnotationConfigWebApplicationContext对象
调用创建的对象中的register(SpringMvcConfig.class);去加载配置类*/

public class ServletContainerInitConfig extends AbstractDispatcherServletInitializer {
    @Override //加载springMVC容器(createServletApplicationContext())
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext applicationContext=new               AnnotationConfigWebApplicationContext();  
        applicationContext.register(SpringMvcConfig.class);
        return applicationContext;
    }

    @Override 
    protected String[] getServletMappings() {
        return new String[]{"/"};//在这个位置确定哪些请求被servlet处理
    }

    @Override //加载spring容器(createRootApplicationContext())
    protected WebApplicationContext createRootApplicationContext() {
         AnnotationConfigWebApplicationContext applicationContext=new               AnnotationConfigWebApplicationContext();  
        applicationContext.register(SpringConfig.class);
        return applicationContext;
    }
   
     @Override //中文字符集过滤和设置方法
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter=new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

或者

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

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

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值