在整合SSM的时候碰到各种让人肉疼的问题,今天就花点时间好好总结一下,算是再熟悉一遍。没弄那么太复杂,主要还是熟悉spring跟mybatis、struts2的配置文件。
问一:jar包问题
1.先是把jar包全部导进来了,eclipse导入jar包有两种方式,一种是添加UserLibrary,一种是直接把所需要的jar都拷到lib目录下。这两种方式有什么区别呢,UserLibrary是引用的方式,lib下就很残暴了,是跟着项目走的。比如把项目文件夹直接发送给另外一个人,jar在lib下的项目是可以直接运行的,但是以UserLibrary的形式就不行了,因为找不到引用了。其实在编译期是没什么区别的,因为都是在本地,总是会找到所要的类的,放到tomcat再运行直接就报ClassNotFoundException。因为tomcat是找不到你本地的jar的。还有就是分web项目跟纯java项目,这个详细就参见另一位大神的博客吧,贴上链接:
http://blog.csdn.net/qiulongtianshi/article/details/8520854
2.所用jar包版本,spring-4.1.7,struts-2.3.24,mybatis-3.3.0。一开始我把所有的jar包都导进去了,当然后来是直接copy到lib下的。但是还是不行,报了一个很奇怪的错误,只怪当时我太心急找答案,问题描述都没有记下来。最后是因为struts的包太多导致的,只要将几个关键的包导入就可以了,具体是哪几个,是struts自带的一个例子blank中的那些,就ok啦。其他的一些jar包问题提示都很详细,直接就说明少什么包,只要按照错误提示来就好。spring我仍是把所有的包放进去的,目前没什么问题,mybaits只放了两个包mybatis-3.3.0和mybatis-spring-1.2.2。
3.struts-spring-plugin,这个包,问题出在我要在action中注入service层中的bean,怎么都注不进去,后来是网上找到的答案
(http://blog.csdn.net/cuihaiyang/article/details/6237799),给我节省了不少时间,一个一个比对jar包那真的是肉疼。就是少了struts-spring-plugin这个包,导入,触决问题。
问二:配置文件问题
1.主配置文件总共也就4个,外加一些mapper.xml,
web.xml, struts.xml, applicationContext.xml, mybatis-config。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts 2配置文件</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="utf-8" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="register" class="com.ssm.action.RegisterAction">
<result name="success">/jsp/registerSuccess.jsp</result>
<result name="fail">/error.jsp</result>
</action>
<action name="login" class="com.ssm.action.LoginAction">
<result name="success">/jsp/loginSuccess.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<!-- 自动扫描包实现注入 -->
<context:component-scan base-package="com.ssm">
</context:component-scan>
<!-- 指定数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/ssh?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池 -->
<property name="dataSource" ref="dataSource" />
<!--configLocation属性指定mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--mapperLocations属性指定 mybatis的 mapper.xml文件 -->
<property name="mapperLocations" value="classpath*:sqlMap/**/*.xml" />
<!--typeAliasesPackage属性指定mybatis的 typeAliases对应的实体类 -->
<property name="typeAliasesPackage" value="com.ssm.entity" />
</bean>
<!-- 以mapper scan的方式扫描,不用再一个个配要映射的mapper
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
sqlSessionFactory属性指定要用到的SqlSessionFactory实例
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象
<property name="mapperInterface" value="com.ssm.dao.UserDao" />
</bean>
-->
<!-- mybatis mapper scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 通过spring中mapperLocation属性自动扫描mapper.xml文件
通过spring中typeAliasesPackage属性自动依赖类
<typeAliases>
<typeAlias alias="User" type="com.ssm.entity.User"/>
</typeAliases>
<mappers>
<mapper resource="sqlMap/UserMapper.xml"/>
</mappers>
-->
</configuration>
配置文件中遇到的问题主要还是路径的问题,注意web.xml中加载spring配置文件路径写法和,applicationContext中加载mybatis主配置文件的路径写法,配置文件都放在与src同级的sourceFolder下
问三:jsp上的上下文路径
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
</head>
<body>
欢迎!
</br>
<a href="jsp/register.jsp">注册</a>
</br>
<a href="jsp/login.jsp">登录</a>
</br>
</body>
</html>
写超链接的时候,要根据所在文件夹写,在加上basepath后,以webContent为根目录写路径。