Spring SpringMVC MyBatis 起步(一)
【TODO: 简述】
1.项目创建
开发环境选择eclipse, java1.8.0, maven 3.5.0
使用eclipse创建新项目-》选择maven项目-》选择webapp-》输入项目信息-》点击finish创建项目
创建项目后在在 src/main/ 目录下创建好java文件目录,如红框所示
2.编辑web.xml,配置servlet
打开web.xml,内容如下
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>Archetype Created Web Application</display-name> 7 </web-app>
更改一下web.xml的内容,进行servlet的相关配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <display-name>FileReportary</display-name> 8 9 <!-- Spring和mybatis的配置文件 --> 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value>classpath:applicationContext*.xml</param-value> 13 </context-param> 14 15 <!-- Spring监听器 --> 16 <listener> 17 <listener-class> 18 org.springframework.web.context.ContextLoaderListener 19 </listener-class> 20 </listener> 21 22 <!-- 防止Spring内存溢出监听器 --> 23 <listener> 24 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 25 </listener> 26 27 <servlet> 28 <servlet-name>mvc-dispatcher</servlet-name> 29 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 30 <!-- DispacherServlet对应的上下文配置,默认为/WEB-INF/$servlet-name$-servlet.xml --> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:mvc-dispatcher-servlet.xml</param-value> 34 </init-param> 35 <load-on-startup>1</load-on-startup> 36 </servlet> 37 <servlet-mapping id="ServletMapping_1533543131510"> 38 <servlet-name>mvc-dispatcher</servlet-name> 39 <!-- mvc-dispatcher拦截所有的请求 --> 40 <url-pattern>/</url-pattern> 41 </servlet-mapping> 42 </web-app>
<param-value>标签中包含了引用配置文件的路径
1 <param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
classpath路径在每个J2ee项目中都会用到,即WEB-INF目录下的classes目录,所有src目录下的java、xml、properties等文件编译后都会在此目录下,开发时经常将xml配置文件放置于src目录或子目录下
引用classpath路径下的文件,只需在文件名前加【classpath:】
在resources目录下创建mvc-dispatcher-servvlet.xml文件,内容如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 14 15 16 <!-- 激活 @Required @Autowired,JSR 250's @PostConstruct, @PreDestroy and @Resource 17 等标注 --> 18 <context:annotation-config></context:annotation-config> 19 20 <!-- 注解扫描 只搜索@Controller标注的类 --> 21 <context:component-scan base-package="com.xcz"> 22 <context:include-filter type="annotation" 23 expression="org.springframework.stereotype.Controller" /> 24 </context:component-scan> 25 26 <!-- 扩充注解驱动,可以将请求参数绑定到控制器参数 --> 27 <mvc:annotation-driven /> 28 29 <!-- 静态资源处理 css ,js ,imgs --> 30 <mvc:resources location="/resources/" mapping="/resources/**" /> 31 32 <!-- ViewResolver 可以用多个ViewResolver 使用order属性排序 InternalResourceViewResolver放在最后 --> 33 <bean 34 class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 35 <property name="order" value="1" /> 36 <property name="mediaTypes"> 37 <map> 38 <entry key="json" value="application/json" /> 39 <entry key="xml" value="application/xml" /> 40 <entry key="htm" value="text/html" /> 41 </map> 42 </property> 43 44 <property name="defaultViews"> 45 <list> 46 <!-- JSON View --> 47 <bean 48 class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> 49 </bean> 50 </list> 51 </property> 52 <property name="ignoreAcceptHeader" value="true" /> 53 </bean> 54 55 <!-- 200M大小限制 resolveLazily属性启用是为了推迟文件解析,以便捕获文件大小异常 --> 56 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 57 <property name="maxUploadSize" value="209715200"/> 58 <property name="defaultEncoding" value="UTF-8"/> 59 <property name="resolveLazily" value="true"/> 60 </bean> 61 62 <!-- 定义跳转的文件的前后缀 ,视图模式配置 --> 63 <bean 64 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 65 <property name="viewClass" 66 value="org.springframework.web.servlet.view.JstlView" /> 67 <property name="prefix" value="/WEB-INF/jsp/" /> 68 <property name="suffix" value=".jsp" /> 69 </bean> 70 </beans>
3.使用maven管理项目依赖
打开Preferences设置框:菜单栏-》Window-》preferences
打开maven配置: 左侧目录中选择Maven-》installations,点击Add,在Installation home中选中你安装的maven所在目录,点击finish完成配置
设置Maven User Settings:左侧目录中选择Maven-》User Settings,在User Settings 处选择你安装的maven的配置文件,之后会在配置文件中读取你的maven仓库位置
点击【Apply and Close】 完成Maven的设置
pom.xml是Maven在项目中创建的依赖管理配置文件,打开pom.xml进行编辑,为项目所需的包添加依赖
PS: 注意在<name>标签内容中去掉空格,有时这些空格可能会导致异常
打开pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.xcz</groupId> 6 <artifactId>fileReportary</artifactId> 7 <packaging>war</packaging> 8 <version>0.0.1-SNAPSHOT</version> 9 <name>fileReportary Maven Webapp</name> 10 <url>http://maven.apache.org</url> 11 12 <dependencies> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>3.8.1</version> 17 <scope>test</scope> 18 </dependency> 19 </dependencies> 20 <build> 21 <finalName>fileReportary</finalName> 22 </build> 23 </project>
加入properties标签,自定义一些变量,在之后的依赖配置中方便使用
1 <modelVersion>4.0.0</modelVersion> 2 <groupId>com.xcz</groupId> 3 <artifactId>fileReportary</artifactId> 4 <packaging>war</packaging> 5 <version>0.0.1-SNAPSHOT</version> 6 <name>fileReportary Maven Webapp</name> 7 <url>http://maven.apache.org</url> 8 9 <properties> 10 <spring.version>4.1.1.RELEASE</spring.version> 11 <commons-lang.version>2.6</commons-lang.version> 12 <slf4j.version>1.7.6</slf4j.version> 13 <jackson.version>2.5.4</jackson.version> 14 <mybatis.version>3.2.6</mybatis.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>junit</groupId> 20 <artifactId>junit</artifactId> 21 <version>3.8.1</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies> 25 <build> 26 <finalName>fileReportary</finalName> 27 </build>
添加依赖包
编辑完成后使用【ALT+F5】更新项目依赖,此时maven会自动下载依赖包
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>3.8.1</version> 6 <scope>test</scope> 7 </dependency> 8 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-core</artifactId> 12 <version>${spring.version}</version> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-web</artifactId> 17 <version>${spring.version}</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-webmvc</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-jdbc</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 30 <dependency> 31 <groupId>commons-lang</groupId> 32 <artifactId>commons-lang</artifactId> 33 <version>${commons-lang.version}</version> 34 </dependency> 35 <dependency> 36 <groupId>org.slf4j</groupId> 37 <artifactId>slf4j-log4j12</artifactId> 38 <version>${slf4j.version}</version> 39 <exclusions> 40 <exclusion> 41 <artifactId>slf4j-api</artifactId> 42 <groupId>org.slf4j</groupId> 43 </exclusion> 44 </exclusions> 45 </dependency> 46 47 <dependency> 48 <groupId>org.slf4j</groupId> 49 <artifactId>slf4j-api</artifactId> 50 <version>${slf4j.version}</version> 51 </dependency> 52 53 <dependency> 54 <groupId>javax.servlet</groupId> 55 <artifactId>javax.servlet-api</artifactId> 56 <version>3.1.0</version> 57 </dependency> 58 59 <dependency> 60 <groupId>commons-fileupload</groupId> 61 <artifactId>commons-fileupload</artifactId> 62 <version>1.3.1</version> 63 </dependency> 64 65 <!-- 引入JSON --> 66 <dependency> 67 <groupId>org.codehaus.jackson</groupId> 68 <artifactId>jackson-mapper-asl</artifactId> 69 <version>1.9.13</version> 70 </dependency> 71 <dependency> 72 <groupId>com.fasterxml.jackson.core</groupId> 73 <artifactId>jackson-databind</artifactId> 74 <version>${jackson.version}</version> 75 </dependency> 76 </dependencies>
在build标签中加入jetty插件的配置
1 <build> 2 <finalName>fileReportary</finalName> 3 <plugins> 4 <plugin> 5 <groupId>org.eclipse.jetty</groupId> 6 <artifactId>jetty-maven-plugin</artifactId> 7 <version>9.2.2.v20140723</version> 8 </plugin> 9 <plugin> 10 <artifactId>maven-compiler-plugin</artifactId> 11 <configuration> 12 <source>1.6</source> 13 <target>1.6</target> 14 <!-- 指明编译源代码时使用的字符编码, maven编译的时候默认使用的GBK编码, 通过encoding属性设置字符编码, 告诉maven这个项目使用UTF-8来编译 --> 15 <encoding>utf8</encoding> 16 </configuration> 17 </plugin> 18 </plugins> 19 </build>
4.配置log4j
【resources】目录下新建log4j.properties文件,对log4j进行配置
1 # Root logger set to DEBUG using the A2 appender defined above. 2 log4j.rootLogger=info, Cons 3 log4j.additivity=false 4 5 log4j.appender.Cons=org.apache.log4j.ConsoleAppender 6 log4j.appender.Cons.layout=org.apache.log4j.PatternLayout 7 log4j.appender.Cons.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 8 9 10 #Application Logger+ 11 log4j.logger.com.xcz=info, Cons 12 log4j.logger.org.springframework=info, Cons 13 log4j.additivity.com.xcz=false 14 log4j.additivity.org=false
5.输出Helllo World!
在com.xcz.controller 下创建IndexController.java文件
为IndexController类添加类级注解@Controller,@RequestMapping(value = "/index")
@RequestMapping(value = "/index") 这个注解表明了改类的访问url,是在【 http://localhost:8080/index】 下
1 package com.xcz.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 @RequestMapping(value = "/index") 8 public class IndexController { 9 10 }
创建Index()方法,为其添加@RequestMapping("")注解,指明该方法的访问url为【 http://localhost:8080/index】
方法内部返回字符串 "index" ,这个index就是所调用的jsp文件的名称
1 @RequestMapping("") 2 public String Index() { 3 return "index"; 4 }
在 【webapp/WEB-INF/jsp】目录下创建 index.jsp文件,并为其添加内容
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>我是标题</title> 8 </head> 9 <body> 10 Hello World! 11 </body> 12 </html>
打开windows资源管理器,找到项目根目录,按住SHIFT并右键空白处,选择“在此处运行power shell” 打开命令行控制台
输入 mvn jetty:run 运行项目
这里提示在8080端口启动的话,就说明启动成功了
在浏览器中访问 localhost:8080/index查看结果
输出了Hello World! 说明整个流程跑通了!