<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.10.RELEASE</version> </dependency>
3)创建controller包 创建一个HelloController.java @Controller 注解方式在IOC容器创建bean @RequestMapping("/hello")注解映射请求路径 RequestMapping注解使用参考文章https://blog.csdn.net/zalan01408980/article/details/82904126
package com.zb.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") public String haha(){ System.out.println("Controller"); //指定到WEB-INF/jsp下的success.jsp文件 return "success"; } }
4)创建resource包创建一个spring-mvc.xml配置文件 配置组件扫描 指定要扫描的包 包里的所有已经注解的类和方法都会被在IOC容器内创建bean对象 视图解析器 参考文章:https://blog.csdn.net/weixin_40277684/article/details/100596257
<?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"> <!-- 组件扫描 扫描注解--> <context:component-scan base-package="com.zb.controller"/> <!-- 视图解析器 resourceView 在Controller注解的类的方法中收到返回值 再将返回值与前缀prefix的值WEB-INF/jsp/拼接 与suffix的值.jsp拼接 --> <bean id="resourceView" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:annotation-driven/> </beans>
5)配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <!--dispatcherServlet前置控制器 MVC C层核心控制器--> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- dipatcherServlet加载springIOC容器--> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <!--1指在web容器加载时 就加载dispatcherServlet--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <!--/ 表示dispatcherServlet拦截所有用户请求url 除了后缀为.jsp的文件url--> <!-- /* 表示拦截所有文件url--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
6)创建View视图层(即jsp) 创建 WEB-INF/jsp/success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>success</title> </head> <body> <h1>hello world!!!!haha</h1> </body> </html>index.jsp
<html> <body> <a href="hello"> <h2>Hello World!</h2></a> </body> </html>
7)部署tomcat服务器 测试