这个Web应用很简单,就是实现一个用户注册页面,当用户在register.jsp页面输入注册信息,点保存,如果成功,进入registerSuccess.jsp页面。这个应用虽然很小,但它也有三层组成,表示层由Struts框架负责,业务层由Spring框架负责,持久层由Hibernate框架负责。
开发环境:MyEclipse 5+ MySQL4 + Struts1.3.5 + Spring2.0.1 + Hibernate3.2.1 + Tomcat5.0.19 + jdk1.4.2_06.
1. 首先,我们在MyEclipse中新建一个Web Project ,login;
2.然后把Struts,Spring,Hibernate框架所需要的jar包以及MySQL的驱动拷贝到WEB-INF\lib下;
3.创建用户注册页面register.jsp
4.创建注册成功后显示的页面registerSuccess.jsp
5. 创建消息资源文件registermessages.properties
6. 由于我们使用动态ActionForm,只需在Struts-config.xml中配置与register.jsp页面中form对应的ActionForm。
7. 创建配置文件Struts-config.xml
8. 创建实体类Register.java
9.创建Hibernate映射文件,Register.hbm.xml
9. 创建Spring配置文件applicationContext.xml
10. 创建数据访问对象RegisterDAO.java
12.创建服务对象RegisterService.java
11. 创建web.xml
14.创建RegisterAction.java
在RegisterAction.java中,为了获取spring容器中的RegisterService对象,我们要对struts-config.xml作下修改,具体修改如下:
在struts-config.xml增加controller元素
目的告诉Struts自动将对Action的请求委托给Spring上下文中的Struts Action来处理。
并且为了让Struts能访问Spring容器,还必须在struts-config.xml注册一个能访问Spring上下文的插件:
action-servlets.xml文件的具体内容如下:
注意:这里bean是name属性,而不是id。
至此,一个完整的SSH(Struts,Spring,Hibernate)框架搭建好了。
开发环境:MyEclipse 5+ MySQL4 + Struts1.3.5 + Spring2.0.1 + Hibernate3.2.1 + Tomcat5.0.19 + jdk1.4.2_06.
1. 首先,我们在MyEclipse中新建一个Web Project ,login;
2.然后把Struts,Spring,Hibernate框架所需要的jar包以及MySQL的驱动拷贝到WEB-INF\lib下;
3.创建用户注册页面register.jsp
1
<%
@ taglib uri
=
"
http://struts.apache.org/tags-bean
"
prefix
=
"
bean
"
%>
2 <% @ taglib uri = " http://struts.apache.org/tags-html " prefix = " html " %>
3 < html >
4
5 < head >
6 < title >< bean:message key = " register.title " /></ title >
7 </ head >
8
9 < body >
10 < html:errors />
11 < html:form action = " register.do " >
12 < table >
13 < tr >
14 < td >< bean:message key = " register.username " /> : </ td >
15 < td >< html:text property = " register.username " /></ td >
16 </ tr >
17 < tr >
18 < td >< bean:message key = " register.password " /> : </ td >
19 < td >< html:password property = " register.password " /></ td >
20 </ tr >
21 < tr >
22 < td >< bean:message key = " register.email " /> : </ td >
23 < td >< html:text property = " register.email " /></ td >
24 </ tr >
25 < tr >
26 < td colspan = " 2 " align = " center " >
27 < html:submit >< bean:message key = " register.submit " /></ html:submit >
28 </ td >
29 </ tr >
30 </ table >
31 </ html:form >
32 </ body >
33
34 </ html >
2 <% @ taglib uri = " http://struts.apache.org/tags-html " prefix = " html " %>
3 < html >
4
5 < head >
6 < title >< bean:message key = " register.title " /></ title >
7 </ head >
8
9 < body >
10 < html:errors />
11 < html:form action = " register.do " >
12 < table >
13 < tr >
14 < td >< bean:message key = " register.username " /> : </ td >
15 < td >< html:text property = " register.username " /></ td >
16 </ tr >
17 < tr >
18 < td >< bean:message key = " register.password " /> : </ td >
19 < td >< html:password property = " register.password " /></ td >
20 </ tr >
21 < tr >
22 < td >< bean:message key = " register.email " /> : </ td >
23 < td >< html:text property = " register.email " /></ td >
24 </ tr >
25 < tr >
26 < td colspan = " 2 " align = " center " >
27 < html:submit >< bean:message key = " register.submit " /></ html:submit >
28 </ td >
29 </ tr >
30 </ table >
31 </ html:form >
32 </ body >
33
34 </ html >
4.创建注册成功后显示的页面registerSuccess.jsp
1
<%
@ taglib uri
=
"
http://struts.apache.org/tags-bean
"
prefix
=
"
bean
"
%>
2 < html >
3
4 < head >
5 < title >< bean:message key = " register.title " /></ title >
6 </ head >
7
8 < body >
9 < bean:message key = " register.success " />
10 </ body >
11
12 </ html >
13
2 < html >
3
4 < head >
5 < title >< bean:message key = " register.title " /></ title >
6 </ head >
7
8 < body >
9 < bean:message key = " register.success " />
10 </ body >
11
12 </ html >
13
5. 创建消息资源文件registermessages.properties
1
register.title
=
Register
2 register.username = UserName
3 register.password = Password
4 register.email = Email
5 register.submit = Submit
6 register.success = RegisterSuccess!
7
2 register.username = UserName
3 register.password = Password
4 register.email = Email
5 register.submit = Submit
6 register.success = RegisterSuccess!
7
6. 由于我们使用动态ActionForm,只需在Struts-config.xml中配置与register.jsp页面中form对应的ActionForm。
7. 创建配置文件Struts-config.xml
1
<?
xml version="1.0" encoding="iso-8859-1"
?>
2 <! DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4 "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
5 <!--
6 Default configuration file for examples application.
7 Each module also has its own struts-config under: /WEB-INF/$MODULE/
8 @version $Revision: 1.9 $ $Date: 2006-02-13 18:39:49 -0700 (Mon, 13 Feb 2006) $
9 -->
10 < struts-config >
11 < form-beans >
12 <! —配置register.jsp中form对应的ActionFrom-- >
13 < form-bean name ="registerForm" type ="org.apache.struts.action.DynaActionForm" >
14 < form-property name ="register" type ="test.register.entity.Register" />
15 </ form-bean >
16 </ form-beans >
17 <! —配置对Struts Action请求的代理-- >
18 < controller processorClass ="org.springframework.web.struts.DelegatingRequestProcessor" />
19 <! —配置register.jsp中form对应的Action-- >
20 < action-mappings >
21 < action
22 name ="registerForm"
23 path ="/register"
24 scope ="request"
25 type ="test.register.action.RegisterAction"
26 validate ="false"
27 input ="register.jsp" >
28 < forward name ="success" path ="/registerSuccess.jsp" />
29 </ action >
30 </ action-mappings >
31
32 < message-resources parameter ="test.register.registermessages" />
33 <! —配置插件,让action能访问sping容器-- >
34 < plug-in
35 className ="org.springframework.web.struts.ContextLoaderPlugIn" >
36 < set-property property ="contextConfigLocation"
37 value ="/WEB-INF/action-servlets.xml" />
38 </ plug-in >
39
40 </ struts-config >
41
2 <! DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4 "http://struts.apache.org/dtds/struts-config_1_3.dtd" >
5 <!--
6 Default configuration file for examples application.
7 Each module also has its own struts-config under: /WEB-INF/$MODULE/
8 @version $Revision: 1.9 $ $Date: 2006-02-13 18:39:49 -0700 (Mon, 13 Feb 2006) $
9 -->
10 < struts-config >
11 < form-beans >
12 <! —配置register.jsp中form对应的ActionFrom-- >
13 < form-bean name ="registerForm" type ="org.apache.struts.action.DynaActionForm" >
14 < form-property name ="register" type ="test.register.entity.Register" />
15 </ form-bean >
16 </ form-beans >
17 <! —配置对Struts Action请求的代理-- >
18 < controller processorClass ="org.springframework.web.struts.DelegatingRequestProcessor" />
19 <! —配置register.jsp中form对应的Action-- >
20 < action-mappings >
21 < action
22 name ="registerForm"
23 path ="/register"
24 scope ="request"
25 type ="test.register.action.RegisterAction"
26 validate ="false"
27 input ="register.jsp" >
28 < forward name ="success" path ="/registerSuccess.jsp" />
29 </ action >
30 </ action-mappings >
31
32 < message-resources parameter ="test.register.registermessages" />
33 <! —配置插件,让action能访问sping容器-- >
34 < plug-in
35 className ="org.springframework.web.struts.ContextLoaderPlugIn" >
36 < set-property property ="contextConfigLocation"
37 value ="/WEB-INF/action-servlets.xml" />
38 </ plug-in >
39
40 </ struts-config >
41
8. 创建实体类Register.java
1
package
test.register.entity;
2
3 import java.io.Serializable;
4
5 publicclass Register implements Serializable {
6 privatestaticfinallongserialVersionUID = 1521645755684977819L;
7 private Long id;
8 private String username;
9 private String password;
10 private String email;
11
12 public Register(){
13
14 }
15
16 public String getEmail() {
17 returnemail;
18 }
19
20 publicvoid setEmail(String email) {
21 this.email = email;
22 }
23
24 public Long getId() {
25 returnid;
26 }
27
28 publicvoid setId(Long id) {
29 this.id = id;
30 }
31
32 public String getPassword() {
33 returnpassword;
34 }
35
36 publicvoid setPassword(String password) {
37 this.password = password;
38 }
39
40 public String getUsername() {
41 returnusername;
42 }
43
44 publicvoid setUsername(String username) {
45 this.username = username;
46 }
47
48
49
50}
51
2
3 import java.io.Serializable;
4
5 publicclass Register implements Serializable {
6 privatestaticfinallongserialVersionUID = 1521645755684977819L;
7 private Long id;
8 private String username;
9 private String password;
10 private String email;
11
12 public Register(){
13
14 }
15
16 public String getEmail() {
17 returnemail;
18 }
19
20 publicvoid setEmail(String email) {
21 this.email = email;
22 }
23
24 public Long getId() {
25 returnid;
26 }
27
28 publicvoid setId(Long id) {
29 this.id = id;
30 }
31
32 public String getPassword() {
33 returnpassword;
34 }
35
36 publicvoid setPassword(String password) {
37 this.password = password;
38 }
39
40 public String getUsername() {
41 returnusername;
42 }
43
44 publicvoid setUsername(String username) {
45 this.username = username;
46 }
47
48
49
50}
51
9.创建Hibernate映射文件,Register.hbm.xml
1
<?
xml version="1.0"
?>
2 <! DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
5 < hibernate-mapping
6 package ="test.register.entity" >
7
8 < class name ="Register" table ="REGISTER" >
9
10 < id name ="id" >
11 < generator class ="native" />
12 </ id >
13
14 < property name ="username"
15 not-null ="true"
16 length ="15"
17 column ="USERNAME" />
18
19 < property name ="password"
20 not-null ="true"
21 length ="15"
22 column ="PASSWORD" />
23
24 < property name ="email"
25 not-null ="true"
26 length ="15"
27 column ="EMAIL" />
28
29 </ class >
30
31 </ hibernate-mapping >
32
2 <! DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
5 < hibernate-mapping
6 package ="test.register.entity" >
7
8 < class name ="Register" table ="REGISTER" >
9
10 < id name ="id" >
11 < generator class ="native" />
12 </ id >
13
14 < property name ="username"
15 not-null ="true"
16 length ="15"
17 column ="USERNAME" />
18
19 < property name ="password"
20 not-null ="true"
21 length ="15"
22 column ="PASSWORD" />
23
24 < property name ="email"
25 not-null ="true"
26 length ="15"
27 column ="EMAIL" />
28
29 </ class >
30
31 </ hibernate-mapping >
32
9. 创建Spring配置文件applicationContext.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
3
4 < beans >
5
6 < bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
7 < property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
8 < property name ="url" value ="jdbc:mysql://localhost/register" />
9 < property name ="username" value ="root" />
10 < property name ="password" value ="root" />
11 </ bean >
12
13 <!-- Hibernate SessionFactory -->
14 < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
15 < property name ="dataSource" ref ="dataSource" />
16 < property name ="mappingDirectoryLocations" >
17 < value > classpath:test\register\entity\ </ value >
18 </ property >
19 < property name ="hibernateProperties" >
20 < props >
21 < prop key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
22 < prop key ="hibernate.show_sql" > true </ prop >
23 < prop key ="hibernate.generate_statistics" > true </ prop >
24 </ props >
25 </ property >
26 </ bean >
27
28 < bean id ="registerDAO" class ="test.register.dao.RegisterDAO" >
29 < property name ="sessionFactory" >
30 < ref local ="sessionFactory" />
31 </ property >
32 </ bean >
33
34 < bean id ="registerService" class ="test.register.service.RegisterService" >
35 < property name ="registerDAO" >
36 < ref local ="registerDAO" />
37 </ property >
38 </ bean >
39
40 </ beans >
41
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
3
4 < beans >
5
6 < bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" >
7 < property name ="driverClassName" value ="com.mysql.jdbc.Driver" />
8 < property name ="url" value ="jdbc:mysql://localhost/register" />
9 < property name ="username" value ="root" />
10 < property name ="password" value ="root" />
11 </ bean >
12
13 <!-- Hibernate SessionFactory -->
14 < bean id ="sessionFactory" class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
15 < property name ="dataSource" ref ="dataSource" />
16 < property name ="mappingDirectoryLocations" >
17 < value > classpath:test\register\entity\ </ value >
18 </ property >
19 < property name ="hibernateProperties" >
20 < props >
21 < prop key ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ prop >
22 < prop key ="hibernate.show_sql" > true </ prop >
23 < prop key ="hibernate.generate_statistics" > true </ prop >
24 </ props >
25 </ property >
26 </ bean >
27
28 < bean id ="registerDAO" class ="test.register.dao.RegisterDAO" >
29 < property name ="sessionFactory" >
30 < ref local ="sessionFactory" />
31 </ property >
32 </ bean >
33
34 < bean id ="registerService" class ="test.register.service.RegisterService" >
35 < property name ="registerDAO" >
36 < ref local ="registerDAO" />
37 </ property >
38 </ bean >
39
40 </ beans >
41
10. 创建数据访问对象RegisterDAO.java
1
package
test.register.dao;
2
3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4
5 import test.register.entity.Register;
6
7 publicclass RegisterDAO extends HibernateDaoSupport {
8
9 public Register saveRegister(Register r){
10 try{
11 this.getHibernateTemplate().saveOrUpdate(r);
12 return r;
13 }catch (Exception e) {
14 e.printStackTrace();
15 returnnull;
16 }
17 }
18}
19
2
3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4
5 import test.register.entity.Register;
6
7 publicclass RegisterDAO extends HibernateDaoSupport {
8
9 public Register saveRegister(Register r){
10 try{
11 this.getHibernateTemplate().saveOrUpdate(r);
12 return r;
13 }catch (Exception e) {
14 e.printStackTrace();
15 returnnull;
16 }
17 }
18}
19
12.创建服务对象RegisterService.java
1
package
test.register.service;
2
3 import test.register.dao.RegisterDAO;
4 import test.register.entity.Register;
5
6 publicclass RegisterService {
7 private RegisterDAO registerDAO;
8
9 publicRegisterDAO getRegisterDAO() {
10 returnregisterDAO;
11 }
12
13 publicvoid setRegisterDAO(RegisterDAO registerDAO) {
14 this.registerDAO = registerDAO;
15 }
16
17 public Register saveRegister(Register r){
18 this.getRegisterDAO().saveRegister(r);
19 return r;
20 }
21}
22
2
3 import test.register.dao.RegisterDAO;
4 import test.register.entity.Register;
5
6 publicclass RegisterService {
7 private RegisterDAO registerDAO;
8
9 publicRegisterDAO getRegisterDAO() {
10 returnregisterDAO;
11 }
12
13 publicvoid setRegisterDAO(RegisterDAO registerDAO) {
14 this.registerDAO = registerDAO;
15 }
16
17 public Register saveRegister(Register r){
18 this.getRegisterDAO().saveRegister(r);
19 return r;
20 }
21}
22
11. 创建web.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE web-app PUBLIC
3 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
4 "http://java.sun.com/dtd/web-app_2_3.dtd" >
5 < web-app >
6 < context-param >
7 < param-name > contextConfigLocation </ param-name >
8 < param-value > /WEB-INF/applicationContext.xml </ param-value >
9 </ context-param >
10 < listener >
11 < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
12 </ listener >
13 < servlet >
14 < servlet-name > action </ servlet-name >
15 < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
16 < init-param >
17 < param-name > config </ param-name >
18 < param-value > /WEB-INF/struts-config.xml </ param-value >
19 </ init-param >
20 < init-param >
21 < param-name > debug </ param-name >
22 < param-value > 3 </ param-value >
23 </ init-param >
24 < init-param >
25 < param-name > detail </ param-name >
26 < param-value > 3 </ param-value >
27 </ init-param >
28 < load-on-startup > 0 </ load-on-startup >
29 </ servlet >
30
31 < servlet-mapping >
32 < servlet-name > action </ servlet-name >
33 < url-pattern > *.do </ url-pattern >
34 </ servlet-mapping >
35
36 < welcome-file-list >
37 < welcome-file > register.jsp </ welcome-file >
38 </ welcome-file-list >
39
40 </ web-app >
41
2 <! DOCTYPE web-app PUBLIC
3 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
4 "http://java.sun.com/dtd/web-app_2_3.dtd" >
5 < web-app >
6 < context-param >
7 < param-name > contextConfigLocation </ param-name >
8 < param-value > /WEB-INF/applicationContext.xml </ param-value >
9 </ context-param >
10 < listener >
11 < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
12 </ listener >
13 < servlet >
14 < servlet-name > action </ servlet-name >
15 < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
16 < init-param >
17 < param-name > config </ param-name >
18 < param-value > /WEB-INF/struts-config.xml </ param-value >
19 </ init-param >
20 < init-param >
21 < param-name > debug </ param-name >
22 < param-value > 3 </ param-value >
23 </ init-param >
24 < init-param >
25 < param-name > detail </ param-name >
26 < param-value > 3 </ param-value >
27 </ init-param >
28 < load-on-startup > 0 </ load-on-startup >
29 </ servlet >
30
31 < servlet-mapping >
32 < servlet-name > action </ servlet-name >
33 < url-pattern > *.do </ url-pattern >
34 </ servlet-mapping >
35
36 < welcome-file-list >
37 < welcome-file > register.jsp </ welcome-file >
38 </ welcome-file-list >
39
40 </ web-app >
41
14.创建RegisterAction.java
import
javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import test.register.entity.Register;
import test.register.service.RegisterService;
publicclass RegisterAction extends Action {
private RegisterService registerService;
public RegisterService getRegisterService() {
returnregisterService;
}
publicvoid setRegisterService(RegisterService registerService) {
this.registerService = registerService;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Register r = (Register)((DynaActionForm)form).get("register");
getRegisterService().saveRegister(r);
return mapping.findForward("success");
}
}
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import test.register.entity.Register;
import test.register.service.RegisterService;
publicclass RegisterAction extends Action {
private RegisterService registerService;
public RegisterService getRegisterService() {
returnregisterService;
}
publicvoid setRegisterService(RegisterService registerService) {
this.registerService = registerService;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Register r = (Register)((DynaActionForm)form).get("register");
getRegisterService().saveRegister(r);
return mapping.findForward("success");
}
}
在RegisterAction.java中,为了获取spring容器中的RegisterService对象,我们要对struts-config.xml作下修改,具体修改如下:
在struts-config.xml增加controller元素
<
controller
processorClass
="org.springframework.web.struts.DelegatingRequestProcessor"
/>
目的告诉Struts自动将对Action的请求委托给Spring上下文中的Struts Action来处理。
并且为了让Struts能访问Spring容器,还必须在struts-config.xml注册一个能访问Spring上下文的插件:
<
plug-in
className
="org.springframework.web.struts.ContextLoaderPlugIn"
>
< set-property property ="contextConfigLocation"
value ="/WEB-INF/action-servlets.xml" />
</ plug-in >
< set-property property ="contextConfigLocation"
value ="/WEB-INF/action-servlets.xml" />
</ plug-in >
action-servlets.xml文件的具体内容如下:
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
3
4 < beans >
5 < bean name ="/register" class ="test.register.action.RegisterAction" >
6 < property name ="registerService" ref ="registerService" />
7 </ bean >
8 </ beans >
9
2 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
3
4 < beans >
5 < bean name ="/register" class ="test.register.action.RegisterAction" >
6 < property name ="registerService" ref ="registerService" />
7 </ bean >
8 </ beans >
9
注意:这里bean是name属性,而不是id。
至此,一个完整的SSH(Struts,Spring,Hibernate)框架搭建好了。