SSH环境搭建

转载来源于:http://blog.csdn.net/xuefeng0707/article/details/8888484

1. 资源准备

Struts-2.3.4.1
Spring-3.0.5
Hibernate-3.6.10

2. 新建空白Web项目

新建一个空白的Web项目,取名为ssh,目录结构如图:


部署到Tomcat中:


3. 配置Struts2

复制Struts2必需的jar文件到WEB-INF/lib中,并修改web.xml,加入Struts2的filter:



添加一个测试Action,并配置struts.xml:


LoginAction的代码如下:

  1. package com.xuefeng.ssh.action; 
  2.  
  3. import com.opensymphony.xwork2.Action; 
  4. import com.xuefeng.ssh.service.LoginService; 
  5. import com.xuefeng.ssh.service.impl.LoginServiceImpl; 
  6.  
  7. public class LoginAction implements Action { 
  8.  
  9.     // 对应于login.jsp的输入,需要getter和setter 
  10.     private String username; 
  11.  
  12.     private String password; 
  13.  
  14.     /**
  15.      * 验证登录的逻辑
  16.      */ 
  17.     private LoginService loginService; 
  18.      
  19.     public LoginAction() { 
  20.         loginService = new LoginServiceImpl(); 
  21.     } 
  22.  
  23.     public String execute() throws Exception { 
  24.         if (loginService.validate(username, password)) { 
  25.             return SUCCESS; 
  26.         } 
  27.         return INPUT; 
  28.     } 
  29.  
  30.     public String getUsername() { 
  31.         return username; 
  32.     } 
  33.  
  34.     public void setUsername(String username) { 
  35.         this.username = username; 
  36.     } 
  37.  
  38.     public String getPassword() { 
  39.         return password; 
  40.     } 
  41.  
  42.     public void setPassword(String password) { 
  43.         this.password = password; 
  44.     } 
package com.xuefeng.ssh.action;

import com.opensymphony.xwork2.Action;
import com.xuefeng.ssh.service.LoginService;
import com.xuefeng.ssh.service.impl.LoginServiceImpl;

public class LoginAction implements Action {

	// 对应于login.jsp的输入,需要getter和setter
	private String username;

	private String password;

	/**
	 * 验证登录的逻辑
	 */
	private LoginService loginService;
	
	public LoginAction() {
		loginService = new LoginServiceImpl();
	}

	public String execute() throws Exception {
		if (loginService.validate(username, password)) {
			return SUCCESS;
		}
		return INPUT;
	}

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


login.jsp代码如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"%> 
  2. <html> 
  3.     <head> 
  4.         <title>登录页面</title> 
  5.     </head> 
  6.      
  7.     <body> 
  8.         <form action="login.action" method="post"> 
  9.             <table cellpadding="0" cellspacing="0" norder="0" align="center"> 
  10.                 <caption>用户登录</caption> 
  11.                 <tr> 
  12.                     <td>用户名:</td> 
  13.                     <td><input type="text" name="username"></td> 
  14.                 </tr> 
  15.                 <tr> 
  16.                     <td>密  码:</td> 
  17.                     <td><input type="text" name="password"></td> 
  18.                 </tr> 
  19.                 <tr> 
  20.                     <td colspan="2"><input type="submit" value="登录"></td>  
  21.                 </tr> 
  22.             </table> 
  23.         </form> 
  24.     </body> 
  25. </html> 
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
	<head>
		<title>登录页面</title>
	</head>
	
	<body>
		<form action="login.action" method="post">
			<table cellpadding="0" cellspacing="0" norder="0" align="center">
				<caption>用户登录</caption>
				<tr>
					<td>用户名:</td>
					<td><input type="text" name="username"></td>
				</tr>
				<tr>
					<td>密  码:</td>
					<td><input type="text" name="password"></td>
				</tr>
				<tr>
					<td colspan="2"><input type="submit" value="登录"></td>	
				</tr>
			</table>
		</form>
	</body>
</html>


login.jsp里的input的name需要和LoginAction里的属性名一致。

这样,此时访问登录页面进行测试:


登录成功后,按照struts.xml里的设置,跳转(redirect)到success.jsp:


这里的测试账号在LoginServiceImpl中设置为test和test,如果输错,按照struts.xml里的设置,回到登录界面。

4. 配置Spring

4.1 添加Spring支持

添加Spring需要的jar和配置文件applicationContext.xml。jar可以从Struts的lib中复制,也可以到Spring的官网下载Spring的RELEASE。


然后,修改web.xml,添加Spring支持:

  1. <listener> 
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  3. </listener> 
	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


 

4.2 Struts集成Spring

Struts是以plugin的方式来集成Spring的,添加struts2-spring-plugin-2.3.4.1.jar,在Struts初始化时,会去加载该jar中包含的struts-plugin.xml,以StrutsSpringObjectFactory取代默认的StrutsObjectFactory,从而达到以Spring容器管理对象的目的。

然后,修改applicationContext.xml,配置已有的Action对象:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
  4.  
  5. <beans> 
  6.     <bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction" scope="prototype"></bean> 
  7. </beans> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction" scope="prototype"></bean>
</beans>


最后,修改struts.xml中Action的引用,把Action的class指定为Spring中bean的id,这样当创建Action的时候,就会以Spring的方式来创建对象了:

  1. <action name="login" class="loginAction"> 
  2.     <result name="input" type="redirect">/login.jsp</result> 
  3.     <result name="success" type="redirect">/success.jsp</result> 
  4. </action> 
		<action name="login" class="loginAction">
			<result name="input" type="redirect">/login.jsp</result>
			<result name="success" type="redirect">/success.jsp</result>
		</action>



4.3 以Spring管理对象创建方式

把Action对象和Spring管理的对象结合起来,在applicationContext.xml中配置:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
  4.  
  5. <beans> 
  6.     <bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction" scope="prototype"> 
  7.         <property name="loginService" ref="loginService"></property> 
  8.     </bean> 
  9.     <bean id="loginService" class="com.xuefeng.ssh.service.impl.LoginServiceImpl"></bean> 
  10. </beans> 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction" scope="prototype">
		<property name="loginService" ref="loginService"></property>
	</bean>
	<bean id="loginService" class="com.xuefeng.ssh.service.impl.LoginServiceImpl"></bean>
</beans>


这样,就不需要再LoginAction中显示的对loginService赋值了。


5. 配置Hibernate

5.1 添加必需jar


5.2 添加映射文件和DAO层代码


5.3 使用Spring配置Hibernate

修改applicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"  
  3.     "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
  4.  
  5. <beans> 
  6.     <bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction" 
  7.         scope="prototype"> 
  8.         <property name="loginService" ref="loginService"></property> 
  9.     </bean> 
  10.     <bean id="loginService" class="com.xuefeng.ssh.service.impl.LoginServiceImpl"> 
  11.         <property name="userDao" ref="userDao" /> 
  12.     </bean> 
  13.  
  14.     <bean id="dataSource" 
  15.         class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
  16.         <property name="driverClassName"> 
  17.             <value>com.mysql.jdbc.Driver</value> 
  18.         </property> 
  19.         <property name="url"> 
  20.             <value>jdbc:mysql://localhost:3306/ssh</value> 
  21.         </property> 
  22.         <property name="username"> 
  23.             <value>root</value> 
  24.         </property> 
  25.         <property name="password"> 
  26.             <value>root</value> 
  27.         </property> 
  28.  
  29.     </bean> 
  30.  
  31.     <bean id="sessionFactory" 
  32.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  33.         <property name="dataSource" ref="dataSource"> 
  34.         </property> 
  35.         <property name="mappingResources"> 
  36.             <list> 
  37.                 <value>com/xuefeng/ssh/model/User.hbm.xml</value> 
  38.             </list> 
  39.         </property> 
  40.         <property name="hibernateProperties"> 
  41.             <props> 
  42.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
  43.                 <prop key="hibernate.hbm2ddl.auto">update</prop> 
  44.             </props> 
  45.         </property> 
  46.     </bean> 
  47.  
  48.     <bean id="userDao" class="com.xuefeng.ssh.dao.impl.UserDaoHibernateImpl"> 
  49.         <property name="sessionFactory" ref="sessionFactory" /> 
  50.     </bean> 
  51. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="loginAction" class="com.xuefeng.ssh.action.LoginAction"
		scope="prototype">
		<property name="loginService" ref="loginService"></property>
	</bean>
	<bean id="loginService" class="com.xuefeng.ssh.service.impl.LoginServiceImpl">
		<property name="userDao" ref="userDao" />
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/ssh</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>root</value>
		</property>

	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource">
		</property>
		<property name="mappingResources">
			<list>
				<value>com/xuefeng/ssh/model/User.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>

	<bean id="userDao" class="com.xuefeng.ssh.dao.impl.UserDaoHibernateImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
</beans> 


对象的引用链为:

loginAction - loginService - userDao - sessionFactory - dataSource


工程代码下载:http://download.csdn.net/detail/xuefeng0707/5336394

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值