SSI框架搭建

Posted 刘承c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSI框架搭建相关的知识,希望对你有一定的参考价值。

SSI框架为struts、spring、ibatis,在该框架中,使用ibatis作为数据持久层,其中ibatis使用最新版本mybatis3。

注:本文使用版本:struts-2.3.4、spring-3.2.1、mybatis-3.2.1

注: i.文件结构为:/src/main/resources,src/main/java,并将其添加到build path中。【Java Build Path - Add Folder】  
        ii./src/main/resources下文件有:struts.xml、applicationContext.xml、jdbc.oracle.properties、sqlMapConfigMybatis.xml  
          src/main/java下包结构为:com.hsinghsu.testSSI.model、com.hsinghsu.testSSI.dao、com.hsinghsu.testSSI.dao.impl、com.hsinghsu.testSSI.data、com.hsinghsu.testSSI.action。  
       iii.修改web的输出目录,Java Build Path - Default output folder:testSSI/WebContent/WEB-INF/classes。  
       iv.添加jar包时将jar包放在WebContent/WEB-INF/lib文件夹下。

  
一、框架下载  
1、Struts2:http://struts.apache.org/download   
2、Spring:http://www.springsource.org/download/community  
3、mybatis:https://code.google.com/p/mybatis/downloads/detail?name=mybatis-3.2.1.zip  
mybatis-spring: https://code.google.com/p/mybatis/downloads/list?q=label:Product-Spring  
  
二、创建项目,添加jar包  
1、创建java web项目testSSI.  
2、添加struts包:

aopalliance-1.0.jar、  
freemarker-2.3.19.jar,  
ognl-3.0.5.jar,  
struts2-core-2.3.4.jar,  
struts2-spring-plugin-2.3.4.jar,  
xwork-core-2.3.4.jar,  
commons-fileupload-1.2.2.jar、  
commons-logging-api-1.1.jar、  
commons-io-2.0.1.jar、  
commons-lang-2.4.jar、  
commons-lang3-3.1.jar、  
commons-logging-api-1.1.jar。

3、添加spring包:

spring-aop-3.2.1.RELEASE.jar、  
spring-aspects-3.2.1.RELEASE.jar、  
spring-beans-3.2.1.RELEASE.jar、  
spring-context-3.2.1.RELEASE.jar、  
spring-context-support-3.2.1.RELEASE.jar、  
spring-core-3.2.1.RELEASE.jar、  
spring-expression-3.2.1.RELEASE.jar、  
spring-jdbc-3.2.1.RELEASE.jar、  
spring-orm-3.2.1.RELEASE.jar、  
spring-test-3.2.1.RELEASE.jar、  
spring-tx-3.2.1.RELEASE.jar、  
spring-web-3.2.1.RELEASE.jar。

4、添加ibatis包:

mybatis-3.2.1.jar  
mybatis-spring-1.2.0.jar  

5、添加其他包:

ojdbc14.jar 【下载地址 http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html 】  
aspectjweaver.jar 【下载地址 http://search.maven.org/remotecontent?filepath=org/aspectj/aspectjweaver/1.6.12/aspectjweaver-1.6.12.jar 】  
javassist-3.15.0-GA.jar  
c3p0-0.9.1.jar  【下载地址:https://code.google.com/p/aicms/downloads/detail?name=c3p0-0.9.1.jar&can=2&q=】

三、配置struts  
1、配置struts核心过滤器。  
打开web.xml,配置struts核心过滤器和过滤器映射。  
web.xml文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    <display-name></display-name>  
      
    <!-- 定义Struts 2的核心控制器 -->   
    <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>*.action</url-pattern>  
    </filter-mapping>  
  
    <listener>  
        <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>  
    </listener>  
  
    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
        <welcome-file>index.htm</welcome-file>  
        <welcome-file>index.jsp</welcome-file>  
        <welcome-file>default.html</welcome-file>  
        <welcome-file>default.htm</welcome-file>  
        <welcome-file>default.jsp</welcome-file>  
    </welcome-file-list>  
  
</web-app>

2、建立BaseAction。  
新建包com.hsinghsu.testSSI.action,在其中建立类BaseAction,继承ActionSupport类。  
BaseAction.java文件代码如下:

package com.hsinghsu.testSSI.action;  
  
import com.opensymphony.xwork2.ActionSupport;  
  
public class BaseAction  extends ActionSupport  
  
    /**  
     *   
     */  
    private static final long serialVersionUID = 4565394360208096613L;  
  

