初级SpringMVC入门

一:认识SpringMVC:

SpringMVC是一个基于JAVA来实现MVC(Model-View-Controller)设计模式的请求驱动类型的轻量级Web框架。

二、SpringMVC采用的技术以及每个技术对应的作用。

1、技术:JSP(视图)+Servlet(控制器)+JavaBean(模型)。

2、作用:Servlet首先接收浏览器发送的请求,然后根据请求信息实例化。JavaBean对象用于封装操作数据库后返回的数据,最后选择相应的JSP页面将响应的结果显示在浏览器上。

三、在IDEA创建一个SpringMvc项目的基本配置

1、新建一个项目

选择webapp

2、配置pom.xml

<!--dependencies中的基本配置-->
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <!--引入springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.1.5</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
        </dependency>
    </dependencies>
<!--tomcat的基本配置-->
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
</build>

 3、在resource目录下新建springmvc.xml

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd ">
    <!--开启controller扫描-->
    <!--配置springmvc视图解析器-->
   <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>-->
    <!--处理器引射器,处理器适配器-->
    <!--注册最合适的处理器适配器-->
    <context:component-scan base-package="com.gcxy.controller"/>
    <mvc:annotation-driven/>
</beans>

 4、在使用SpringMvc时常用的注解

@RequestMapping:用于处理请求url映射的注解,可以用于类或者方法上。用于类上表示所有响应请求的方法都是以该地址作为父路径。

@RequestBody:主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。

@ResponseBody:将Controller的方法返回的对象,通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML数据。

@RequestParam:将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)。其中value是默认属性,用于指定前段传入的参数名称;required用于指定参数是否必传;defaultValue指当前参数为非必传参且前端没有传入参数时,指定一个默认值。

@ExceptionHandler:是标注在立场处理类中的方法,表示该方法可以处理异常类型。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值