IDEA基于maven创建SpringMVC(一)
本人主要是.net web开发最近为了丰富自己的知识,开始学习Java web开发,看了许多的博客学习,现在做了一个学习记录,有什么不对的地方欢迎指出。
一、环境
jdk:1.8
idea:2019.3.3
maven:3.6.3
二、搭建
1、创建maven项目(注:选择maven-archetype-webapp)点击next
2、填写项目名称,点击next
3、配置maven,查看选择maven版本、当前maven的配置文件setting.xml以及当前本地仓库位置配置情况。maven配置正确,点击Finish
4、点开pom.xml,插入需要jar包的maven坐标
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--Spring Mvc核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<!--servlet相关API,因为servlet容器中有此包,scope采用provided打包时不在打入此包-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--aspectj aop切面需要-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<!--springMVC数据库访问核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
5、右键项目,点击add Framework Support
6、选择spring MVC 点击ok
注:若是没有 点击左上角File中选择Project Structure
添加成功后
7、在 main文件夹下创建文件夹名称 java 标记为 Sources 及其 一些子包 , 创建文件夹resources标记为 Resources 在src下创建test文件夹标记为Tests等,完成后如图
8、打开web.xml 配置,建议自己先梳理一下意思,可以直接copy
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<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 page-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
9、配置dispatcher-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
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--启用spring注解-->
<context:annotation-config/>
<!--配置扫描包-->
<context:component-scan base-package="simple"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<mvc:resources mapping="/js/**" location="/statics/js/"/>
<mvc:resources mapping="/css/**" location="/statics/css/"/>
<!--访问静态资源-->
<mvc:default-servlet-handler/>
<!--视图解析器-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
10、在controller包下创建HomeController.java文件
package simple.comtroller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* ClassName:HomeController
* Package:simple.comtroller
* Description:
*
* @Date:2020/3/28 16:10
* @Author:JZK
*/
@Controller
@RequestMapping("/home")
public class HomeController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
11、在views文件夹下创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SpringMVC-Maven</title>
</head>
<body>
<h1>SpringMVC-Maven</h1>
</body>
</html>
12、配置tomcat
个人习惯修改:
13、点击启动tomcat服务,启动完成输入地址:http://localhost:8080/Spring_Maven_First/home/index 这样就配置成功了
总结:上述是自己创建中一点截图搞的,可能有点啰嗦。有什么不对欢迎指出,后续继续添加内容,入门 创建先写到这里