SpringMVC注解方式配置

一、SpringMVC简介

1、SpringMVC中重要组件

    1.1  DispatcheServlet:前端控制器,接收所有请求(如果配置/不包含jsp);

    1.2  HandlerMapping:解析请求格式的。判断希望要执行那个具体的方法;

    1.3  HandlerAdapter:负责调用具体的方法;

    1.4  ViewResovler:视图解析器。解析结果,准备跳转到具体的物理视图。

2、SpingMVC运行原理图

SpringMVC注解方式配置

二、SpringMVC环境搭建

1、导入jar

SpringMVC注解方式配置

2、在web.xml中配置前端控制器DispatcherServlet、字符编码过滤器CharacterEncodingFilter

<!-- 配置前端控制器 -->
    <servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 

<!-- 字符编码过滤器 -->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

3、在src下新建springmvc.xml,引入xmlns:mvc命名空间

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 扫描注解 -->
    <context:component-scan base-package="com.gzz.controller"></context:component-scan>
    
    <!-- 注解驱动 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 静态资源 -->
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
    
    <!-- 自定义视图解析器 -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=""></property>
    </bean>
</beans>

 

4、新建jsp文件:index.jsp,main.jsp(位置为WEB-INF)

index.jsp:

<body>
    <form action="demo" method="post">
        <input type="text" name="name">
        <input type="text" name="age">
    </form>
</body>

main.jsp

<body>
跳转成功
</body>

5、编写控制器类(在src下建包名为com.gzz.controller,在包下建类名为DemoController)

@Controller
public class DemoController {
    
    @RequestMapping("demo")
    public String demo(String name,int age){
        return "main.jsp";
    }

}