开发工具是idea
开始先创建一个maven项目,因为想做一个小说网站,所以就需要设置一些属性:
groupId(项目的组名,方便管理代码):top.lemon.fiction(我是用自己的域名倒序加上一个标识字段,这是个人习惯不一定正确)
artifacted(maven项目的名称):fiction-web
创建完后就要引入jar包了:
初始版本是这样的(后面会陆续加上其他框架)
<?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>top.lemon.fiction</groupId>
<artifactId>fiction-web</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.6.RELEASE</spring.version>
<dubbo.version>2.5.3</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<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>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
这其中也有一些非必要的jar包,你可以从网上详细了解每个模块的功能后,自己挑选后引入自己的项目中。
idea为了将maven项目变成web项目需要进行些操作:
进入后勾选WebApplication确定,这样会在项目中新增一个名为web的文件夹,其中就有用于web项目配置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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>fiction-name</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fiction-name</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这里涉及两个类:
ContextLoaderListener 在启动Web容器时,自动装配Spring applicationContext.xml的配置信息,默认会加载WEB-INF下的springContext.xml,如果在context-param中定义了contextConfigLocation,也会根据它的值来加载配置文件。
DispatcherServlet springMVC的关键类,你可以理解为一个入口,它将对servlet的调用,通过路径分配到spring容器中的带springMVC注解或在xml配置的类上的方法中
现在需要配置springMvc了,我的spring-mvc.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">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
InternalResourceViewResolver用于将用RequestMapping注解的方法返回的字符串,再包装然后映射到对应目录下jsp文件。
这时一个简单项目的雏形已经产生了,但加了web支持的maven项目变成能在tomcat上运行的web项目还有一段路要走
首先需要配置idea的运行参数
然后在tomcat配置运行时启动的项目
这里你需要编辑运行在tomcat上的项目结构
比如需要创建lib,并将maven引入的jar包导入其中
而"fiction-web" module:"web" facet resources 则会将项目中web-info里的本地内容加入到tomcat里web-inf目录,比如一些css样式表、js脚本这类的静态内容
最后写个HelloController
一个简单地hello服务器就搭建出来了