网上搜索到的很多配置都是实现了HelloWorld,这里我简单实现一个用户注册的配置流程,我选用的开发环境是MyEclipse,Web容器是Tomcat6,数据库为mySql。
我们在MyEclipse里面新建一个web工程Test
一.配置Struts
首先下载Struts2的包,可以在http://struts.apache.org/下载到最新版本,我使用的是2.06,下载后的文件解压后进入apps/struts2-blank-2.0.6/WEB-INF/lib目录下,把所有的lib文件复制Test工程的WEB-INF目录下,加入后的包自动会显示在系统包的下方,目录如下图:
然后我们修改web.xml文件如下:
xml version="1.0" encoding="UTF-8"
?>
<
web-app
version
="2.4"
xmlns
="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
<
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
>
web-app
>
告诉web容器所有以/*开头的url都交给struts处理
接着新建一个test.beans包,在此包下建立一个User.java文件,此为标准javaBean,有三个成员userName和password,userId,和相应的setter和getter方法
package
test.beans;
public
class
User
...
{
private Integer userId;
private String userName;
private String password;
public String getPassword() ...{
return password;
}
public void setPassword(String password) ...{
this.password = password;
}
public String getUserName() ...{
return userName;
}
public void setUserName(String userName) ...{
this.userName = userName;
}
public Integer getUserId() ...{
return userId;
}
public void setUserId(Integer userId) ...{
this.userId = userId;
}
}
接下来新建一个包,我的名称为test.action,来存放工程action文件,在此包下建立一个UserRegisterAction的类,代码如下:
package
test.action;
import
test.beans.User;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UserRegisterAction
extends
ActionSupport
...
{
private User user;
public String execute()
...{
return SUCCESS;
}
public User getUser() ...{
return user;
}
public void setUser(User user) ...{
this.user = user;
}
}
此处并没有添加业务逻辑,仅仅加入了一个user对象声明,和相应的getter和setter方法,主要是为了说明在jsp页面提交以后user对象会被自动创建和注入属性,业务逻辑将在配置Spring的时候添加.
在webRoot下建立2个jsp文件分别为userRegister.jsp和success.jsp
userRegister.jsp
<%
@ page language
=
"
java
"
contentType
=
"
text/html; charset=UTF-8
"
pageEncoding
=
"
UTF-8
"
%>
<%
@taglib prefix
=
"
s
"
uri
=
"
/struts-tags
"
%>
<
html
>
<
head
>
<
title
>
注册页面
title
>
head
>
<
body
>
<
s:form action
=
"
userRegister
"
>
<
table align
=
"
center
"
>
<
caption
><
h3
>
用户注册
h3
>
caption
>
<
tr
>
<
td
><
s:textfield label
=
"
用户名
"
name
=
"
user.userName
"
/>
td
>
tr
>
<
tr
>
<
td
><
s:password label
=
"
密 码
"
name
=
"
user.password
"
/>
td
>
tr
>
<
tr align
=
"
center
"
>
<
td
><
input type
=
"
submit
"
value
=
"
注册
"
/>
td
><
td
><
input type
=
"
reset
"
value
=
"
重填
"
/>
td
>
tr
>
table
>
s:form
>
body
>
html
>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
操作成功
操作成功
最后我们在struts.xml里面配置这个Action,struts.xml位于src目录下
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
>
<
package
name
="user"
extends
="struts-default"
>
<
action
name
="userRegister"
class
="test.action.UserRegisterAction"
>
<
result
name
="success"
>
/success.jsp
result
>
action
>
package
>
struts
>
struts2和webwork2的配置基本类似,到此struts配置完成了,在配置Spring的时候我们将加入业务逻辑
一.配置Spring
下载Struts2的包,可在http://www.springframework.org/下载到最新版本,我使用的是2.04,解压缩后将dist目录下的三个jar文件加入到项目lib中,还需要把struts2中的struts2-spring-plugin-2.0.6.jar包加入到项目lib中
在src目录下新建test.Iservice和test.service包,分别为服务的接口和实现类,在test.Iservice包里面新建IUserService.java文件
package
test.Iservice;
import
test.beans.User;
public
interface
IUserService
...
{
public void saveUser(User user);
}
只写了一个保存接口,主要是演示作用,在实际项目中应该最少有findUser ,delUser等接口
在test.service包里面添加UserService.java文件来实现上面的接口,此处体现了反转控制 (IOC),用接口来控制实现类,提高了移植性
package
test.service;
import
test.Iservice.IUserService;
import
test.beans.User;
public
class
UserService
implements
IUserService
...
{
public void saveUser(User user) ...{
}
}
此处我并没有实现任何业务逻辑,在配置Hibernate的时候会添加
在配置struts的时候在userRegisterAction中没有添加任何逻辑,现在我们写了UserService类了,我们修改userRegisterAction代码如下
package
test.action;
import
test.Iservice.IUserService;
import
test.beans.User;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UserRegisterAction
extends
ActionSupport
...
{
private User user;
private IUserService userService;
public String execute()
...{
return SUCCESS;
}
public User getUser() ...{
return user;
}
public void setUser(User user) ...{
this.user = user;
}
public IUserService getUserService() ...{
return userService;
}
public void setUserService(IUserService userService) ...{
this.userService = userService;
}
}
可以看到我们添加了
userService成员和相应的setter和getter方法,接下来我们在web.xml里面添加Spring监听器
<
listener
>
<
listener
-
class
>
org.springframework.web.context.ContextLoaderListener
listener
-
class
>
listener
>
之后在src目录下添加文件struts.properties文件
struts.tag.altSyntax
=
true
struts.devMode
=
true
struts.objectFactory
=
spring
其中struts.objectFactory=spring将struts的Action交给spring管理,我们只需要在Action里面设置相应的setter方法就可以了,就像上面的getUserService
接下来就可以开始配置Spring的Bean了,在WEB-INF目录下建立一个applicationContext.xml文件,这是spring的主要配置文件
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
="userService"
class
="test.service.UserService"
/>
beans
>
基本的Spring就配置完了,接下来配置Hibernate
一.配置Hibernate
和前面一样,首先要下载Hibernate的包http://www.hibernate.org/ 有最新下载,我使用的是hibernate-3.1.2,将解压缩后的hibernate3.jar加入到项目lib中
创建数据库
使用mysql Query Brower在test数据库中添加user表Sql语句如下:
CREATE TABLE `test`.`user` (
`userId` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`userName` VARCHAR(
45
) NOT NULL,
`password` VARCHAR(
45
) NOT NULL,
PRIMARY KEY (`userId`)
)
ENGINE
=
InnoDB;
编写映射文件
在配置struts的时候我们创建了一个User的类,我们要为它写一个映射文件User.hbm.xml
xml version="1.0"
?>
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="test.beans.User"
table
="user"
>
<
id
name
="userId"
type
="java.lang.Integer"
column
="userId"
>
<
generator
class
="native"
/>
id
>
<
property
name
="userName"
type
="java.lang.String"
column
="userName"
not-null
="true"
length
="100"
/>
<
property
name
="password"
type
="java.lang.String"
column
="password"
not-null
="true"
length
="100"
/>
class
>
hibernate-mapping
>
我们创建test.IDAO和test.DAO包,在IDAO包中编写一个IUserDAO.java
package
test.IDAO;
import
test.beans.User;
public
interface
IUserDAO
...
{
public void save(User user);
}
在DAO包中编写UserDAO.java实现上面的接口,同样体现了IOC
package
test.DAO;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import
test.IDAO.IUserDAO;
import
test.beans.User;
public
class
UserDAO
extends
HibernateDaoSupport
implements
IUserDAO
...
{
public void save(User user) ...{
getHibernateTemplate().save(user);
}
}
UserDAO继承了HibernateDaoSupport类,这是Spring为Hibernate设计的一个类,封装了很多操作,可以直接调用getHibernateTemplate()获得一个hibernatetemplate的实例.
在配置spring的时候我们编写了UserService类,但是没有任何业务逻辑,这里我们为UserService添加业务逻辑,修改后的UserService如下
package
test.service;
import
test.IDAO.IUserDAO;
import
test.Iservice.IUserService;
import
test.beans.User;
public
class
UserService
implements
IUserService
...
{
private IUserDAO userDAO;
public void saveUser(User user) ...{
userDAO.save(user);
}
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
}
添加了userDAO成员,我们需要修改applicationContext.xml为UserService注入userDAO属性,并且配置数据库的一些属性,修改后的applicationContext.xml如下
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
="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource"
destroy-method
="close"
>
<
property
name
="driverClassName"
value
="com.mysql.jdbc.Driver"
/>
<
property
name
="url"
value
="jdbc:mysql://localhost:3306/test"
/>
<
property
name
="username"
value
="root"
/>
<
property
name
="password"
value
="68467823"
/>
bean
>
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
local
="dataSource"
/>
property
>
<
property
name
="mappingResources"
>
<
list
>
<
value
>
test/beans/User.hbm.xml
value
>
list
>
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.MySQLDialect
prop
>
<
prop
key
="hibernate.show_sql"
>
true
prop
>
<
prop
key
="hibernate.cglib.use_reflection_optimizer"
>
true
prop
>
<
prop
key
="hibernate.query.substitutions"
>
true 1, false 0
prop
>
props
>
property
>
bean
>
<
bean
id
="userService"
class
="test.service.UserService"
>
<
property
name
="userDAO"
>
<
ref
local
="userDAO"
/>
property
>
bean
>
<
bean
id
="userDAO"
class
="test.DAO.UserDAO"
>
<
property
name
="sessionFactory"
ref
="sessionFactory"
/>
bean
>
beans
>
注意映射文件的位置还有数据库的用户名和密码要改为自己的
最后我们在UserRegisterAction的execute()方法中加入userService.saveUser(user);修改后如下
package
test.action;
import
test.Iservice.IUserService;
import
test.beans.User;
import
com.opensymphony.xwork2.ActionSupport;
public
class
UserRegisterAction
extends
ActionSupport
...
{
private User user;
private IUserService userService;
public String execute()
...{
userService.saveUser(user);
return SUCCESS;
}
public User getUser() ...{
return user;
}
public void setUser(User user) ...{
this.user = user;
}
public IUserService getUserService() ...{
return userService;
}
public void setUserService(IUserService userService) ...{
this.userService = userService;
}
}
现在配置都完成了,部署到服务器,用http://localhost:8080/test/userRegister.jsp 可以访问,数据成功添加入数据库
提交表单后在eclipse的控制台会显示HQL语句
正规的软件工程设计后才编码,不应该是上面的流程,我这么写是为了一样一样的配置起来看起来简单一点,在真正的工程中不会采取这种方式,还有没有进行表单验证,Struts有标签库支持认证,有兴趣的可以自己看下,很多概念和为什么要这么写都没有写得很清楚,希望大家体谅,有问题可以联系我[email protected] ,希望大家多提宝贵意见也希望能对初学者起到一定的帮助