环境准备
最近试着搭了一个简单的ssh框架,是一个用户注册的功能,在此记录下来流程,备忘的同时,也分享出来。本文使用spring3.2+struts2.2+hibernate3在eclipse上搭建这个框架,所用jar包整合可以在这里下载。点此下载
项目创建
新建一个Dynamic Web Project,一路next,到这里
项目创建成功以后就可以创建包和类文件了,目录如下:
action包存放action类,beans存放pojo类,dao的daoImpl分别存放dao接口和实现类,service和serviceImpl分别存放服务接口和实现类。
然后把在上面地址下载的jar包放入WebContent下的lib文件夹。
接下来就可以创建数据库了。这里用的是mysql。在mysql里执行这段脚本。
CREATE DATABASE ssh;
USE ssh;
CREATE TABLE user(
id INT(11) AUTO_INCREMENT,
username VARCHAR(32),
password VARCHAR(32),
gender INT(1),
PRIMARY KEY(id)
)
搭建过程
- 首先配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SSHDemo</display-name>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
<!-- spring监听器,当项目启动时,加载spring配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- struts2拦截器 -->
<filter>
<filter-name>SSH</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 拦截所有类型的url -->
<filter-mapping>
<filter-name>SSH</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- 创建jsp页面
在WebContent下新建register.jsp、success.jsp和error.jsp
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<s:form action="register">
<s:textfield name="user.username" label="用户名"/>
<s:password name="user.password" label="密码"/>
<s:select list="#{'1':'男','0':'女'}"
name="user.gender" label="性别"/>
<s:submit value="注册"/>
</s:form>
</body>
</html>
success和error的页面分别用一句话显示成功的消息和失败的消息,因此不再列出。
- 创建实体类
根据图片,在对应的包下创建相应的类。
在beans下创建User.java
package beans;
public class User {
private int id;
private String username;
private String password;
private int gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
- 创建User的映射
同样在beans下创建User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="beans.User" table="user">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="username" />
<property name="password" />
<property name="gender" />
</class>
</hibernate-mapping>
- 创建service和dao
UserDao.java
package dao;
import org.hibernate.HibernateException;
public interface UserDao {
void saveUser(Object object) throws HibernateException;
}
UserDaoImpl.java
package daoImpl;
import org.hibernate.HibernateException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import dao.UserDao;
public class UserDaoImpl extends HibernateTemplate implements UserDao {
@Override
public void saveUser(Object object) throws HibernateException {
// TODO Auto-generated method stub
save(object);
}
}
UserService.java
package service;
import beans.User;
public interface UserService {
void saveUser(User user);
}
UserServiceImpl.java
package serviceImpl;
import org.hibernate.HibernateException;
import beans.User;
import dao.UserDao;
import service.UserService;
public class UserServiceImpl implements UserService {
private UserDao dao;
public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
this.dao = dao;
}
@Override
public void saveUser(User user) throws HibernateException{
dao.saveUser(user);
}
}
- 创建action
RegisterAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
import beans.User;
import service.UserService;
public class RegisterAction extends ActionSupport{
private User user;
private UserService userService;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute(){
try {
userService.saveUser(user);
return SUCCESS;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return ERROR;
}
}
}
- 配置struts文件
在WEB-INF下新建classes文件夹,新建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>
<!-- 此处告诉struts创建action的工作交给spring来完成 -->
<constant name="struts.objectFactory" value="spring"></constant>
<package name="reg" extends="struts-default">
<!-- regAction配置在spring中 -->
<action name="register" class="regAction" method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
- 配置spring文件
在WEB-INF下新建applicationContext.xml文件,默认是到WEB-INF下寻找spring的配置文件,也可以在web.xml中更改spring配置文件的路径。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置数据源,-->
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssh"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="ds">
</property>
<property name="mappingResources">
<list>
<value>beans/User.hbm.xml</value>
</list>
</property>
<property name="schemaUpdate">
<value>true</value>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hbm2ddl.auto=update
</value>
</property>
</bean>
<!-- dao层 -->
<bean id="userDao" class="daoImpl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--用户注册逻辑-->
<bean id="userService" class="serviceImpl.UserServiceImpl">
<property name="dao" ref="userDao"/>
</bean>
<!-- 用户注册的Action scope="prototype" 可以保证 当有请求的时候 都创建一个新的Action对象 -->
<bean id="regAction" class="action.RegisterAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>
</beans>
WebContent下的文件结构
至此,搭建完成,运行即可。