s 2 sh統合例Struts 2.0 Spring2.6 Hibernate3.2




Struts2 Spring2.6、Hibernate3.2         :

    :MyEclipse 8.5、Tomcat 6.0、MySql

  :

1.    WEB  ,   s2sh(     ,        );

2.  struts2   jar ,    WEB-INF  lib      jar   
commons-fileupload.jar
commons-io.jar
freemarker.jar
javassist.jar
ognl-2.6.11.jar
struts2-core-2.0.14.jar
struts2-spring-plugin-2.0.14.jar
xwork-2.0.7.jar

3.  Spring  (    MyEclipse Spring Capabilities …)  
             :  
    1) Spring version:Spring 2.5  
    2) Select the libraries to add the buildpath:  
    Spring 2.5 AOP Libraries  
    Spring 2.5 Core Libraries  
    Spring 2.5 Persistence Core Libraries  
    Spring 2.5 Persistence JDBC Libraries  
    Spring 2.5 Web Libraries  
           3) JAR Library Installation:  
    Copy checked Library contents to project folder (TLDs always copied)  
           Next  
    4) Spring        Folder:WebRoot/WEB-INF  
    Finish  


4.  Hibernate  (    MyEclipse Hibernate Capabilities …)  
             :  
           1)Hibernate Specification:Hibernate3.2  
           2)Select the libraries to ad to the buildpath:Hibernate 3.2 Core Libraries Hibernage 3.2 Annoations  
    3)JAR Library Installation:Copy checked Library Jars to project folder and add to build-path  
    Next  
    4)  :Spring configuration file(applicationContext.xml) Next  
           5)  :Existing Spring configuration file  
    SessionFactory Id: sessionFactory Next  
           6)Bean Id:dataSource  
    DB Driver:      Hibernate。Next  
           7)   SessionFactory(   Create SessionFactory class?)  
           8)Finish。  
	
	
5.  struts.xml  , src      struts.xml  ,       :

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<constant name="struts.objectFactory" value="spring" />
	<package name="default" extends="struts-default">
		<action name="registerAction" class="RegisterAction">
			<result name="fail">/login.jsp </result>
			<result name="success">/success.jsp</result>
		</action>
	</package>

</struts>

6.  web.xml  ,     :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
	<!-- Struts2   Spring    -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/applicationContext.xml
		</param-value>
	</context-param>

	<!--   Struts2 -->
	<filter>
		<filter-name>Struts2.0</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2.0</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!--   Spring Struts2        -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
</web-app>


7.         

 1).     ,    s2sh,     User    
      
       
    2). MyEclipse         ,   MyEclipse Database Explorer  
       
    3).          ,  New(             ,Driver Name s1sh,      ,          ),,         Next,       ,            (Display the selected Schemas),       Add  ,           ,        ,              ;  
      
      
           s1sh   ,  Open connection,        s2sh   TABLE,  TABLE,      User      ,  Hibernate Reverse Engineering(        ),          Java src folder        src  ,   Java Package                bean ,                 ,  Next,        ,         ,    Next,        ,  Finish,               。
	
8.  Action

 src       ,  "com.beyondsoft.action"(      ,    com   ,       struts  action    ),        action RegisterAction.java,     :

package com.beyondsoft.action;

import java.util.List;

import com.beyondsoft.bean.User;
import com.beyondsoft.bean.UserDAO;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String userName;
	private String password;
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	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;
	}

	public String execute() {
		userDAO.save(new User(userName,password));
		return "success";
	}
}

10.    ,      login.jsp,    :

<%@ page language="java" pageEncoding="gb2312"%>

<%@taglib uri="/struts-tags" prefix="s"%>

<html>
	<head>
		<title>    </title>
	</head>
	<body>
		<center>
			<div id="register">
				<s:form action="registerAction">
					<s:textfield label="   " name="userName"></s:textfield>
					<s:password label="  " name="password"></s:password>
					<s:submit value="  "></s:submit>
				</s:form>
			</div>

		</center>
	</body>
</html>

   ,   struts2   ,    form  。form           RegisterAction.java  , RegisterAction.java struts.xml      name  "registerAction”,     action  "loginAction"。               name   RegisterAction.java    userName password    。

11.  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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver">
		</property>
		<property name="url" value="jdbc:mysql://localhost:3306/s2sh"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/beyondsoft/bean/User.hbm.xml</value>
			</list>
		</property>
	</bean>

	<bean id="RegisterAction" class="com.beyondsoft.action.RegisterAction">
		<property name="userDAO">
			<ref bean="UserDAO" />
		</property>
	</bean>

	<bean id="UserDAO" class="com.beyondsoft.bean.UserDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
</beans>
 
  sam-2.2.3.jar  cglib-2.1.3.jar   
        Struts2 Spring2.6 Hibernate3.2       ,                       。