整理赵龙ssh2
一、新建web项目
1.创建web项目ssh2
2.将项目添加到tomcat中
<context path=”/ssh2” docBase=“H:\workspace201306\ssh2\WebRoot”
reloadable=”true”/>
3.测试 http://localhost:8080/ssh2/
二、给项目添加jar包
1.手动添加struts所需jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.0.4.jar(在hibernate中存在可以不添加)
freemarker-2.3.15.jar
ongl-2.7.3.jar
struts2-core-2.1.8.jar
struts2-spring-plugin-2.1.8.jar(主要用于sptruts与spring的整合)
xwork-core-2.1.6.jar
2.通过myEclipse的add hibernate Capabilities添加hibernate3.2的jar包
注意:
1)勾选copy checked Library Jars to project and add to build-path
2)commons-logging-1.0.4.jar在hibernate及struts中同时存在保留一个
3.通过myEclipse的add spring Capabilities添加spring2.5的jar包
Spring2.5 AOP Libraries(选中第三项时自动勾选)
Spring2.5 Core Libraries
Spring2.5 Persistence Core Libraries
Spring2.5 Web Libraries
注意:
1)Copy checked Library contents to project folder(TLDS always copied)
2)applicationContext.xml必须指定放到WebRoot/WEB-INF目录下
3)不要勾选Create Spring SessionFactory that references
4)asm.jar是由hibernate引入的jar,asm-2.2.3.jar是spring引入的jar,它们是同
一个jar,必须进入\WebRoot\WEB-INF\lib文件夹中删除一个就行,我删除后面的一个.
4.添加连接池
Commons-dbcp-1.3.jar
Commons-pool-1.5.5.jar
5.添加数据库
ojdbc14.jar
6.添加日志支持
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
并将log4j.properties拷贝到src目录下
三、修改及添加配制文件
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 下面是struts过滤器 -->
<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>
<!-- 下面是spring的监天器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.添加applicationContext.xml
3.添加struts.xml
四、创建项目包
1.com.test.action
2.com.test.bean
3.com.test.dao
4.com.test.dao
5.com.test.dao.impl
6.com.test.service
7.com.test. service.impl
8. com.test.util
五、代码编写
1.编写bean和相应的映射文件
User.java
package com.test.bean;
public class User {
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;}
}
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.bean.User" table="users">
<id name="id" column="id" type="int">
<generator class="native">
</generator>
</id>
<property name="firstname" column="firstname"
type="string"></property>
<property name="lastname" column="lastname"
type="string"></property>
<property name="age" column="age"
type="int"></property>
</class></hibernate-mapping>
2.编写持久层dao及daoimpl
UserDAO.java
package com.test.dao;
import java.util.List;
import com.test.bean.User;
public interface UserDAO {
public void saveUser(User user);
public List<User> findAllUsers();
public void removeUser(User user);
public void updateUser(User user);
public User findUserById(Integer id);}
UserDAOImpl.java
package com.test.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.test.bean.User;
import com.test.dao.UserDAO;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
public void saveUser(User user) {
this.getHibernateTemplate().save(user); }
@SuppressWarnings("unchecked")
public List<User> findAllUsers() {
String hql = "from User user order by user.id desc";
return (List<User>) this.getHibernateTemplate().find(hql); }
public void removeUser(User user) {
this.getHibernateTemplate().delete(user); }
public void updateUser(User user) {
this.getHibernateTemplate().update(user); }
public User findUserById(Integer id) {
User user = (User) this.getHibernateTemplate().get(User.class, id);
return user; }
}
3.编写业务层service及serviceimpl
UserService.java
package com.test.service;
import java.util.List;
import com.test.bean.User;
public interface UserService {
public void save(User user);
public List<User> findAll();
public void delete(User user);
public User findById(Integer id);
public void update(User user);
}
UserServiceImpl.java
package com.test.service.impl;
import java.util.List;
import com.test.bean.User;
import com.test.dao.UserDAO;
import com.test.service.UserService;
public class UserServiceImpl implements UserService {
private UserDAO userDao;
public void save(User user) {
userDao.saveUser(user);// 完成业务逻辑 }
public List<User> findAll() {
return this.userDao.findAllUsers(); }
public UserDAO getUserDao() {
return userDao; }
public void setUserDao(UserDAO userDao) {
this.userDao = userDao; }
public void delete(User user) {
this.userDao.removeUser(user); }
public User findById(Integer id) {
return this.userDao.findUserById(id); }
public void update(User user) {
this.userDao.updateUser(user); }
}
4.编写action
SaveUserAction.java
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;
public class SaveUserAction 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; }
@Override
public String execute() throws Exception {
//调用service相关的方法,完成实际的业务逻辑处理
userService.save(user);
return this.SUCCESS;
}
}
ListUserAction.java
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 userService;
public UserService getUserService() {
return userService; }
public void setUserService(UserService userService) {
this.userService = userService; }
@Override
public String execute() throws Exception {
Map request= (Map)ActionContext.getContext().get("request");
request.put("list", this.userService.findAll());
return this.SUCCESS; }
}
RemoveUserAction.java
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;
public class RemoveUserAction 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; }
@Override
public String execute() throws Exception {
userService.delete(user);
return this.SUCCESS;}
}
UpdatePUserAction.java
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;
public class UpdatePUserAction 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; }
@Override
public String execute() throws Exception {
user = userService.findById(user.getId());
return SUCCESS; }
}
UpdateUserAction.java
package com.test.action.user;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.User;
import com.test.service.UserService;
public class UpdateUserAction 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; }
@Override
public String execute() throws Exception {
userService.update(user);
return this.SUCCESS; }
}
5.配制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"
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</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@10.75.208.11:1521:ora9i</value>
</property>
<property name="username">
<value>zyrj</value>
</property>
<property name="password">
<value>dda01zbh12</value>
</property>
<property name="maxActive">
<value>255</value>
</property>
<property name="maxIdle">
<value>2</value>
</property>
<property name="maxWait">
<value>120000</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9iDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="com.test.dao.impl.UserDAOImpl"
scope="singleton">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userServiceTarget"
class="com.test.service.impl.UserServiceImpl" scope="singleton">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="userServiceTarget"></property>
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="saveUserAction"
class="com.test.action.user.SaveUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="listUserAction"
class="com.test.action.user.ListUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="removeUserAction"
class="com.test.action.user.RemoveUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="updatePUserAction"
class="com.test.action.user.UpdatePUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
<bean id="updateUserAction"
class="com.test.action.user.UpdateUserAction" scope="prototype">
<property name="userService" ref="userService"></property>
</bean>
</beans>
6.配制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>
<package name="ssh2" extends="struts-default">
<action name="saveUser" 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="deleteUser" class="removeUserAction">
<result name="success" type="redirect">
listUser.action
</result>
</action>
<action name="updatePUser" class="updatePUserAction">
<result name="success">/update.jsp</result>
</action>
<action name="updateUser" class="updateUserAction">
<result name="success" type="redirect">listUser.action</result>
</action>
</package>
</struts>
7.编写jsp页面
Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color="red">Operation List</font></h1>
<s:a href="save.jsp"> Save User</s:a><br><br>
<s:a href="listUser.action"> List Users</s:a>
</body>
</html>
Save.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'save.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color="red">Save User</font></h1>
<s:form action="saveUser">
<s:textfield name="user.firstname" label="firstname"/>
<s:textfield name="user.lastname" label="lastname"/>
<s:textfield name="user.age" label="age"/>
<s:submit value="submit"/>
</s:form>
</body>
</html>
List.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function del(){
if(confirm("Are You sure?")){
return true;
}
return false;
}
</script>
</head>
<body>
<h1><font color="red"><center> User List</center> </font> </h1>
<table border="1" width="80%" align="center">
<tr>
<td>序号</td>
<td>姓</td>
<td>名</td>
<td>年龄</td>
<td>删除</td>
<td>更新</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="deleteUser.action?user.id=%{#us.id}" onclick="return del();">delete</s:a> </td>
<td><s:a href="updatePUser.action?user.id=%{#us.id}">更新</s:a></td>
<tr>
</s:iterator>
</table>
</body>
</html>
Update.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'update.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1><font color="red"><center>Update User</center></font></h1>
<s:form action="updateUser">
<table>
<tr><td>
<s:hidden name="user.id" value="%{user.id}"></s:hidden>
</td></tr>
<tr><td>
<s:textfield name="user.firstname" value="%{user.firstname}" label="first name"></s:textfield>
</td></tr>
<tr><td>
<s:textfield name="user.lastname" value="%{user.lastname}" label="last name"></s:textfield>
</td></tr>
<tr><td>
<s:textfield name="user.age" value="%{user.age}" label="age"></s:textfield>
</td></tr>
<tr><td>
<s:submit value="submit"></s:submit>
</td></tr>
</table>
</s:form>
</body>
</html>
六、测试