搭建之前还劳烦大家把有关后台、JSP、mysql、spring等依赖包全部像上篇文章教程一样贴入pom.xml文件中去,我这边也给大家提供一下我自己现在所在用的依赖包,如果大家需要,直接复制就可以
首先我们打开IDEA,重新来配置一下pom.xml这个文件,下面我贴完整的代码,你们复制到 dependencies 中去就可以了
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.2version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.33version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.3.2version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-collections4artifactId>
<version>4.0version>
dependency>
<dependency>
<groupId>commons-dbutilsgroupId>
<artifactId>commons-dbutilsartifactId>
<version>1.6version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-dbcp2artifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.3.3.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.3.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.3.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.3.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>4.3.9.RELEASEversion>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.4version>
dependency>
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>javax.mailgroupId>
<artifactId>mailartifactId>
<version>1.4.7version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
<type>jartype>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.3.10.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.3.11.RELEASEversion>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocityartifactId>
<version>1.7version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-toolsartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.3.7.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-jpaartifactId>
<version>1.10.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.hibernate.javax.persistencegroupId>
<artifactId>hibernate-jpa-2.0-apiartifactId>
<version>1.0.1.Finalversion>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-entitymanagerartifactId>
<version>5.2.10.Finalversion>
dependency>
<dependency>
<groupId>org.jsongroupId>
<artifactId>jsonartifactId>
<version>20090211version>
dependency>
<dependency>
<groupId>commons-httpclientgroupId>
<artifactId>commons-httpclientartifactId>
<version>3.1version>
dependency>
等它自动下载完成后,我们来配一下两个文件
一个是蓝色的java和绿色的java,这两个都是用来存放java类的,怎么让它们变色呢?刚开始你们是没有的,我们要来新建
右键点击main–New–Directory,取名为java
按照我标记的顺序依次点击,点OK就好了,绿色的是测试类,我们可以新建一个test包名,test下面再新建一个java,按照这个顺序,第五步点那个绿色的Tests点OK就好了。
首先我们手动新建几个目录:
这里我们是配置配置了静态文件路径,这几个文件件用以放静态文件例如脚本,图片文件,视图之类的
在WEB-INF文件夹下新建 spring-servlet.xml
鼠标点击WEB-INF右键,New–XML Configuration–Spring Config
配置spring-servlet.xml这里采用注解的方式:
<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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<mvc:annotation-driven >mvc:annotation-driven>
<context:component-scan base-package="自己新建的存放Java类的包名" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
bean>
<mvc:resources location="/files/" mapping="/files/**" />
<mvc:resources location="/scripts/" mapping="/scripts/**" />
<mvc:resources location="/styles/" mapping="/styles/**" />
<mvc:resources location="/Views/" mapping="/Views/**" />
beans>
context:component-scan base-package=”自己新建的存放Java类的包名” 大家注意这个地址,这个地址需要自己手动改一下,这里是存放控制器的包名位置
然后在resources资源文件夹下新建applicationContext.xml
配置applicationContext.xml这里并没有进行配置什么信息,我们可以在其中添加我们所需要配置的bean,也可以添加相应的数据库连接和事务处理等等,方便后续拓展,所以先放着不写它,接着最后一步
配置web.xml:
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
把这些代码复制到web-app 中去就可以了,具体什么作用我已经打了注释
这里我们就将SpringMVC的基本XML配置文件设置完毕,接下来我们进行一个小实例进行测试配置好的SpringMVC。
这个是我新建的包名,在这个包名下新建一个java类,名字随意
这些代码的作用我都已经打上了注释
接着我们在之前新建好的Views包名下新建一个jsp页面
接着我们就可以运行我们的tomcat来做个测试了,上次教过大家怎么开启maven插件的tomcat7,这里就不多说了,没明白的请翻我上一篇文章,上次有开启过的在IDEA右上角会有一个标记
这个时候只需要点这个小爬虫debug就可以了
当成功开启之后,我们打开浏览器输入:
localhost:85/Test/returnString
这里说明一下,85是我自己定义的端口号,你们要输入你们自己的8080端口即可。
当看到这个的时候表示我们成功了,接着我们输入:
localhost:85/Test/returnSuccess
大家一定有疑问,为什么这个访问路径是这个样子的,具体的我就不在这里给大家详细讲解了,可以百度一下“Spring MVC”的原理,自然就会明白了,本人对MVC也是初体验,所以不便给大家讲解,希望大家共同进步。