Eclipse中搭建ssh框架

来源:互联网 发布:c语言 逆波兰式计算器 编辑:程序博客网 时间:2024/04/30 17:25

开发环境及工具

* java jdk:  jdk-7u60-windows-i586  设置JAVA_HOME(必须),否则tomcat无法启动成功* tomcat: apache-tomcat-7.0.54-windows-x86* Eclipse javaee: eclipse-jee-kepler-SR2-win32

ssh框架版本号及添加支持

* struts 2.3

导入struts/lib包下所必须的jar包,添加到webContent/WEB-LIB/lib目录下,包括Commons-logging-1.0.4.jar,Freemarker-2.3.13.jar, Ognl-2.6.11.jar,Struts2-core-2.1.6.jar,Xwork-2.1.2.jar。其余jar包并不是struts必须的。还有3个包也要注意导入。不导入运行Tomcat时候可能会出现异常。commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,javassist-3.7.ga.jar 。并在web.xml文件中配置。

* spring 3.2.4

将spring lib下除了sources和javadoc之外的jar包添加到webContent/WEB-LIB/lib目录下。并在web.xml文件中添加spring的配置,见下文。

* hibernate版本:3.6(达梦数据库7方言最高支持4.0)

required目录、jpa目录和optional/c3p0(数据源)目录下的jar包添加到webContent/WEB-LIB/lib目录下,并在spring的配置文件中添加hibernate的管理。

安装达梦数据库
创建数据库时,注意不要勾选大小写敏感,并采用utf编码。
达梦方言jar包和驱动包也放在lib下

建立java web项目

* 在Eclipse中新建java web 项目,在项目中的webContent/WEB-LIB/目录下新建web.xml文件,增加对struts2的支持
<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>

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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version ="2.5">  < display-name>资料管理系统 </display-name ><welcome-file-list>    <welcome-file >index.jsp </welcome-file >  </ welcome-file-list><filter>    <filter-name >setCharacterEncoding </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>   <!-- log4j的配置 -->   <!-- log4j config path -->    <context-param>      <param-name>log4jConfigLocation</param-name>      <param-value>/WEB-INF/classes/log4j.properties</param-value>    </context-param>       <!-- log4j config listener -->   <listener>        <listener-class>             org.springframework.web.util.Log4jConfigListener        </listener-class>   </listener>  
* 在java src目录下添加struts.xml配置文件,部署后自动放置在 WEB-INF/classes目录下
<?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.custom.i18n.resources" value= "resource" />     <constant name ="struts.i18n.encoding" value= "utf-8" />     <package name ="struts2" extends="struts-default" namespace= "/">            <interceptors >                 <interceptor-stack name ="GlobalStack">                      <interceptor-ref name ="defaultStack">                            <param name= "exception.logEnabled">true</param >                            <param name= "exception.logLevel">ERROR</param >                      </interceptor-ref >                 </interceptor-stack >            </interceptors >            <default-interceptor-ref name= "GlobalStack"></default-interceptor-ref >            <global-results >                 <result name= "error">/WEB-INF/content/error.jsp </result >            </global-results >            <global-exception-mappings >                 <!--在此可以设置多个异常类型 -->                 <exception-mapping result ="error" exception= "java.lang.Exception"></exception-mapping >            </global-exception-mappings >            <action name ="addPerson" class="addPersonAction">                 <result name= "success">/login_success.jsp </result >                 <result name ="error">/login_failure.jsp</ result>            </action >          <action name= "*_*">                 <result >/WEB-INF/{1}/{2}. jsp</ result>            </action >     </package ></struts>

在web.xml文件中配置spring

    <!-- Spring相关的配置 -->     <context-param >            <param-name >contextConfigLocation </param-name >            <param-value >/WEB-INF/applicationContext.xml,  //默认的                          /WEB-INF/dataBeanContext.xml     //自己加的,配置dao,service,action,更有条理些                          </param-value >     </context-param >          <!-- 使用ContextLoaderListener初始化Spring容器 -->     <listener >           <listener-class >org.springframework.web.context.ContextLoaderListener            </listener-class >     </listener >

