很早就有写点博客的想法了,却一直没有实施。这个是我的第一篇博客,就从ssm框架的搭建开始吧。
因时间仓促所以先纪录下搭建ss的,后期再逐渐完善,虽然没有对数据的操作,但是项目基本是可以正常跑起来了。
一 开发环境:
jdk1.8;tomcat8;idea;maven
二 开发过程
1 创建项目
使用idea开发工具创建的一个maven项目。
2 项目结构(见附件)
src
|-- filters (主要是一些环境配置文件,比如一些数据库的参数配置等等)
|-- dev.properties
|-- test.properties
|-- main
|-- java
|-- FirstDemo
|-- controller
|--test
|--dao
|--server
|-- resources (资源配置文件)
|-- dubbo
|-- spring
|-- applicationContext.xml
|-- spring-dal-mysql.xml
|--spring-mvc.xml
|-- webapp
|-- WEB-INF
|-- view
|-- hello.html
|-- web.xml
|-- index.jsp
3 maven中项目的依赖包
<properties> <spring.version>4.1.6.RELEASE</spring.version> <logback-version>1.1.2</logback-version> <slf4j-version>1.7.6</slf4j-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 1.日志 --> <!-- 实现slf4j接口并整合 --> <!-- logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${slf4j-version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>${slf4j-version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback-version}</version> <type>jar</type> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback-version}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!-- mybatis-generator-core --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.4</version> </dependency> <!--放弃直接应用spring.jar而是单独映入spring 的其他单独jar--> <!--<dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>3.1.1</version> </dependency>--> <!--spring和springmvc有关架包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- aspectj --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.6</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!--cglib代理--> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency>
4 web.xml文件的配置
ps:只是一些简单配置,如果项目全面还需要添加一些过滤器啊等等配置,每个配置都有对应的注释
<!--加载spring的配置,主要是spring的配置文件,当然也可以在启动的时候加载其他的配置文件入spring容器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置servlet拦截器,springmvc框架的配置文件--> <servlet> <servlet-name>springWeb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-mvc.xml</param-value> </init-param> <!--容器启动的优先级,正整数值越小,优先级越高,相同则按顺序加载--> <load-on-startup>1</load-on-startup> </servlet> <!--设定需要拦截的url--> <servlet-mapping> <servlet-name>springWeb</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springWeb</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!--过滤页面字符编码--> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5 资源配置文件
<?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"> <!--配置视图文件地方--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".html"/> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-lazy-init="true"> <!--扫描包--> <context:component-scan base-package="FirstDemo"/> <aop:aspectj-autoproxy expose-proxy="true"/> <!--注解驱动--> <mvc:annotation-driven/> <!--开启哪个数据库连接包下的事务--> <!-- <tx:annotation-driven transaction-manager="channelTansactionManager" proxy-target-class="true" />--> <!--可以通过import引用一些别的资源配置文件..比如专门数据源的等等--> <!--<import resource="spring-mvc.xml"/>--> <!-- <import resource="spring-dal-mysql.xml"></import>--> </beans>
6 以上差不多就可以简单的跑起来了,看下效果吧
7 当然好像还忘了点什么,就是controller中的一点东西
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * Created by sunholdIng on 2017/1/12. */ @Controller @RequestMapping(value = "/hello") public class test { @RequestMapping(value = "/hello") public String hello(){ return "hello"; } }
8 顺便在说下idea中如何配置tomcat吧
1⃣️
2⃣️
3⃣️
4⃣️
5⃣️