用注解的方式实现Spring MVC
1 创建项目
Idea创建web项目
2 导入依赖
导入依赖,并且配置build标签,防止.xml文件无法被加载
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
3 配置web.xml
在WEB-INF文件下的web.xml中进行如下操作:
1 注册DispatcherServlet
2 关联SpringMVC配置文件
3 启动级别设置为1
4 映射路径为 / (不能写 /* 否则会将WEB-INF中所有的.jsp文件加载进来)
<?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">
<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-servlet.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>
</web-app>
4 编写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
http://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.xzzlx.controller"/>
<mvc:default-servlet-handler/>
<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>
5 写控制类
实现对应的控制类
@controller 标注该类是一个Spring托管的类
@RequestMapping 该方法的访问路径
package com.xzzlx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
model.addAttribute("msg", "你好,SpringMVC Annotation!");
return "hello";
}
}
6 完善前端视图和controller之间的对应
在WEB-INF下创建jsp文件夹,里面创建hello.jsp视图
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>AnnotationSpringMVC</title>
</head>
<body>
${msg}
</body>
</html>
7 测试运行
启动Tomcat测试项目
在注解实现MVC中,我们实现视图解析就可以了,<mvc:annotation-driven/> 帮我们做了Url处理映射器和处理适配器,省去了大量的xml配置。
有问题可以评论一起讨论~~~
|