最近学习spring,小白一个,把创建项目和配置的过程记录一下
准备工作
- 准备maven,配置maven的库路径,主要是修改.m2的repository路径和setting.xml的配置路径
- IDEA新建maven项目,不要勾选其他框架,默认就好,给项目起名
- 进入项目,右键add framework support,选择web4.0框架,生成WEB-INF路径
- 进入pom.xml,给maven添加依赖,注意IDEA右下角有个自动导入,勾选后导入会方便很多:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>com.kb</groupId>
<artifactId>springmvc1.0</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--springframework-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</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>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
- 进入web.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_4_0.xsd"
version="4.0">
<!--1.注册servlet-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--通过初始化参数指定SpringMVC配置文件的位置,进行关联-->
<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>
<!--所有请求都会被springmvc拦截 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 进入
/src/main/resources
下面,添加一个springmvc的配置文件,即web.xml里的配置文件: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
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.kb.controller"/>
<!-- 让Spring MVC不处理静态资源 -->
<mvc:default-servlet-handler />
<!--
支持mvc注解驱动
在spring中一般采用@RequestMapping注解来完成映射关系
要想使@RequestMapping注解生效
必须向上下文中注册DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter实例
这两个实例分别在类级别和方法级别处理。
而annotation-driven配置帮助我们自动完成上述两个实例的注入。
-->
<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>
注意最上边<context:component-scan base-package="com.kb.controller"/>
指定了扫描的包名,这里后面要创建的包要保持一致,可以后面再添加。
添加代码
- 添加类MyController:
package com.kb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController
{
@RequestMapping("/controller1")
public String controllerFunc1(Model m)
{
m.addAttribute("msg","hello controller 1");
return "test";
}
@RequestMapping("/controller2")
public String controllerFunc2(Model m)
{
m.addAttribute("msg","hello controller 2");
return "test";
}
@RequestMapping("/controller3")
public String controllerFunc3(Model m)
{
m.addAttribute("msg","hello controller 3");
return "test";
}
}
这里使用注解的方式,有三个方法可以被前端调用,分别为controllerFunc1
;controllerFunc2
;controllerFunc3
;URL分别为http://localhost:8080/controller1
;http://localhost:8080/controller2
;http://localhost:8080/controller3
。由于springmvc-servlet.xml
里配置了web访问的路径前缀包括jsp,因此在WEB-INFO路径下添加jsp文件夹,并创建test.jsp文件。
<%--
Created by IntelliJ IDEA.
User: KingBoy
Date: 2021/11/14
Time: 19:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
部署
-
到这里项目部分完成。配置部署环境。打开configuration,配置Tomcat路径,不要用太新的Tomcat,7 8 什么的都可以。deployment里添加项目。
-
configuration右边有个小文件夹按钮,即project structure配置,进到artifact里,在WEB-INFO里面添加一个lib文件夹,将maven的库都添加,否则会有404错误。
-
启动项目。OK