3、编写测试struts的Action。  
在包com.hsinghsu.testSSI.action中建立LoginAction类,继承BaseAction,作为简单的struts测试。  
LoginAction.java文件代码如下:

package com.hsinghsu.testSSI.action;  
  
public class LoginAction extends BaseAction   
  
    /**  
     *   
     */  
    private static final long serialVersionUID = -6144396976094110475L;  
      
    public String execute() throws Exception   
        
        return "success";    
        
  

4、配置action。  
在struts.xml文件中,添加对LoginAction的配置。  
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.3.dtd">  
  
<struts>  
    <constant name="struts.objectFactory" value="spring" />  
    <constant name="struts.i18n.encoding" value="UTF-8" />  
    <constant name="struts.i18n.reload" value="false" />  
    <constant name="struts.configuration.xml.reload" value="false" />  
    <constant name="struts.custom.i18n.resources" value="globalMessages" />  
    <constant name="struts.action.extension" value="action,," />  
  
    <constant name="struts.convention.package.locators.basePackage"  
              value="com.hsinghsu.testSSI.action" />  
    <constant name="struts.convention.result.path" value="/" />  
    <constant name="struts.serve.static" value="true" />  
    <constant name="struts.serve.static.browserCache" value="false" />  
      
      
    <package name="com.hsinghsu.testSSI.action.test" extends="struts-default">  
        <action name="login" class="com.hsinghsu.testSSI.action.LoginAction">  
            <result name="success">/jsp/welcome.jsp</result>  
        </action>  
    </package>  
      
</struts>

5、建立测试jsp页面  
新建测试jsp页面welcome.jsp。  
welcome.jsp文件代码如下:

<%@ page language="java" pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title>IWelcome</title>  
</head>  
<body>  
Welcome HsingHsu  
</body>  
</html> 

6、配置spring  
打开applicationContext.xml文件,配置spring,添加action bean。  
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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
    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.1.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
       ">  
         
    <bean id="loginAction" class="com.hsinghsu.testSSI.action.LoginAction"/>   
       
</beans>

7、配置Spring监听器  
打开web.xml,添加Spring监听器。  
web.xml文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    <display-name></display-name>  
      
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
        classpath:applicationContext.xml  
        </param-value>  
    </context-param>  
  
    <!-- ssh 中文过滤 -->  
    <filter>  
        <filter-name>characterEncoding</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>characterEncoding</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
  
    <!-- 定义Struts 2的核心控制器 -->   
    <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>*.action</url-pattern>  
    </filter-mapping>  
  
    <!-- 对Spring 容器进行实例化 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <listener>  
        <listener-class>org.apache.struts2.dispatcher.ng.listener.StrutsListener</listener-class>  
    </listener>  
  
    <welcome-file-list>  
        <welcome-file>index.html</welcome-file>  
        <welcome-file>index.htm</welcome-file>  
        <welcome-file>index.jsp</welcome-file>  
        <welcome-file>default.html</welcome-file>  
        <welcome-file>default.htm</welcome-file>  
        <welcome-file>default.jsp</welcome-file>  
    </welcome-file-list>  
  
</web-app>

8、配置tomcat并启动,在浏览器中输入http://localhost:8686/testSSI/login.action   

   则页面会显示Welcome HsingHsu。

 

四、配置ibatis  

1、建立数据库与数据表  
本文采用oracle数据库,建立T_TEST_USER表,新建列ID,NAME,PWD,AGE,SEX。插入一条数据,如id:1,name:hsing,pwd:hsu。 

2、配置ibatis数据库连接等配置。新建jdbc.oracle.properties文件。  
jdbc.oracle.properties文件代码如下:

 
ibatis.connection.driver_class oracle.jdbc.driver.OracleDriver  
ibatis.connection.url jdbc:oracle:thin:@10.127.58.1:1521:ora11g  
ibatis.connection.username adm  
ibatis.connection.password adm  
  
c3p0.acquireIncrement=3  
c3p0.idleConnectionTextPeriod=900  
c3p0.minPoolSize=2  
c3p0.maxPoolSize=50  
c3p0.maxStatements=100  
c3p0.numHelperThreads=10  
c3p0.maxIdleTime=600  
c3p0.initialPoolSize=3 

