作 Java Web 开发的你,必定据说过SpringMVC的大名,做为如今运用最普遍的Java框架,它到目前为止依然保持着强大的活力和普遍的用户群。css
本文介绍如何用eclipse一步一步搭建SpringMVC的最小系统,所谓最小系统,就是足以使项目在SpringMVC框架下成功跑起来,而且可以作一些简单的事情(好比访问页面)的系统。html
在开始以前,想提几个问题让你们思考一下:java
让咱们开始吧。web
其余环境:
操做系统:Windos 10
Tomcat : v7.0
JDK : 1.7面试
咱们用eclipse新建项目,选择Dynamic Web Project
(动态的Web项目)。spring
点击Next
docker
Project name
里面写上 springmvc
,这就是咱们项目的名称,其余不用改,直接点击Finish
。浏览器
OK,项目就建好了。spring-mvc
接下来必定要将项目的字符集改成UTF-8
安全
右键项目properties
改成UTF-8
,点击OK。
web.xml
当咱们打开WebContent/WEB-INF
目录的时候,发现里面只有一个lib目录,这是存放各类jar包的地方。咱们知道一个web项目必需要有一个web.xml
文件才行。
既然没有,咱们本身写一个咯。
右键WEB-INF
——new
——file
,新建一个web.xml
文件。
点击Finish
将如下内容填进去便可。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"> <!-- 这是项目的名称 --> <display-name>springmvc</display-name> </web-app>
这样就完成了基本的配置,个人意思是说,如今这个项目就已是一个标准的web项目了。
为了验证到目前为止的正确性,咱们在WebContent
目录下面新建一个jsp
文件。
名字就叫index.jsp
内容以下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <meta charset="UTF-8" /> 5 </head> 6 <body> 7 恭喜,web項目已經成功搭建! 8 </body> 9 </html>
咱们如今就将这个项目部署到Tomcat
,来验证是否能够跑起来。
在项目上右键——Debug As
——Debug on Server
直接点击Finish
通过一段时间,控制台开始打印日志信息,当咱们看到这些信息的时候,说明Tomcat
已经启动完毕了。
让咱们打开浏览器,在地址栏输入如下信息
http://localhost:8088/springmvc/index.jsp
我电脑上Tomcat配置的端口号是8088
,具体状况视你本身的Tomcat
决定,多是8080
等。
可见,可以成功访问页面了,这说明咱们到目前为止的操做是正确的。
SpringMVC
咱们在web.xml
文件里面添加下面的配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>
POST
乱码问题<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
SpringMVC
分发器,拦截全部请求<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>dispatcher-servlet</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
在这个配置中,咱们规定了DispatcherServlet
的关联 XML 文件名称叫作dispatcher-servlet
。
注意,这里的路径是相对于web.xml
来讲的,也就是说,这个文件也在WEB-INF
的根目录下。
因此,咱们须要在WEB-INF
的根目录下新建一个dispatcher-servlet.xml
文件。
至此,web.xml
文件的编写就告一段落了。
dispatcher-servlet.xml
dispatcher-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 开启注解模式驱动 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 扫包 --> <context:component-scan base-package="com.springmvc.*"></context:component-scan> <!-- 静态资源过滤 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 视图渲染 jsp/freemaker/velocity--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 制定页面存放的路径 --> <property name="prefix" value="/WEB-INF/pages"></property> <!-- 文件的后缀 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
根据配置,有三个须要注意的地方。
com.springmvc
包下全部的Java类,但凡是遇到有注解的,好比@Controller
, @Service
, @Autowired
,就会将它们加入到Spring的bean
工厂里面去。js
,css
,images
都须要放在/resources
目录下,这个目录如今咱们尚未建。/WEB-INF/pages
目录下,这个目录如今咱们也没有建。首先是Java
文件的目录。
咱们在这个地方右键,新建一个 com
包,再在里面建一个springmvc
包,或者用 .
的方式一块儿建。
点击Finish
根据SpringMVC
的分层,咱们在springmvc
包下面建三个包,分别是controller
, service
, dao
这样的话, 当咱们项目一旦启动,springmvc
就会扫描这三个包,将里面但凡有注解的类都提取起来,放进Spring
容器(或者说Spring
的bean
工厂),借由Spring
容器来统一管理。这也就是你历来没有去new一个Controller
的缘由。
接下来,咱们来建静态资源的目录。
在WebContent
目录下新建一个resources
文件夹。
而后顺便把js
,css
,img
的文件夹都建一下,这里就存放咱们的静态资源文件。
最后,咱们在WEB-INF
目录下建一个pages
文件夹,做为展现页面的存放目录。
将以前的index.jsp
拷贝进来。
这样就配置的差很少了。
咱们将jar
包放到lib
目录:
而后启动项目,验证一下到目前为止的构建是否正确。
打开Servers
视图,点击如图像是甲虫同样的图标。
发现报错了,错误信息以下:
错误:
Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
它说咱们在WEB-INF
下面少了一个applicationContext.xml
这个文件,原来,咱们少了对SpringBean
工厂的配置,它的意思就是说,咱们要规定一下,在Spring
容器启动的时候,须要自动加载哪些东西?
因而,咱们把 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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd "> </beans>
里面咱们啥也不配置,再次启动Tomcat
。
这回不报错了。
ViewController
咱们知道,WEB-INF
目录下的任何资源都是没法直接经过浏览器的url
地址去访问的,保证了安全性。这也是咱们为何把页面都放在该目录下的缘由。
为了有所区分,咱们还单独创建了一个pages
文件夹,将这些页面保存起来。
如今,为了访问这个页面,咱们须要用到SpringMVC
的页面跳起色制。
咱们在Controller
包下新建一个ViewController
点击Finish
ViewController
代码:
package com.springmvc.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class ViewController { @RequestMapping("/view") public ModelAndView view(HttpServletRequest request){ String path = request.getParameter("path") + ""; ModelAndView mav = new ModelAndView(); mav.setViewName(path); return mav; } }
我只须要将想要访问的页面放在path
里面,经过url
传进来就好了。
由于添加了java
类,所以咱们从新启动Tomcat
。
启动完成后,在地址栏输入:
http://localhost:8088/springmvc/view?path=index
结果:
不要紧,咱们看他报什么错。
message /springmvc/WEB-INF/pagesindex.jsp
pagesindex.jsp
是什么鬼??
原来,在dispatcher-servlet.xml
中,咱们少写了一个 "/"
添上去就好了。
保存后,由于修改了XML
配置文件,所以咱们仍是须要从新启动Tomcat
。
启动完成后,继续!
成功了。
好比,我在resources/img
目录下放了一张图片,怎么引入到index.jsp
呢?
background : url(http://localhost:8088/springmvc/resources/img/bg.jpg); background-size : 100% 100%;
的确,这是一种方式。但是,它有一个缺点就是根路径写死了,咱们确定不但愿这样的。
其实,咱们能够在viewController
里面拿到项目根路径,而后传递到jsp
页面就OK了。
咱们把调试信息 “恭喜,web項目已經成功搭建!” 删掉。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta charset="UTF-8" /> </head> <style> body { background : url(${contextPath}/resources/img/bg.jpg); background-size : 100% 100%; } </style> <body> </body> </html>
${contextPath}
能够取到Controller
传过来的contextPath
值。
成功了!
本次给你们推荐一个免费的学习群,里面归纳Java架构/分布式/微服务/docker/高性能高并发以及面试资源等。
对Java架构感兴趣的程序猿,欢迎加入Q群:790047143,无论你是刚入行得仍是大牛我都欢迎,还有大牛整理的一套高效率学习路线和教程与您免费分享,同时天天更新视频资料。 最后,祝你们早日学有所成。