Package Explorer
Users
package com.test.bean;
public class Users {
private int id;
private String firstname;
private String lastname;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Users.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.test.bean.Users" table="USERS" schema="SYSTEM">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="9" scale="0" />
<generator class="sequence">
<param name="sequence">SQ_USERS</param>
</generator>
</id>
<property name="firstname" type="java.lang.String">
<column name="firstname" length="20" />
</property>
<property name="lastname" type="java.lang.String">
<column name="LASTNAME" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="AGE" precision="9" scale="0" />
</property>
</class>
</hibernate-mapping>
UserDAO
package com.test.dao;
import java.util.List;
import com.test.bean.Users;
public interface UserDAO {
public void saveUsers(Users users);
public List<Users> findAllUsers();
public void removeUser(Users users);
public void updateUsers(Users usre);
public Users findUsersById(int id);
}
UserDAOImpl
package com.test.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.bean.Users;
import com.test.dao.UserDAO;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
public void saveUsers(Users users) {
this.getHibernateTemplate().save(users);
}
@SuppressWarnings("unchecked")
public List<Users> findAllUsers() {
String hql="from Users us order by us.id desc";
return this.getHibernateTemplate().find(hql);
}
public void removeUser(Users users) {
this.getHibernateTemplate().delete(users);
}
public void updateUsers(Users usre) {
this.getHibernateTemplate().update(usre);
}
public Users findUsersById(int id) {
Users users=(Users) this.getHibernateTemplate().get(Users.class, id);
return users;
}
}
UserService
package com.test.service;
import java.util.List;
import com.test.bean.Users;
public interface UserService {
public void save(Users users);
public List<Users> findAllUsers();
public void delete(Users users);
public void update(Users users);
public Users findById(int id);
}
UserServiceImpl
package com.test.service.impl;
import java.util.List;
import com.test.bean.Users;
import com.test.dao.UserDAO;
import com.test.service.UserService;
public class UserServiceImpl implements UserService {
private UserDAO userDao;
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
public void save(Users users) {
this.userDao.saveUsers(users); //finish the really business logic.
}
public List<Users> findAllUsers() {
return this.userDao.findAllUsers();
}
public void delete(Users users) {
this.userDao.removeUser(users);
}
public void update(Users users) {
this.userDao.updateUsers(users);
}
public Users findById(int id) {
return this.userDao.findUsersById(id);
}
}
ListUserAction
package com.test.action.user;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UserService;
public class ListUserAction extends ActionSupport {
private UserService service;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
Map request=(Map) ActionContext.getContext().get("request");
request.put("list", this.service.findAllUsers());
return SUCCESS;
}
}
SaveUserAction
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Users;
import com.test.service.UserService;
public class SaveUserAction extends ActionSupport {
private Users users;
private UserService service;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
@Override
public String execute() throws Exception {
this.service.save(users);
return SUCCESS;
}
}
DeleteUserAction
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.recompile;
import com.test.bean.Users;
import com.test.service.UserService;
public class DeleteUserAction extends ActionSupport {
private Users users;
private UserService service;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
this.service.delete(users);
return SUCCESS;
}
}
ShowBeforeUpdateAction
package com.test.action.user;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Users;
import com.test.service.UserService;
public class ShowBeforeUpdateAction extends ActionSupport {
private Users users;
private UserService service;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
this.users=this.service.findById(users.getId());
return SUCCESS;
}
}
UpdateUsersAction
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Users;
import com.test.service.UserService;
public class UpdateUsersAction extends ActionSupport {
private Users users;
private UserService service;
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@Override
public String execute() throws Exception {
this.service.update(users);
return SUCCESS;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="ssh" extends="struts-default">
<!-- 处理防止表单重复提交方式:1用重定向,2.令牌-->
<action name="saveUsers" class="saveUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>
<action name="listUser" class="listUserAction">
<result name="success">/list.jsp</result>
</action>
<action name="deleteUsers" class="removeUserAction">
<result name="success" type="chain">listUser</result>
</action>
<!-- Show the information before update. -->
<action name="showBeforeUpdate" class="showBeforeUpdateAction">
<result name="success">/update.jsp</result>
</action>
<!-- Execute the really update -->
<action name="updateUsers" class="updateUserAction">
<result name="success" type="redirect">listUser</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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="orcl"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/Users.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置dao -->
<bean id="userDao" class="com.test.dao.impl.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 配置service -->
<bean id="userService" class="com.test.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 配置action 若scope不是配置为prototype,错误! -->
<bean id="saveUserAction" class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="listUserAction" class="com.test.action.user.ListUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<bean id="removeUserAction" class="com.test.action.user.DeleteUserAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
<!-- Show the information before update. -->
<bean id="showBeforeUpdateAction" class="com.test.action.user.ShowBeforeUpdateAction">
<property name="service" ref="userService"></property>
</bean>
<!-- Executed the update. -->
<bean id="updateUserAction" class="com.test.action.user.UpdateUsersAction" scope="prototype">
<property name="service" ref="userService"></property>
</bean>
</beans>
Spring Explorer
index.jsp
<%@ 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>index Page</title>
<style type="text/css">
#main{border: 1px black solid; width: 300px; margin-left: 25%; padding-left: 60px; height: 200px;}
</style>
</head>
<body>
<div id="main">
<h1><font color="red">Operation List</font></h1>
<ul>
<li><s:a href="save.jsp">Save Users</s:a></li><br>
<li><s:a href="listUser.action">List Users</s:a></li>
</ul>
</div>
</body>
</html>
save.jsp
<%@ 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>Save Page.</title>
<style type="text/css">
#main{border: 1px black solid; width: 300px; margin-left: 25%; padding-left: 60px;}
</style>
</head>
<body>
<div id="main">
<h1><font color="red">Save Users</font></h1>
<s:form action="saveUsers">
<s:textfield name="users.firstname" label="fist name"></s:textfield>
<s:textfield name="users.lastname" label="last name"></s:textfield>
<s:textfield name="users.age" label="age"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</div>
</body>
</html>
list.jsp
<%@ 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>List information Page.</title>
<style type="text/css">
table {border-collapse: collapse; width: 70%; text-align: center; font-size: 12px;}
.ti{color: red;}
.th{background: #CCCCCC; font-size: 14px;}
#mian{margin-left: 25%;}
</style>
<script type="text/javascript">
function del(){
if(confirm("Are you sure?")){
return true;
}
return false;
}
</script>
</head>
<body>
<div id="mian">
<h1 class="ti">Users List</h1>
<table border="1">
<font class="ta"><s:a href="index.jsp"> back</s:a></font>
<tr class="th">
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Age</td>
<td>Operation</td>
</tr>
<s:iterator value="#request.list" id="us">
<tr>
<td><s:property value="#us.id"/></td>
<td><s:property value="#us.firstname"/></td>
<td><s:property value="#us.lastname"/></td>
<td><s:property value="#us.age"/></td>
<td>
<s:a href="showBeforeUpdate.action?users.id=%{#us.id}">Update</s:a>
<s:a href="deleteUsers.action?users.id=%{#us.id}" onclick="return del()">Delete</s:a>
</td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
update.jsp
<%@ 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>Update Page.</title>
<style type="text/css">
table {border-collapse: collapse; text-align: center; font-size: 12px; }
.ti{color: red;}
#main{border: 1px black solid; width: 300px; margin-left: 25%; padding-left: 60px;}
</style>
</head>
<body>
<div id="main">
<h1 class="ti">Update users</h1>
<s:form action="updateUsers.action">
<table border="0">
<tr>
<%--<td><s:hidden name="users.id" value="%{users.id}"></s:hidden></td>--%>
<td><s:textfield label="ID" name="users.id" value="%{users.id}" readonly="true"></s:textfield></td>
</tr>
<tr>
<td><s:textfield label="firstname" name="users.firstname" value="%{users.firstname}"></s:textfield></td>
</tr>
<tr>
<td><s:textfield label="lastname" name="users.lastname" value="%{users.lastname}"></s:textfield></td>
</tr>
<tr>
<td><s:textfield label="age" name="users.age" value="%{users.age}"></s:textfield></td>
</tr>
<s:submit value="Update Now"></s:submit>
</table>
</s:form>
</div>
</body>
</html>