3、配置spring,添加ibatis支持。  
修改applicationContext.xml文件,添加ibatis支持。  
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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
    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.1.xsd  
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd  
       ">  
  
    <bean id="placeholderConfig"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location">  
            <value>classpath:jdbc.oracle.properties</value>  
        </property>  
    </bean>  
  
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="jdbcUrl" value="$ibatis.connection.url" />  
        <property name="driverClass" value="$ibatis.connection.driver_class" />  
        <property name="user" value="$ibatis.connection.username" />  
        <property name="password" value="$ibatis.connection.password" />  
        <property name="acquireIncrement" value="$c3p0.acquireIncrement" />  
        <property name="initialPoolSize" value="$c3p0.initialPoolSize" />  
        <property name="minPoolSize" value="$c3p0.minPoolSize" />  
        <property name="maxPoolSize" value="$c3p0.maxPoolSize" />  
        <property name="maxStatements" value="$c3p0.maxStatements" />  
        <property name="numHelperThreads" value="$c3p0.numHelperThreads" />  
        <property name="maxIdleTime" value="$c3p0.maxIdleTime" />  
    </bean>  
  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="configLocation" value="classpath:sqlMapConfigMybatis.xml" />  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <bean id="abstractDao" abstract="true">  
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
    </bean>  
      
    <bean id="userDao"  
        class="com.hsinghsu.testSSI.dao.impl.UserDaoImpl"  
        parent="abstractDao" />  
  
    <context:component-scan base-package="com.hsinghsu.testSSI">  
        <context:exclude-filter type="annotation"  
            expression="org.springframework.stereotype.Controller" />  
    </context:component-scan>  
  
</beans>

4、配置ibatis映射文件  
新建sqlMapConfigMybatis.xml,添加ibatis映射文件  
代码如下:

 

<?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>  
    <mappers>  
        <!-- 配置sql语句文件 -->  
        <mapper resource="com/hsinghsu/testSSI/data/User.xml" />  
    </mappers>  
</configuration>

5、创建持久层实体类。  
创建包com.hsinghsu.testSSI.model,创建User类,添加5个私有成员,对应数据表中5个属性,并一定要添加set和get方法。  
User.java文件代码如下:

 
package com.hsinghsu.testSSI.model;  
  
public class User   
  
    private Long id;  
    private String name;  
    private String pwd;  
    private Long age;  
    private Long sex;  
  
    public Long getId()   
        return id;  
      
  
    public void setId(Long id)   
        this.id = id;  
      
  
    public String getName()   
        return name;  
      
  
    public void setName(String name)   
        this.name = name;  
      
  
    public String getPwd()   
        return pwd;  
      
  
    public void setPwd(String pwd)   
        this.pwd = pwd;  
      
  
    public Long getAge()   
        return age;  
      
  
    public void setAge(Long age)   
        this.age = age;  
      
  
    public Long getSex()   
        return sex;  
      
  
    public void setSex(Long sex)   
        this.sex = sex;  
         
  
 

6、配置User映射文件  
创建包com.hsinghsu.testSSI.data。该包主要放置对应类的映射文件。  
新建User.xml,添加其sql的映射  
代码如下:

 
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"    
     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  