在WEB-INF文件夹中建立applicationContext.xml,dataBeanContext.xml文件(spring的配置文件)
applicationContext.xml中配置数据库连接,整合hibernate,对hibernate的Session的创建、提交、关闭的整个生命周期进行管理(Hibernat 对数据库的操作是通过Session来实现的,这里的session不同于页面间传递参数的session, 而是类似于JDBC中的 Connection),事务管理(aop)等。

<?xml version="1.0" encoding= "GBK"?><!-- 指定Spring配置文件的Schema信息 --><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:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="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/aop     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >     <!-- 定义数据源Bean,使用C3P0数据源实现 -->     <!-- 设置连接数据库的驱动、URL、用户名、密码 连接池最大连接数、最小连接数、初始连接数等参数 -->     <bean id ="dataSource" class= "com.mchange.v2.c3p0.ComboPooledDataSource"            destroy-method= "close">            <property name ="driverClass">                 <value >dm.jdbc.driver.DmDriver </value >            </property >            <property name ="jdbcUrl">                 <value >jdbc:dm:// localhost:5236/MILITARYMS</value >            </property >            <property name ="user">                 <value >SYSDBA </value >            </property >            <property name ="password">                 <value >SYSDBA </value >            </property >            <property name ="maxPoolSize">                 <value >40 </value >            </property >            <property name ="minPoolSize">                 <value >1 </value >            </property >            <property name ="initialPoolSize" value= "1"></property >            <property name ="maxIdleTime" value="20"></ property>     </bean >     <!-- 定义Hibernate的SessionFactory ,-->     <!-- 依赖注入数据源,注入正是上面定义的dataSource -->     <bean id ="sessionFactory"           class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >            <property name ="dataSource">                 <ref bean ="dataSource"></ ref>            </property >            <!-- mappingResouces属性用来列出全部映射文件 -->            <property name ="mappingResources">                 <list >                      <!-- 以下用来列出 Hibernate映射文件 -->                      <value >org/military/po/Employee.hbm.xml </value >                 </list >            </property >            <!-- 定义Hibernate 的SessionFactory的属性 -->            <property name ="hibernateProperties">                 <!-- 指定数据库方言、是否自动建表 是否生成SQL语句等 -->                 <props >                      <prop key ="hibernate.dialect">                           org.hibernate.dialect.DmDialect                      </prop >                      <prop key ="hibernate.show_sql">                           true                      </prop >                      <prop key= "hibernate.connection.autocommit" >                           false                      </prop >                 </props >            </property >     </bean >     <bean id ="hibernateTemplate" class= "org.springframework.orm.hibernate3.HibernateTemplate" >            <property name ="sessionFactory">                 <ref bean ="sessionFactory" />            </property >     </bean ></beans>

dataBeanContext.xml文件配置dao,service,action等

<?xml version="1.0" encoding= "GBK"?><!-- 指定Spring配置文件的Schema信息 --><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-3.0.xsd" >     <bean id ="employeeDAO" class= "org.military.dao.impl.EmployeeDAOImpl" >            <property name ="hibernateTemplate">                 <ref bean ="hibernateTemplate"/>            </property >     </bean >     <bean id ="employeeService" class= "org.military.service.impl.EmployeeServiceImpl" >            <property name ="employeeDao" ref= "employeeDAO"></property >     </bean >     <bean id ="employeeAction" class= "org.military.action.EmployeeAction" scope ="prototype">            <property name ="employeeService" ref= "employeeService"></property >     </bean ></beans

新建jsp页面,页面可以放在webContent/WEB-LIB目录下,也可以放在该目录外,webContent/WEB-LIB目录下的文件不能通过链接的形式直接访问到,必须通过action的跳转来访问,所以更加安全。因此可以将登录页面放在webContent/WEB-LIB目录外,并通过跳转到真正的登录页面,比如:
登录的index.jsp页面放在webContent/WEB-LIB外,添加如下代码访问webContent/WEB-LIB里真正的登录页面:
< jsp:forward page= “/WEB-INF/content/main.jsp” >

0 0
原创粉丝点击