由于工作没有用到的原因,已经很久没有用到SSH。趁着最近有时间粗略的整理一下
一、开发前准备
eclipse(已经安装好Maven插件)、tomcat、mysql数据库
二、用Maven Dependencies 的jar包 这是我的pom.xml 文件
由于SSH版本之间的jar包的不兼容问题吧,在这个包的整理的时候花了挺多的时间,这个就是少用需要付出的代价啦。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vincent</groupId>
<artifactId>SSH</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SSH Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.6rc1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>java</id>
<name>java official repository</name>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<build>
<finalName>SSH</finalName>
</build>
</project>
三、从导入的文件的版本中,查找到需要用到xml文件 ,这个也是Maven一个不好使的地方,每个版本的文件的头都不同,有时候因为
xml头不同,搞出莫名其妙的错误来。郁闷
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/ssh</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>135099</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>User.cfg.xml</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="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="com.vincent.dao.UserDAOImpl" scope="singleton">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userServiceTarget" class="com.vincent.service.UserServiceImpl" scope="singleton">
<property name="userDao">
<ref local="userDao"/>
</property>
</bean>
<bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name="target">
<ref local="userServiceTarget"/>
</property>
<property name="transactionManager">
<ref local="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.vincent.action.SaveUserAction">
<property name="userService" ref="userService" />
</bean>
<bean id="deleteUserAction" class="com.vincent.action.DeleteUserAction">
<property name="userService" ref="userService" />
</bean>
<bean id="updateUserAction" class="com.vincent.action.UpdateUserAction">
<property name="userService" ref="userService" />
</bean>
<bean id="searchUserAction" class="com.vincent.action.SearchUserAction">
<property name="userService" ref="userService" />
</bean>
<bean id="listUserAction" class="com.vincent.action.ListUserAction">
<property name="userService" ref="userService" />
</bean>
</beans>
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="saveUserAction" class="com.vincent.action.SaveUserAction">
<result name="success" type="redirectAction">
listUserAction.action
</result>
</action>
<action name="listUserAction" class="com.vincent.action.ListUserAction">
<result name="success" type="dispatcher">
/list.jsp
</result>
</action>
<action name="deleteUserAction" class="com.vincent.action.DeleteUserAction">
<result name="success" type="redirectAction">
listUserAction.action
</result>
</action>
<action name="searchUserAction" class="com.vincent.action.SearchUserAction">
<result name="success" type="dispatcher">
/update.jsp
</result>
</action>
<action name="updateUserAction" class="com.vincent.action.UpdateUserAction">
<result name="success" type="redirectAction">
listUserAction.action
</result>
</action>
</package>
</struts>
User.cfg.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.vincent.model.User" table="User">
<id name="id" type="int">
<generator class="increment" />
</id>
<property name="firstName" column="firstname" type="string" />
<property name="lastName" column="lastname" type="string" />
<property name="age" column="age" type="int" />
</class>
</hibernate-mapping>
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"
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encoder filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
</web-app>
那么重要的配置文件都在上面啦。这面的就是java文件和JSP文件
DeleteUserAction.java
package com.vincent.action;
import com.opensymphony.xwork2.ActionSupport;
import com.vincent.model.User;
import com.vincent.service.UserService;
public class DeleteUserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
@Override
public String execute() throws Exception {
this.userService.deleteUser(user);
return SUCCESS;
}
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;
}
}
ListUserAction.java
package com.vincent.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vincent.service.UserService;
public class ListUserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserService userService;
@Override
public String execute() throws Exception {
Map request = (Map) ActionContext.getContext().get("request");
request.put("list", this.userService.findAllUsers());
return SUCCESS;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
SaveUserAction.java
package com.vincent.action;
import com.opensymphony.xwork2.ActionSupport;
import com.vincent.model.User;
import com.vincent.service.UserService;
public class SaveUserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
@Override
public String execute() throws Exception {
this.userService.createUser(user);
return SUCCESS;
}
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;
}
}
SearchUserAction.java
package com.vincent.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vincent.model.User;
import com.vincent.service.UserService;
public class SearchUserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
@Override
public String execute() throws Exception {
this.user = this.userService.findUserById(user.getId());
return SUCCESS;
}
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;
}
}
UpdateUserAction.java
package com.vincent.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.vincent.model.User;
import com.vincent.service.UserService;
public class UpdateUserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private User user;
private UserService userService;
@Override
public String execute() throws Exception {
this.userService.updateUser(user);
return SUCCESS;
}
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;
}
}
UserDAO.java
package com.vincent.dao;
import java.util.List;
import com.vincent.model.User;
public interface UserDAO {
public void createUser(User user);
public List<User> findAllUsers();
public void deleteUser(User user);
public void updateUser(User user);
public User findUserById(int id);
}
UserDAOImpl.java
package com.vincent.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.vincent.model.User;
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {
@Override
public void createUser(User user) {
this.getHibernateTemplate().save(user);
}
@Override
public List<User> findAllUsers() {
String hql = "from User u order by u.id desc";
List<User> list = this.getHibernateTemplate().find(hql);
return list;
}
@Override
public void deleteUser(User user) {
this.getHibernateTemplate().delete(user);
}
@Override
public void updateUser(User user) {
this.getHibernateTemplate().update(user);
}
@Override
public User findUserById(int id) {
return (User)this.getHibernateTemplate().get(User.class, id);
}
}
User.java
package com.vincent.model;
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;
}
}
UserServiceImpl.java
package com.vincent.service;
import java.util.List;
import com.vincent.dao.UserDAO;
import com.vincent.model.User;
public class UserServiceImpl implements UserService{
private UserDAO userDao;
@Override
public void createUser(User user) {
this.userDao.createUser(user);
}
@Override
public List<User> findAllUsers() {
return this.userDao.findAllUsers();
}
@Override
public void deleteUser(User user) {
this.userDao.deleteUser(user);
}
@Override
public void updateUser(User user) {
this.userDao.updateUser(user);
}
@Override
public User findUserById(int id) {
return (User)this.userDao.findUserById(id);
}
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
this.userDao = userDao;
}
}
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>test</title></head>
<body>
<h1><font color="red">Operation List</font></h1>
<s:a href="save.jsp">Save user</s:a>
<s:a href="listUserAction.action">List users</s:a>
</body>
</html>
list.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>Insert title here</title>
<script type="text/javascript">
function del(){
if(confirm("Are you sure?")){
return true;
}
return false;
}
</script>
</head>
<body>
<h1><font color="red">User List</font></h1>
<s:a href="/SSH/index.jsp"><<-</s:a>
<table border="1" width="80%" align="center">
<tr>
<th>序号</th>
<th>姓</th>
<th>名</th>
<th>年龄</th>
<th>删除</th>
<th>修改</th>
</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 align="center">
<s:a href="deleteUserAction.action?user.id=%{#us.id}" οnclick="return del()">Delete</s:a>
</td>
<td align="center">
<s:a href="searchUserAction.action?user.id=%{#us.id}">Update</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
save.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>Insert title here</title>
</head>
<body>
<h1><font color="red">Save user</font></h1>
<s:form action="saveUserAction" method="post">
<s:textfield label="First Name" name="user.firstName" />
<s:textfield label="Last Name" name="user.lastName" />
<s:textfield label="Age" name="user.age" />
<s:submit label="commit"></s:submit>
</s:form>
</body>
</html>
update.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>Insert title here</title>
</head>
<body>
<h1><font color="red">Update user</font></h1>
<s:form action="updateUserAction.action" method="post">
<table>
<tr><td><s:hidden name="user.id" value="%{user.id}"/></td></tr>
<tr>
<td>
<s:textfield lable="First Name" name="user.firstName" value="%{user.firstName}" />
</td>
</tr>
<tr>
<td>
<s:textfield lable="Last Name" name="user.lastName" value="%{user.lastName}" />
</td>
</tr>
<tr>
<td>
<s:textfield lable="Age" name="user.age" value="%{user.age}" />
</td>
</tr>
<tr>
<td>
<s:submit lable="submit" />
</td>
</tr>
</table>
</s:form>
</body>
</html>