* 前言:为了学习java,就搭建了一次SSH框架权当复习理解。*
环境介绍:
IDE:eclipse
Struts版本:2.3.8
Spring版本:3.2.1
Hibernate版本:4.2.0
数据库:mysql
开始搭建
1.首先建立一个新项目,名称为:SSH_DEMO
2.添加SSH所依赖的jar包和我们将使用的mysq包
3.添加, 这里我使用了user_library,这样就可以把各个jar包整理归类,可以看的更舒服一点。并且添加log4j用到的log4j-1.2.12.jar和slf4j-log4j12-1.4.1.jar
各个user_library下面的类如下:
4.添加类完毕后,就可以进行配置一系列文件了。先从web.xml开始。
<!-- Struts配置 -->
<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>
<!-- log4j配置文件 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/log4j.properties</param-value>
</context-param>
<!-- log4j刷新时间 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>6000</param-value>
</context-param>
<!-- log4j配置文件 -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- Spring配置 -->
<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>
这样我们配置了log4j的路径及刷新时间,struts过滤器,spring的ContextLoaderListener的配置文件位置及命名方式。那么接下来就是配置struts.xml和applicationContext*.xml.
5.在SRC目录行下新建struts.xml,applicationContext.xml和hibernate.cfg.xml。此时配置需要示例程序的相关信息。程序如下图所示:
其中:
LoginAction为接收jsp登陆请求的action.
User.java为实体bean.
UserDao.java为Dao接口.
UserDaoImpl.java为Dao实现类.
Login.hbm.xml为对应实体User的配置文件.
index.jsp为登陆页面.
login.jsp为登陆成功页面.
6.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!--namespace对应于form请求的namespace.name为是管理方便 -->
<package namespace="/" name="struts2" extends="struts-default">
<!-- action name是名字 method要执行的方法 指向的类是com.action.LoginAction -->
<action name="login" method="execute" class="com.action.LoginAction">
<!--成功的话返回/jsp/login.jsp -->
<result name="success">/jsp/login.jsp</result>
<!--失败的话返回/jsp/index.jsp -->
<result name="input">/jsp/index.jsp</result>
</action>
</package>
</struts>
7.配置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:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- component scan -->
<context:component-scan base-package="." />
<!-- hibernate session -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="ConfigLocation">
<!--指定hibernate的配置文件-->
<value>classpath:/hibernate.cfg.xml</value>
</property>
</bean>
</beans>
8.配置hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 各属性的配置 -->
<!-- 为true表示将Hibernate发送给数据库的sql显示出来 -->
<property name="show_sql">true</property>
<!-- SQL方言,这边设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 一次读的数据库记录数 -->
<property name="jdbc.fetch_size">16</property>
<!-- 设定对数据库进行批量删除 -->
<property name="jdbc.batch_size">30</property>
<!--驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/mysql?characterEncoding=UTF-8</property>
<!-- 数据库用户名 -->
<property name="connection.username">root</property>
<!-- 数据库密码 -->
<property name="connection.password">root@123</property>
<property name="current_session_context_class">thread</property>
<!-- 指定mapping文件的位置 -->
<mapping resource="com/hbm/Login.hbm.xml"/>
</session-factory>
</hibernate-configuration>
9.配置Login.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test">
<class name="com.bean.User" table="T_user">
<id name="username" column="username" type="java.lang.String">
<generator class="native"/>
</id>
<!-- <property name="username">
<column name="username"></column>
</property> -->
<property name="password">
<column name="password"></column>
</property>
</class>
</hibernate-mapping>
10.配置相应的log4j.properties
log4j.rootCategory=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\Tomcat 5.5\logs\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
11.然后编写各个相应的代码即可。在数据库中新增一条记录,username和password都是ddd.运行项目.
发现报以下错误:
java.lang.ClassNotFoundException: org.springframework.web.util.Log4jConfigListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4148)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
at org.apache.catalina.core.StandardService.start(StandardService.java:525)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
2016-2-24 11:44:41 org.apache.catalina.core.StandardContext listenerStart
这里使用的user libraries,所以要设置项目依赖。在项目上右键,选择 properties ,选择 Deployment Assembly设置项目依赖于三个user library和引入的jar包.如果不行,就放在tomcat发布项目的WEB-INF/lib目录下。
运行文件效果如下:
总结:
本次搭建的SSH框架属于简单入门级的,其中好多东西没有用到,比如连接池,opensessioninview等。并且有些东西因为没有深入了解,还是有些模糊的。
附件在我的资源中可以下载。
问题总结:
1.The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced from required .class files
DaoSupport is packaged within spring-tx. Please double check your classpath. HibernateDaoSupport is packaged within spring-orm. Maybe you missed the tx jar file.
原因:缺失spring-tx.jar
2.could not load the Tomcat server configuration the servers is closed
这是因为server的project关闭了,open就好了。
3.使用USER_LIBIRARY的时候一定要在PROJECT的PREFERENCE里Deployment Assembly->ADD->JAVA BUILD Path Entries->选择使用的LIBRARY
4.java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
5.Spring frame work config issue “prefix context for for element context:annotation-config is not bound”
I was experiencing the same problem until I realized beans tag attribute xmlns:context was missing. Just added the below lines
xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
使用的版本:
<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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
6.元素类型为 “class” 的内容必须匹配 “(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),di
这是因为mapping配置文件hbm.xml,因为没有配置id,所以才会这样。