日期:2014-05-16  浏览次数:20813 次

Struts2 + Spring2.5 + Hibernate3.2 框架 + MySQL 用Eclipse手工搭建

最近跟着项目组做了两个好玩的项目,

一个是Struts2 + Hibernate3.2的B/S项目

另一个是CXF + Spring2.5 + Hibernate3.2的C/S项目

?

于是乎自己就特别想实际动手,用Eclipse来搭建Struts2 + Spring2.5 + Hibernate3.2 框架。

与大家一起分享,有的地方可以与我讨论,共通进步。

?

整体步骤是:

1、先加入Struts2

2、加入Spring2.5

3、加入Hibernate3.2

?

具体步骤是:

1、建个Web项目,而Eclipse里是Tomcat project

2、加入Struts2的核心包,(自己找吧网上很多)

3、写个web.xml放Web-Info下

	<listener>  
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
	</listener>  
       <filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

?4、写个三个很简单的jsp文件login.jsp/success.jsp/error.jsp

<body>
   	<s:form action="action_executeFunction" namespace="/">
   		名称:<s:textfield name="user.name"/>
   		密码:<s:textfield name="user.password"/>
   		<input type="submit" value="提交"/>
   	</s:form>
</body>

<body>
   	this is success page/
</body>

<body>
           this is error page.
</body>

?5、写个Struts.xml文件放src下

<!-- 配置中文支持 --> 
<constant name="struts.custom.i18n.resources" value="alternate" />
	
<!-- 配置Spring-->
<constant name="struts.objectFactory" value="spring" />

<package name="user" namespace="/" extends="struts-default">
 		<global-results>
 			<result name="message">/WEB-INF/page/message.jsp</result>
 		</global-results>
		<action name="action_*" method="{1}"
			class="com.***.action.LoginAction" >
			<result name="input">login.jsp</result>
			<result name="error">error.jsp</result>
			<result name="success">success.jsp</result>
		</action>
</package>

?6、写个action

public class LoginAction extends ActionBase{
	
	private User user  = new User();
	private ILoginBo loginBo ;
	
	public String input() throws Exception {

		this.user.setName("llll");
		return INPUT;
	}
	
	public void validateExecuteFunction() throws Exception{
		
		if(user.getName() == null || user.getPassword() == null ){
			this.redraw();
		}	
	}
	public String executeFunction() throws Exception {
		
		//ILoginBo loginBean = (ILoginBo)this.appContext.getBean("LoginBo");
		if(!loginBo.isExits(user.getName())){
			
			loginBo.addUser(user);
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String redraw() throws Exception {
		
		return ERROR;
	}
	public ILoginBo getLoginBo() {
		return loginBo;
	}
	public void setLoginBo(ILoginBo loginBo) {
		this.loginBo = loginBo;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

}

?7、以上这些文件对测试配好Struts2基本完成。其中还加入了一下配置Spring的应该能看出来在web.xml和Struts.xml中。

8、开始配置Spring,写个applicationContext.xml放Web-Info下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:amq="http://activemq.apache.org/schema/core"
	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.xsd
    http://activemq.apache.org/schema/core 
    http://activemq.apache.org/schema/core/activemq-core.xsd">
	
	<!-- 这里scope="prototype"代表Action层不是单例模式,每次请求都会新创建一个对象-->
	<bean id="loginActionBean" class="com.***.action.LoginAction" scope="prototype">
		<property name="loginBo" ref="loginBo"/>
		<property name="user" ref="userBean"/>