老求了,好多东西记不住了,再不记以后怕是记不住了。

eclipse JAVAEE for web Version: Mars.2 Release (4.5.2)

tomcat 7.0.29

spring3.1.1

freemarker-2.3.22

commons-io-2.5.jar  没这个东西 request.getParameter() 会报错

算了 来个JAR包截图

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

源码结构截图

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

新建tomcat server服务器

打开server管理面板,拖到左边去

window-show view -servers

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

servers面板空白处右键

new - server

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

选7.0 再取个酷炫的名字

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

直接finish完成 就可以在servers面板中看到了

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

双击myserver打开这个服务的配置

主要说下 server locations 这里的配置,相信很多新手对于这三个鬼是

一脸蒙蔽的,多半会进行鬼畜设置,然后各种报错....

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

先说 use custom location 如果你选择了这项 并像下面配置

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

你会发现..你的webapps目录下有个transfeServer  目录和跟tomacat根目录下一样的conf目录

如果你这样设定。。那么当你的tomcat运行的时候 读取是这里的server.xml 和web.xml..

tomcat根目录下conf目录的设置就没效了。。

(transfeServer是我自己取的名字)

所以各位果断选择第二个 use tomcat installation

deploy path 设置 webapps,因为需要将项目发布到tomcat 的webapps下

两个重点

当你建好server时,工作空间(eclipse workspace) 中会多出 servers/Myserver-config(我建TOMCAT server名)的目录,里面的目录和tomcat的conf目录内容一样的

什么意思呢?

就是说当RUN或者DEBUG的时候这里的配置文件会作为基准文件自动拿去覆盖

tomcat下的conf目录下的所有文件。

那么以后就在此改配置就可以了

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

另外一个重点

选择 use tomcat installation 后,eclipse工作空间基准配置文件servers/Myserver-config/server.xml中的 <host> 标签里面会自动多出一个虚拟目录的设置,果断删掉。。别问为什么。。

一般会在下图的位置出现

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

以上内容配置好后可以着手 Spring Mvc的配置

先配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>transferServer</display-name> <!-- 容器上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param> <!-- 加载Spring容器配置 -->
<!--Spring 上下文监听器 - -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener> --> <!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- SPRINGMVC拦截器 -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/servlet/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- 使用SPRINGMVC拦截器 -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
--> <!-- 激活Tomcat的defaultServlet来处理静态文件 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping> </web-app>

 分解下web.xml中的配置

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

context-param 关于这个参数解释可以看这http://www.cnblogs.com/cfas/p/7851899.html

所有与Spring容器对象相关的配置都放到WEB-INF/spring/目录下

如果没有定义具体的xxx.xml 则默认会先找 applicationContext.xml 这个配置

所以WEB-INF/spring/ 需要有 applicationContext.xml  这里到底定义什么我们待会儿再说

接着配置spring 监听器 两个都要

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

=========================偷懒分割线===========================

还是觉的没必要解释XML的配置了。直接上XML内容吧

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>transferServer</display-name> <!-- 容器上下文配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param> <!-- 加载Spring容器配置 -->
<!--Spring 上下文监听器 - -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- spring 需要log4日志 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param> <!-- 定义LOG4J监听器 -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener> <!-- SPRINGMVC拦截器 -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/servlet/*.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <!-- 使用SPRINGMVC拦截器 -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 激活Tomcat的defaultServlet来处理静态文件 -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping> </web-app>

 

servlet/springMcvServlet.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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能(除去带@Service注解的类) -->
<context:component-scan base-package="com.cn.xql.controller">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan> <!-- don't handle the static resource -->
<mvc:default-servlet-handler /> <!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven /> <!-- 拦截器 -->
<!--<mvc:interceptors> -->
<!-- 多个拦截器,顺序执行 -->
<!-- 登录认证拦截器 -->
<!-- <mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.cn.xql.Handle"/>
</mvc:interceptor>
</mvc:interceptors> -->
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能(除去带@Service注解的类) --> <!--通用视图解析器-->
<bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/jsp/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
<property name="order" value="2"/>
</bean> <bean id="viewResolverFreemarker" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="order" value="1"></property>
<property name="suffix" value=".html"></property>
<property name="contentType" value="text/html;charset=utf-8"></property>
<property name="viewClass">
<value>org.springframework.web.servlet.view.freemarker.FreeMarkerView</value>
</property>
</bean>
<!--freemarker配置 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/WEB-INF/tpl/</value>
</property>
<property name="freemarkerSettings"><!-- 设置FreeMarker环境属性 -->
<props>
<prop key="template_update_delay">5</prop><!--刷新模板的周期,单位为秒 -->
<prop key="default_encoding">UTF-8</prop><!--模板的编码格式 -->
<prop key="locale">UTF-8</prop><!--本地化设置 -->
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">0.####</prop>
<prop key="boolean_format">true,false</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="tag_syntax">auto_detect</prop>
<prop key="url_escaping_charset">UTF-8</prop>
</props>
</property>
</bean> <!-- 协商视图配置 spring4 才有这个功能
这里用到了 ContentNegotiatingViewResolver ,“内容协商视图解析器”,
其实就是根据返回什么类型的视图,就协商使用哪种视图解析器。
如果返回jsp就使用InternalResourceViewResolver视图解析器,
如果返回JSON格式就使用MappingJackson2JsonView来处理。
如此而已。在 <property name="viewResolvers"> 下的<list> 标签下,
还可以加入其他的各种视图解析器的配置。--> </beans>

 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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 支持@Autowired注解导入 -->
<context:annotation-config />
<!-- 测试模型 -->
<bean id="User" class="com.cn.xql.data.domain.User"></bean>
</beans>

下面 这个是 mybits用的

spring/applicationContext-dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- JDBC配置参数 -->
<context:property-placeholder location="classpath:jdbc.properties" order="1" ignore-unresolvable="true"/>
<!-- 认证 支持注释的事务声明 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource1"/>

<!-- 数据链接 -->
<bean id="dataSource1" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driver.class0}" />
<property name="jdbcUrl" value="${jdbc.url0}" />
<property name="username" value="${jdbc.username0}"/>
<property name="password" value="${jdbc.password0}"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
</bean>

<!-- 数据源 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="mapperLocations" value="classpath*:com/cn/xql/data/dao/*.xml" />
</bean>

<!-- 数据 自动生成部分-->
<!-- 用户操作日志 -->

<bean id="UserOperationLogMapperImpl" class="com.cn.xql.data.dao.impl.UserOperationLogMapperImpl" p:sqlSessionFactory-ref="sqlSessionFactoryBean"/>

</beans>

注意的问题

本次配置spring 和mybaits 都用的是3.1.1

但是为了使用mybaits 的简单日志功能 ,启用了logImpl...但是..mybaits3.1.1这个设置是没用的,

只有3.2.2才行,所以 最后配置成spring 3.1.1 mybatis3.2.3

这样配置后,运行项目时,启动会有点点延迟。。

这是mybatis-config.xml

eclipse JavaEE spring,spring mvc,mybits-SSM老年人搭建记录-LMLPHP

05-11 11:05