1、Idea项目创建
1、在磁盘上创建SpringMvc目录
2、通过File->open打开该目录
3、在目录内创建模块
单击Next
4、给项目增加WebApp目录
设置SDK
添加Web
选择添加的项目:
编辑路径:增加src\main,web修改成webSite
下方的Resource Directories相同的修改
点击Apply即可。
处理之后的目录结构会增加webSite目录,注意图标形态。
5、配置artifact打包方式
选择项目模块,并进行配置
2、Idea配置Tomcat
1、打开配置窗口
2、添加Tomcat
3、配置Tomcat
部署到项目的Artifice
5、配置Tomcat热部署
一定要通过Debug模式运行
2、相关文件配置
2.1、pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.rui</groupId> <artifactId>02QuickAnnotation</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>7</source> <target>7</target> </configuration> </plugin> <!--热部署编译方式--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> <dependencies> <!-- springMVC官方文档 --> <!-- https://www.w3cschool.cn/spring_mvc_documentation_linesh_translation --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-instrument</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>2.6.0</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--jdbcTemplate--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.3.0</version> </dependency> <!--spring测试--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.0</version> <scope>test</scope> </dependency> <!--阿里的数据源--> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.0</version> </dependency> <!--MySQL连接的依赖包--> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <!-- https://mybatis.org/mybatis-3/getting-started.html --> <!-- https://mybatis.org/mybatis-3/zh/index.html --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--mybatis Spring对接包 单独引入--> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!--单元测试--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!--日志输出--> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--JSON数据转换--> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations-java5</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> </project>
2.2、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置SpringMVC的编码格式 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--前端控制器--> <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.xml</param-value> </init-param> </servlet> <!--控制器映射--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!--默认首页--> <welcome-file-list> <welcome-file>index.action</welcome-file> </welcome-file-list> </web-app>
2.3、springmvc.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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--添加所用的处理器映射器--> <!--springMvc提供了很多的处理器映射器,这个映射器是把Bean的名称作为url进行查找 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!--添加所用的处理器适配器--> <!--处理器适配器也是有多个,该处理器支持所有实现了Controller接口的处理器(Handle)--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!--配置视图解析器,视图解析器用来解析处理器返回的ModelAndView对象--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--配置视图的默认前缀位置 --> <property name="prefix" value="/WEB-INF/jsp"></property> <!--配置视图文件的默认后缀--> <property name="suffix" value=".jsp"></property> </bean> <!--配置控制器--> <bean name="/index.action" class="rui.web.TestController" /> </beans>
3、测试例子 - 老技术启用
3.1、创建Controller
package rui.web; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import rui.db.Dao.ex_OrderDao; import rui.db.Model.ex_Order; import java.util.ArrayList; import java.util.List; /*测试控制器*/ public class TestController implements Controller { @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception { System.out.println("执行Controller"); List<ex_Order> orderList = new ArrayList<ex_Order>(); ex_Order item = new ex_Order(); item.setOrderId("T005"); item.setOrderDate(null); orderList.add(item); //创建返回的视图对象 ModelAndView viewResult = new ModelAndView(); //添加返回的数据 viewResult.addObject("orderList",orderList); //设定所用视图 - 自动匹配到/WEB-INF/jsp/test/index.jsp viewResult.setViewName("/test/index"); return viewResult; } }
3.2、创建视图
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2022/2/12 Time: 11:49 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <html> <head> <title>测试页面</title> </head> <body> <h3>订单列表</h3> <table style=" 300px;" > <tr> <td>订单编号1</td> <td>订单时间1</td> </tr> <c:forEach items="${orderList}" var="item"> <tr> <td>${item.orderId}</td> <td>${item.orderDate}</td> </tr> </c:forEach> </table> </body> </html>