下面我们用Intellij IDEA创建一个SpringMVC项目。
- 打开IDE,new一个新的project
选择Maven项目,并且勾中右边的Create from archetype,在下边的列表选择-archetype-webapp,然后next。。。
输入GroupId和ArtifactId,然后next
接下来一路next,项目就创建好了,建好后我们在main下新建一个java包,目录结构如下图
接下来看下我们默认生成的pom.xml
- 1.修改pom.xml,加入一些我们自己需要的东西
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sdl.springmvc</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--加入一些变量,替代我们需要用到的一些工程版本。这样在我们整个工程如果依赖比较复杂,工程比较大的情况下会比较清晰的
了解到我们所使用的一些依赖软件包的版本-->
<properties>
<commons-lang-version>2.6</commons-lang-version>
<slf4j.version>1.7.6</slf4j.version>
<spring.version>4.1.3.RELEASE</spring.version>
<jstl.version>1.2.1</jstl.version>
<jackson.version>2.5.4</jackson.version>
</properties>
<!--依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>springmvc</finalName>
</build>
</project>
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Spring MVC Study</display-name>
<!--Spring应用上下文,理解层次化的ApplicationContext-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/appicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--DispatcherServlet,Spring MVC的核心-->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet对应的上下文配置,默认为WEB-INF/$servlet-name$-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<!--mvc-dispatcher拦截所有的请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
别忘了在/WEB-INF/下建立对应的文件夹和文件哦,此时的目录结构
接下来,让我们配置applicationContext.xml和mvc-dispatcher-servlet.xml
applicationContext.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">
<context:annotation-config />
<context:component-scan base-package="com.sdl.mvcdemo">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
mvc-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">
<!--本配置文件是工名为mvc-dispatcher的DispatcherServlet使用,提供其相关的Spring MVC配置-->
<!--启用Spring基于annotation的DI,使用户可以在Spring MVC中使用Spring的强大功能。
激活@Required @Autowired,JSR 250's @PostConstruct, @PreDestory and @Resource 等标注-->
<context:annotation-config/>
<!--DispatcherServlet上下文,只管理@Controller类型的bean,忽略其他类型的bean,如@Service-->
<context:component-scan base-package="com.sdl.mvcdemo">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--HandlerMapping,无需配置,Spring MVC可以默认启动叫做DefaultAnnotationHandlerMapping的类,
这个类可以为我们解析我们基于一些annotation-driven 的 HandlerMapping-->
<!--扩充了注解驱动,可以将请求参数绑定到控制器参数-->
<mvc:annotation-driven/>
<!--静态资源处理-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!--配置ViewResolver。
可以用多个VireResolver。
使用order属性排序。
InternalResourceViewResolver放在最后,因为他必定会返回一个对象,这个对象可能不是我们所需要的。-->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
<entry key="html" value="text.html"/>
</map>
</property>
<property name="defaultViews">
<list>
<!--JSON view-->
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--200*1024*1024即200M resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常-->
<bean id="multiparResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="209715200"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="resolveLazily" value="true"/>
</bean>
</beans>
此时项目构架已基本完成,下面让我们写个controller看有没有问题
package com.sdl.mvcdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello")
public class HelloMvcController {
@RequestMapping("/mvc")
//http://localhost:8080/hello/mvc
public String helloMvc() {
return "hello";
}
}
jsp代码–>hello.jsp
<%@ page contentType="text/html;charset =UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Hello Spring Mvc!
</body>
</html>
直接tomcat启动,http://localhost:8080/hello/mvc
结果出来了,是我们想要的“Hello Spring Mvc!”是不是感觉很亲切。
嗯,睡觉吧,如果哪里有不对的地方,欢迎大家指正,谢谢。