SpringMvc 从零开始(一)——web工程创建

    Javaweb开发有几年了,但是一直也没有自己动手搭建一个springmvc的框架。虽然springboot为开发者提供了巨大的便利,反而激起了我动手搭建springmvc框架的兴趣。

---------言归正传-----------

准备(配置工作不做详细说明了)

1、jdk1.8

2、eclipse

3、maven3.5

4、tomcat7.0


自己动手,丰衣足食。

1、eclipse中创建maven工程

右键新建->other->maven


->Next


->Next

填写坐标(GroupId、ArtfactId),packaging选择war(创建为web工程)


->Finish

生成的目录结构如下(WebContent为后来创建的)



2、创建WebContent目录

右键->properties->project facets

Further Configuration available选项没有显示的话,将Dynamic Web Module勾掉,点击apply,再勾选Dynamic Web Module,点击apply。该选项就会显示。


点击Further Configuration available


勾选Generate web.xml选项->OK

生成的目录结构如下。



3、添加pom依赖(本次只添加springmvc必须的依赖,springmvc其他功能的依赖之后陆续添加)

<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.xufq</groupId>
	<artifactId>springmvc-login</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<spring-version>5.0.3.RELEASE</spring-version>
		<commons-lang-version>2.1</commons-lang-version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</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-webmvc</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-beans</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring-version}</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>${commons-lang-version}</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

4、编辑web.xml,进行项目配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="springmvc-login" version="3.0">
  <display-name>springmvc-login</display-name>
  <welcome-file-list>
    <welcome-file>/WEB-INF/webpage/page/login.html</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/springmvc-login-context.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>springmvc-login</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/springmvc-login-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc-login</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

<display-name>springmvc-login</display-name>:项目名称

<welcome-file>/WEB-INF/webpage/page/login.html</welcome-file>:欢迎页面

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>:父容器加载(一般会加载整个Spring容器相关的bean配置管理,如Service, Dao)

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>:子容器加载(一般会加载MVC相关的bean配置管理,如Controller)

<url-pattern>/</url-pattern>:servlet拦截的url规则

5、编辑springmvc-login-servlet.xml,配置springmvc的子容器(采用注解方式,提供了配置的便利)

<?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-3.0.xsd">

	<context:component-scan base-package="com.xufq" />
	<mvc:default-servlet-handler/>
	<mvc:annotation-driven />
</beans>

<context:component-scan base-package="com.xufq" />:扫描注解的根目录(项目中暂时只有Controller,所以没有区分Controller和Service等的目录,后续会调整)

<mvc:default-servlet-handler/>:静态资源解析器(避免dispatcherServlet拦截静态资源,欢迎页如果是静态资源也会被拦截。)

<mvc:annotation-driven />:<mvc:annotation-driven>会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,这是Spring MVC为@Controller分发请求所必需的,并且提供了数据绑定支持


6、添加欢迎页



7、将项目发布到tomcat,并启动



8、访问项目

http://localhost:8080/springmvc-login




Springmvc框架初步搭建完毕,后续会逐步完善框架内容。

发布了11 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wddbq/article/details/79319435
今日推荐