<mapper namespace="UserMapper">  
  
    <!-- select 语句,传参一个,返回User对象 。注:select id as id,name as name,pwd as pwd   
        的as后面的名称要与User类中属性名称要一一对应 -->  
    <select id="getUserByName" parameterType="String"  
        resultType="com.hsinghsu.testSSI.model.User">  
        select  
        id as id,name as name,pwd as pwd  
        from T_TEST_USER  
        WHERE NAME = #name  
    </select>  
  
    <insert id="insertUser" parameterType="com.hsinghsu.testSSI.model.User">  
        insert into  
        T_TEST_USER(ID,NAME,PWD,AGE,SEX)  
        values(#id,#name,#pwd,#age,#sex)  
    </insert>  
  
</mapper>

五、编写java代码  
1、编写DAO接口  
新建包com.hsinghsu.testSSI.dao,创建接口UserDao。  
UserDao.java文件代码如下:

 
package com.hsinghsu.testSSI.dao;  
  
import com.hsinghsu.testSSI.model.User;  
  
public interface UserDao   
      
    public User getUserByName(String name);  
  
 

2、编写DAO实现  
新建包com.hsinghsu.testSSI.dao.impl,创建实现类UserDaoImpl。  
UserDaoImpl.java文件代码如下:

 
package com.hsinghsu.testSSI.dao.impl;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import org.mybatis.spring.support.SqlSessionDaoSupport;  
import org.springframework.stereotype.Repository;  
  
import com.hsinghsu.testSSI.dao.UserDao;  
import com.hsinghsu.testSSI.model.User;  
  
@Repository(value = "userDao")  
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao   
  
    @Override  
    public User getUserByName(String name)   
  
        User user = new User();  
  
        // select语句,传入一个参数,返回User对象  -------------------------------------------------------------------------  
        user = getSqlSession().selectOne("UserMapper.getUserByName", name);// select语句,传入一个参数,返回User对象  
  
        //insert语句,传入一个对象  -------------------------------------------------------------------------------------  
        User oneUser = new User();  
        oneUser.setId(Long.parseLong("6"));  
        oneUser.setName("go07");  
        oneUser.setPwd("pesd07");  
        oneUser.setAge(Long.parseLong("25"));  
        oneUser.setSex(Long.parseLong("1"));  
        //int result = getSqlSession().insert("UserMapper.insertUser",oneUser);//insert语句,传入一个对象  
        //System.out.println("insert result:"+result);  
          
  
        return user;  
      
  
 

3、修改action类  
编辑修改LoginAction类。  
LoginAction.java文件代码如下:

 
package com.hsinghsu.testSSI.action;  
  
import org.springframework.beans.factory.annotation.Autowired;  
  
import com.hsinghsu.testSSI.dao.UserDao;  
import com.hsinghsu.testSSI.model.User;  
  
public class LoginAction extends BaseAction   
  
    /**  
     *   
     */  
    private static final long serialVersionUID = -439437585357651788L;  
  
    @Autowired  
    private UserDao userDao;  
  
    private User user;  
  
    private String username;  
    private String password;  
  
    public String execute() throws Exception   
        user = userDao.getUserByName(username);  
        if (user != null)   
            if (user.getPwd().equals(password))   
                return SUCCESS;  
              
          
        return ERROR;  
      
  
    public String getUsername()   
        return username;  
      
  
    public void setUsername(String username)   
        this.username = username;  
      
  
    public String getPassword()   
        return password;  
      
  
    public void setPassword(String password)   
        this.password = password;  
      
  
  

4、新建用户登录页面。  
添加login.jsp。  
login.jsp文件代码如下:

 

<%@ page language="java" pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title>Insert title here</title>  
</head>  
<body>  
    <form action="mylogin.action" method="post">    
        Username:<input type="text" name="username"/><br/>    
        Password:<input type="password" name="password"/><br/>    
        <input type="submit" value="Submit"/>     
    </form>  
</body>  
</html>

5、修改struts.xml配置文件  
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.3.dtd">  
  
<struts>  
    <constant name="struts.objectFactory" value="spring" />  
    <constant name="struts.i18n.encoding" value="UTF-8" />  
    <constant name="struts.i18n.reload" value="false" />  
    <constant name="struts.configuration.xml.reload" value="false" />  
    <constant name="struts.custom.i18n.resources" value="globalMessages" />  
    <constant name="struts.action.extension" value="action,," />  
  
    <constant name="struts.convention.package.locators.basePackage"  
              value="com.hsinghsu.testSSI.action" />  
    <constant name="struts.convention.result.path" value="/" />  
    <constant name="struts.serve.static" value="true" />  
    <constant name="struts.serve.static.browserCache" value="false" />  
      
      
    <package name="com.hsinghsu.testSSI.action.test" extends="struts-default">  
        <action name="login" class="com.hsinghsu.testSSI.action.LoginAction">  
            <result name="success">/jsp/welcome.jsp</result>  
        </action>  
          
        <action name="mylogin" class="com.hsinghsu.testSSI.action.LoginAction">   
            <result name="success">/jsp/welcome.jsp</result>  
            <result name="error">/jsp/login.jsp</result>  
        </action>  
    </package>  
      
</struts>

6、运行tomcat,在浏览器中输入http://localhost:8686/testSSI/jsp/login.jsp 会显示登录页面,如果成功会显示welcome 。失败则会继续要求登录。  

以上是关于SSI框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

java SSH框架 SSM框架 SSI框架 源码 Java全新高大尚HTML5

java:Mybatis框架3(二级缓存,延时和积极加载,SSI(Ibatis)集成,SSM集成)

PostgreSQL串行化隔离级别(SSI)的能力与实现

java互联网架构

关于技术框架

STM32 WEB服务器经常用到CGI SSI,可是为啥这方面一个具体的应用教程都没有?