欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC入门使用注解

程序员文章站 2022-07-15 11:10:26
...

SpringMVC使用注解开发

需要知道的几个注解

  • @Controller : 表示这是一个组件(Controller) 注册到spring容器中,他还有几个相似的注解(@Service @Component @Repository)都表示是一个组件
  • @RequestMapping : 获取所有请求
  • @GetMapping: 获取Get请求
  • @PostMapping:获取Post请求

下面就以一个Hello 程序例子来入门注解开发

  1. 第一步 检查包是否导入 需要的依赖如下
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
  1. 第二步 在web.xml文件中配置DispatcherServlet
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
  1. 第三步 配置我们的springMVC配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 扫描包 该包下的注解生效-->
    <context:component-scan base-package="com.yxx.controller"/>
    <!-- 开启注解驱动
    并且 这个会为我们自动配置映射器和适配器
    RequestMappingHandlerMapping和RequestMappingHandlerAdapter
    -->
    <mvc:annotation-driven/>

    <!--配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  1. 第四步 写Controller
@Controller
public class MyController {

    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","Hello!!!!");
        return "hello";//  /WEB-INF/jsp/hello.jsp
    }
}

注意的点:使用注解 ,我们需要扫描使用注解类的包即要在springMVC的配置文件中如下配置

<!-- 扫描包 该包下的注解生效-->
<context:component-scan base-package="com.yxx.controller"/>