Ssh統合開発の紹介と簡単なログイン事例の実現
20468 ワード
Ssh
:
Ssh strtus2-2.3.1.2+ spring-2.5.6+hibernate-3.6.8 , , mySql。 eclipse,eplipse Indigo Service Release 2
1. struts2 sping hibernate
(1)struts2 :
struts-2.3.1.2\lib\ struts2-core-2.3.1.2.jar
struts-2.3.1.2\lib\ognl-3.0.4.jar
struts-2.3.1.2\lib\ xwork-core-2.3.1.2.jar
struts-2.3.1.2\lib\ freemarker-2.3.18.jar
struts-2.3.1.2\lib\commons-fileupload-1.2.2.jar
struts-2.3.1.2\lib\ commons-io-2.0.1.jar
struts-2.3.1.2\lib\commons-lang-2.5.jar
struts-2.3.1.2\lib\commons-logging-1.1.1.jar
struts-2.3.1.2\lib \struts2-json-plugin-2.3.1.2.jar
struts-2.3.1.2\lib \struts2-spring-plugin-2.3.1.2.jar
(2) hibernate
hibernate-distribution-3.6.8.Final\lib\required\*.jar jar
hibernate-distribution-3.6.8.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-distribution-3.6.8.Final\ hibernate3.jar
(3)spring :
spring-framework-2.5.6\dist\ spring.jar
spring-framework-2.5.6\lib\c3p0\c3p0-0.9.1.2.jar
spring-framework-2.5.6\lib\aspectj\*.jar
spring-framework-2.5.6\lib\j2ee\common-annotations.jar
spring-framework-2.5.6\lib\log4j\log4j-1.2.15.jar
spring-framework-2.5.6\lib\jakarta-commons\ commons-logging.jar
spring-framework-2.5.6\lib\cglib\cglib-nodep-2.1_3.jar
spring-framework-2.5.6\lib\dom4j\dom4j-1.6.1.jar
2. spring
(1) spring xml
class , src applicationContext.xml xml , , , :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
(2) xml , c3p0
<!-- -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="1234" />
<!-- , minPoolSize maxPoolSize 。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!-- 。 -->
<property name="minPoolSize" value="1" />
<!-- 。Default: 15 -->
<property name="maxPoolSize" value="300" />
<!-- ,60 。 0 。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!-- c3p0 。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!-- 60 。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
, properties 。 xml :
<!-- -->
<context:property-placeholder location="jdbc.properties" />
(3) sessionFactory , hibernate.cfg.xml
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- -->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate -->
<property name="mappingResources">
<list>
<value>cn/csdn/hr/s2sh/domain/Admin.hbm.xml</value>
</list>
</property>
<!-- hibernate -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
3. struts2.xml
(1) struts2.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- spring struts2 action -->
<constant name="struts.objectFactory" value="spring"/>
<include file="struts-admin.xml"></include>
</struts>
: include , :
<constant name="struts.objectFactory" value="spring"/>, struts2 spring
(2) struts-admin.xml ,
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="admin" extends="struts-default" namespace="/csdn">
<!-- class spring action id -->
<action name="loginAdmin" class="adminAction" method="login">
<result name="success">../index.jsp</result>
</action>
</package>
</struts>
4. Hibernate.cfg.xml , spring xml
5.
(1) domain, cn.csdn.hr.s2sh.domain, admin
id name pass
:
package cn.csdn.hr.s2sh.domain;
import java.io.Serializable;
public class Admin implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String pass;
public Admin() {
super();
// TODO Auto-generated constructor stub
}
public Admin(Integer id, String name, String pass) {
super();
this.id = id;
this.name = name;
this.pass = pass;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + "]";
}
}
(2)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.csdn.hr.s2sh.domain">
<class name="Admin" table="admin">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" column="name"></property>
<property name="pass" column="pass"></property>
</class>
</hibernate-mapping>
(3) dao , cn.csdn.hr.s2sh.dao, AdminDao, AdminDaoImpl
AdminDao.java
package cn.csdn.hr.s2sh.dao;
import java.util.List;
import cn.csdn.hr.s2sh.domain.Admin;
public interface AdminDao {
public Admin login(final String name,final String pass);
}
AdminDaoImpl.java
package cn.csdn.hr.s2sh.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import cn.csdn.hr.s2sh.domain.Admin;
@SuppressWarnings("unchecked")
public class AdminDaoImpl extends HibernateDaoSupport implements AdminDao {
public Admin login(final String name, final String pass) {
return (Admin) this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
// TODO Auto-generated method stub
Object obj = session
.createQuery(
"from Admin where name=:name and pass=:pass")
.setString("name", name)
.setString("pass", pass).uniqueResult();
return obj;
}
});
}
}
(4) service , cn.csdn.hr.s2sh.service
AdminService.java
package cn.csdn.hr.s2sh.service;
import cn.csdn.hr.s2sh.dao.AdminDao;
public interface AdminService extends AdminDao{
}
AdminServiceImpl.java
package cn.csdn.hr.s2sh.service;
import java.util.List;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import cn.csdn.hr.s2sh.dao.AdminDao;
import cn.csdn.hr.s2sh.domain.Admin;
public class AdminServiceImpl implements AdminService {
private AdminDao adminDao;
public void setAdminDao(AdminDao adminDao) {
this.adminDao = adminDao;
}
public Admin login(String name, String pass) {
// TODO Auto-generated method stub
return adminDao.login(name, pass);
}
}
6. struts2 action, action bean-action.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- struts2 action -->
<!-- admin action -->
<bean id="adminAction" class="cn.csdn.hr.s2sh.action.AdminAction"
scope="prototype">
<property name="adminService" ref="adminServiceImpl"></property>
</bean>
</beans>
ref adminServiceImpl bean-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- admin -->
<bean id="adminServiceImpl" class="cn.csdn.hr.s2sh.service.AdminServiceImpl">
<property name="adminDao" ref="adminDaoImpl"></property>
</bean>
</beans>
ref adminDaoImpl, beans-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- hibernatedao HibernateDaoSuppor abstract="true" -->
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- admin dao -->
<bean id="adminDaoImpl" class="cn.csdn.hr.s2sh.dao.AdminDaoImpl"
parent="hibernateDaoSupport" />
</beans>
beans.xml applicationContext.xml
<!-- -->
<import resource="beans-dao.xml" />
<import resource="beans-service.xml" />
<import resource="beans-action.xml" />
7. 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>s2sh</display-name>
<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>
<!-- spring , web , spring classpath: -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applic*.xml</param-value>
</context-param>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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>
<!-- spring hibernate session -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<!-- org.springframework.orm.hibernate3.LocalSessionFactoryBean spring , sessionFactory. LocalSessionFactoryBean spring sessionFactory, , sessionFactory -->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring struts2 -->
<filter>
<filter-name>encoding</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>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
8. , jsp :
<body>
<div>
<form action="/s2sh/csdn/loginAdmin.action" method="get">
:<input type="text" name="admin.name"/><br/>
:<input type="password" name="admin.pass"/><br/>
<input type="submit" value=" "/>
<input type="reset" value=" "/>
</form>
</div>
<div>
:${admin.name}
</div>
</body>
action :
package cn.csdn.hr.s2sh.action;
import cn.csdn.hr.s2sh.domain.Admin;
import cn.csdn.hr.s2sh.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;
public class AdminAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private AdminService adminService;
private Admin admin;
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
//
public void setAdminService(AdminService adminService) {
this.adminService = adminService;
}
public String login() {
System.out.println(" :" + admin.getName());
admin = adminService.login(admin.getName(), admin.getPass());
return SUCCESS;
}
}