实现一个简单实现也没,往数据库中注册一个用户,使用声明式事务管理对service层进行事务管理。
整个项目分三个层面,dao层处理数据库,action层处理也面请求和跳转,service层处理业务逻辑。
使用的struts2.3.4+spring3.2+hibernate3,数据库为mysql
数据库名为spring,数据库表为user,表结构为id int,name varchar,password varchar
整个目录结构
src
com.cvicse.action
RegistAction.java
com.cvicse.dao
UserDAO.java
com.cvicse.db
User.java
com.cvicse.service
UserService.java
struts.xml
web
WEB-INF
lib
applicationContext.xml
web.xml
index.jsp
success.jsp
配置文件如下:
web.xml的配置如下:配置spring的监听器和struts2的拦截器
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<description>Spring core configuration</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml的配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="user" extends="struts-default">
<action name="regist" class="com.cvicse.action.RegistAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
applicationContext.xml的配置文件如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cvicse" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="cvicse"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan">
<list>
<value>com.cvicse.db</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 自定义的UserDao -->
<bean id="userDao" class="com.cvicse.dao.UserDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<!-- 自定义UserService,注入UserDao -->
<bean id="userService" class="com.cvicse.service.UserService">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 自定义LoginAction,注入userService -->
<bean id="regist" class="com.cvicse.action.RegistAction">
<property name="userService" ref="userService"></property>
</bean>
<!-- 事务管理 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义管理事务的方法格式 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 定义切面 -->
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.cvicse.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
</beans>
登录界面如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<s:form action="regist" method="post">
<s:textfield name="name" label="用户名"></s:textfield><br/>
<s:password name="password" label="密 码"></s:password><br/>
<s:submit label="登录"></s:submit>
</s:form>
</body>
</html>
后台页面:
RegistAction.java如下:
package com.cvicse.action;
import com.cvicse.db.User;
import com.cvicse.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = 5009307619469258933L;
private String name;
private String password;
private UserService userService;
@Override
public String execute() throws Exception {
User user = new User();
user.setName(name);
user.setPassword(password);
userService.addUser(user);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}