一、web.xml配置DispatcherServlet
<?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"
metadata-complete="true">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
<init-param>
<!-- 配置地址解析器-->
<param-name>contextConfigLocation</param-name>
<!-- 绑定springmvc-servlet.xml地址-->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别-1-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二、配置springmvc-servlet.xml配置文件
- 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
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.wei.controller"></context:component-scan>
<!-- 让springmvc不处理静态资源-->
<mvc:default-servlet-handler/>
<!--支持mvc注解驱动,
在springmvc中,一般采用@RequestMapping注解来完成映射关系
想使得@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping(处理器映射器)
和一个AnnotationMethodHandlerMapping(处理器适配器)实例
这两个实例分别在类级别和方法级别处理
而Annotion-driven配置会帮助我们自动完成上述两个实例的注入-->
<mvc:annotation-driven/>
<!--视图解析器:DispatcherServlet给他的ModelAndView-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
三、创建对应的控制类,并完成对应
方法一:@RequestMapping写在方法上
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class helloController {
@RequestMapping("/hello") //让hello页面请求,会从hello页面自动跳到这个方法
public String hello(Model model){
//封装数据
model.addAttribute("msg","hellow SpringMvc");
return "hello"; //返回视图页面,会被视图解析器处理,/WEB-INF/jsp/hello.jsp
}
}
方法二:@RequestMapping写在类上
package com.wei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test") //在类上加RequestMapping,会与下面的方法形成父子关系
public class TestController {
@RequestMapping("/h1") // localhost:8080/test/h1
public String Test(Model model){
//封装数据
model.addAttribute("msg","测试");
return "test"; //返回视图页面,会被视图解析器处理,/WEB-INF/jsp/test.jsp
}
}