SSH简单整合(Struts2.2.3.1+Spring3.1.0+Hibernate4.0)

0.数据库创建脚本

CREATE TABLE `code_store` (
  `id` int(11) NOT NULL,
  `category` int(11) default NULL,
  `name` varchar(45) default NULL,
  `codetext` text,
  `describe` varchar(45) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

1.分别从ssh官网上下载ssh,解压缩后需要的包如下图所示

SSH简单整合(Struts2.2.3.1+Spring3.1.0+Hibernate4.0)_第1张图片

2:项目结构图

SSH简单整合(Struts2.2.3.1+Spring3.1.0+Hibernate4.0)_第2张图片

3.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">
	<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配置文件在何处 -->
  <context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml,classpath*:applicationContext*.xml</param-value>
  </context-param>
   <!--  加载spring配置文件applicationContext.xml  --> 
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

	
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

4.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>
   <constant name="struts.i18n.encoding" value="utf-8"></constant>
   <constant name="struts.objectFactory" value="spring"></constant>
   
	<package name="base"  extends="struts-default" namespace="/">
		<action name="login" class="com.struts2.action.LoginAction">
			<result name="success">/welcome.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
	</package>
</struts>

5.ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC    
    "-//SPRING//DTD BEAN//EN"   
    "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>  
	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/yiiblog?useUnicode=true&amp;characterEncoding=utf8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean> 
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 映射文件 -->
		<property name="mappingResources">
			<list>
				<value>com/struts2/model/CodeStore.hbm.xml</value>
			
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<!-- 指定Hibernate的连接方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 配置启动应用时,是否根据Hibernate映射自动创建数据表 -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>
	
	
	
	
	
	<bean id="loginService" class="com.struts2.service.LoginService"></bean>
    <bean id="hello" class="com.struts2.action.LoginAction">
    	<property name="loginService" ref="loginService"></property>
    </bean>  
</beans>  
	<!-- 事务管理器 -->
	<!-- 
	<bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	 -->
	
	<!-- 定义Spring JDBC模板类bean -->
	<!--  
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	-->

6.CodeStore.java

package com.struts2.model;

/**
 * CodeStore entity. @author MyEclipse Persistence Tools
 */

public class CodeStore implements java.io.Serializable {

	// Fields

	private Integer id;
	private Integer category;
	private String name;
	private String codetext;
	private String describe;

	// Constructors

	/** default constructor */
	public CodeStore() {
	}

	/** minimal constructor */
	public CodeStore(Integer id) {
		this.id = id;
	}

	/** full constructor */
	public CodeStore(Integer id, Integer category, String name,
			String codetext, String describe) {
		this.id = id;
		this.category = category;
		this.name = name;
		this.codetext = codetext;
		this.describe = describe;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getCategory() {
		return this.category;
	}

	public void setCategory(Integer category) {
		this.category = category;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCodetext() {
		return this.codetext;
	}

	public void setCodetext(String codetext) {
		this.codetext = codetext;
	}

	public String getDescribe() {
		return this.describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

}

7.CodeStore.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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.struts2.model.CodeStore" table="code_store" catalog="codestore">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="category" type="java.lang.Integer">
            <column name="category" />
        </property>
        <property name="name" type="java.lang.String">
            <column name="name" length="45" />
        </property>
        <property name="codetext" type="java.lang.String">
            <column name="codetext" length="65535" />
        </property>
        <property name="describe" type="java.lang.String">
            <column name="describe" length="45" />
        </property>
    </class>
</hibernate-mapping>

8.LoginService.java

package com.struts2.service;

public class LoginService {
	public boolean validate(String username,String password) throws Exception{
		boolean flag=false;
		if(username.equals("tom")&&password.equals("111111")){
			System.out.println("this is in service");
			flag=true;
		}
		return flag;
	}
}

9.LoginAction.java

package com.struts2.action;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.struts2.service.LoginService;

public class LoginAction extends ActionSupport implements Action{
	private static final long serialVersionUID  =   1L ;
	private String username;
	private String password;
	
	private LoginService loginService;
	
	@Override
	public void validate()
	{
		
	}
	
	
	public String execute() throws Exception
	{
		boolean result=this.loginService.validate(username, password);
		if(result){
			return this.SUCCESS;
		}else{
			return this.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;
	}

	public LoginService getLoginService() {
		return loginService;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	
	
	
}

 

你可能感兴趣的:(ssh,整合)