💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/springmvc_/com-learn-springmvc01 **** **1. 创建一个Maven项目** ![](https://img.kancloud.cn/6d/70/6d70726ec6600db1e253432da6a125cd_1507x538.jpg) **2. 补全目录结构** ``` |—— 项目名 | |—— src | | |—— main | | | |—— java | | | |—— resources | | | |—— webapp | | | | |—— statics #存放js、css、图片等静态资源 | | | | |—— WEB-INF | | | | | |—— views #存储jsp文件 | | | | | |—— web.xml #servlet核心配置文件 | | | | |—— index.jsp #默认的首页 | |—— pom.xml ``` **3. 配置Tomcat服务器** ![](https://img.kancloud.cn/d5/d2/d5d28e74e3650c7f3ddc103f7716c376_1456x440.jpg) ![](https://img.kancloud.cn/9f/c5/9fc5e9b522036f526a13fd08aa33f045_1444x276.jpg) ![](https://img.kancloud.cn/d7/1f/d71f8fec21a7eac0a0a0c0938fb90fd4_1334x206.jpg) ![](https://img.kancloud.cn/86/e2/86e2d9c6037f71126d3bea0bfbde15dc_1337x747.jpg) Application context为Web项目访问的跟路径,可自由定义。 **4. `pom.xml`** ```xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring.version>5.2.9.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> ``` **5. 配置文件** (1)Spring核心配置文件`resources/applicationContext.xml`。 ```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" 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"> </beans> ``` (2)SpringMVC核心配置文件`resources/springmvc-servlet.xml`。 ```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 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 启用spring的注解配置 --> <context:annotation-config/> <!-- 配置注解驱动 --> <mvc:annotation-driven/> <!-- 自动扫描并装配,可以扫描当前包及子包 --> <context:component-scan base-package="com.learn.springmvc01"/> <!-- 配置JSP视图解析器 --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <!-- prefix和suffix共同决定jsp文件的位置 --> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <property name="exposeContextBeansAsAttributes" value="true"/> </bean> </beans> ``` (3)Servlet核心配置文件`web.xml`。 ```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_3_1.xsd" version="3.1"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置DispatchServlet核心控制器 --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--引入springmvc-servlet.xml配置文件--> <param-name>contextConfigLocation</param-name> <param-value>classpath:/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 引入applicationContext.xml配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/applicationContext.xml</param-value> </context-param> <listener> <!-- ContextLoaderListener组件用来扫描applicationContext.xml文件 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> ``` **6. 创建视图文件`webapp/WEB-INF/views/welcome.jsp`** ```html <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WELCOME</title> </head> <body> <h1>Welcome, Spring MVC</h1> </body> </html> ``` **7. controller层** ```java @Controller public class IndexController { @RequestMapping("/main") public String index() { return "main"; } } ``` **8. 测试** 启动项目,访问 http://localhost:8080/mvc/main ,显示页面如下。 ![](https://img.kancloud.cn/92/15/9215487a37963588a4bf03a533ec6dc7_1510x301.jpg)