SpringMVC-02

使用注解开发

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--1.注册servlet-->
   
<servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
 
<init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springmvc-servlet.xml</param-value>
   </init-param>
        <!-- 启动顺序,数字越小,启动越早 -->
       
<load-on-startup>1</load-on-startup>
    </servlet>
    <!--所有请求都会被springmvc拦截 -->
   
<servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-servlet.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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 自动扫描包,让指定包下的注解生效,IOC容器统一管理 -->
   
<context:component-scan base-package="com.study.annotation"/>
    <!-- Spring MVC不处理静态资源  .js  .css  .html -->
   
<mvc:default-servlet-handler />
    <!--支持mvc注解驱动
        在
spring中一般采用@RequestMapping注解来完成映射关系
        要想使
@RequestMapping注解生效
        必须向上下文中注册
DefaultAnnotationHandlerMapping
       
和一个AnnotationMethodHandlerAdapter实例
        这两个实例分别在类级别和方法级别处理。
        而
annotation-driven配置帮助我们自动完成上述两个实例的注入。
    
-->
   
<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>

注解测试:

@Controller
public class HelloAnno {
    @RequestMapping("/hello")
    public String SayHello(Model model){
        model.addAttribute("msg","Hello,SpringMVC注解");
        return "hello";
    }
}

小结

实现步骤其实非常的简单:

新建一个web项目

导入相关jar包

编写web.xml , 注册DispatcherServlet

编写springmvc配置文件

接下来就是去创建对应的控制类 , controller

最后完善前端视图和controller之间的对应

测试运行调试.

使用springMVC必须配置的三大件:

处理器映射器、处理器适配器、视图解析器

通常,我们只需要手动配置视图解析器,而处理器映射器和处理器适配器只需要开启注解驱动即可,而省去了大段的xml配置

推荐使用注解开发.

 

视图是被复用的,而控制器与视图之间是弱偶合关系。

@RequestMapping详解

可以写在类上,也可以写在方法上

写下类上即为父名,再访问路径需要先写父类再来写方法上的,即:

http://localhost:8080/SpringMVC_04/t/t1

 

 

 

405:

 

posted @ 2021-09-04 14:26  回眸23  阅读(17)  评论(0)    收藏  举报