文章目录
1、创建spring项目
- 根据1,2,3,4顺序进行操作
2、输入项目名称
3、确定maven配置
- 在maven配置文件中设置国内阿里云镜像
<!-- settings.xml -->
...
<mirrors>
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
...
4、确定项目路径
5、生成项目结构
6、添加Springmvc框架支持
- 在项目目录上点击鼠标右键,并在弹出菜单中选择"Add Framework Support"
- 勾选Spring MVC ,并下载jar包
- 添加springmvc框架后,在WEB-INF目录下生成了以下两个文件:
- applicationContext.xml及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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
7、认识web.xml
- SpringMVC框架的web应用,仍然是一个遵循servlet规范的web应用程序。
- 但在SpringMVC框架的web.xml中,会出现一些新的元素。
<!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>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- ContextLoaderListener的作用就是启动Web容器时,
读取在contextConfigLocation中定义的xml文件,
自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象
比如:数据源(DataSource)定义,服务对象(Services)定义等-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--DispatcherServlet是Spring MVC框架Web应用程序的Front Controller,
负责几乎所有对应当前Web应用程序的Web请求的处理 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
关于DispatcherServlet的工作原理,请浏览该文章:Spring完整揭秘(十一):通过三张图认识Spring MVC的架构
8、添加jetty服务测试
请浏览该文章:IntelliJ IDEA 配置Jetty启动SpringMvc项目
- 注意:运行项目时出现"Could not instantiate listener org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException"错误时,把所有spring*.jar 都拷贝到 WEB-INF/lib根文件夹下
- 运行项目
9、重整项目结构,增加Controller层测试
-
增加java文件夹(source目录)及demo.controller包结构,存放Controller控制器源码
-
在WEB-INF目录下增加static目录,存放css,image,js等静态文件
-
在WEB-INF目录下增加views目录,存放视图文件
-
修改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中的配置-->
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--静态资源映射-->
<!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
<mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
<mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
<mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<!-- 自动扫描装配 -->
<context:component-scan base-package="demo.controller"/>
</beans>
- 增加IndexController测试文件
@Controller
@RequestMapping
public class IndexController {
@GetMapping("/index")
public String index() {
return "index";
}
}
- 修改web.xml
<web-app>
...
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 重新运行项目