SSH
手动配置SSH
运行环境
MyEclispe 8.5
Tomcat 6.01. 需要的jar包
struts2:

spring3.0:

hibernate3.3:

2. 配置文件
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>
<include file="struts-default.xml" />
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.custom.i18n.resources" value="messageResouce" />
<package name="strutsqs" extends="struts-default">
<action name="UserAction" class="UserAction">
<result name="result">/result.jsp</result>
</action>
</package>
</struts>
spring-config:
actionApplicationContext.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="UserAction" class="com.sun.action.UserAction">
<property name="userService" ref="userService" />
</bean>
</beans>
daoApplicationContext.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-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value=""></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.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/sun/orm/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Dao -->
<bean id="userDao" class="com.sun.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
serviceApplicationContext.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="userService" class="com.sun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</beans>
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<!-- dispatcher,接收到用户请求时,拦截用户action请求,根据dispacher分配到合适的控制器,控制器执行完毕后,
生成ActionReword对象,dispatcher根据该对象转发到view层,显示给用户-->
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--
*.action 后缀匹配,只要带有.action的都被拦截
/* 路径匹配,去掉根路径之后,以/开头的都被拦截
两者不能同时使用, 如/*.action 会报错
-->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--配置ApplicationContext.xml文件的路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config/*ApplicationContext.xml</param-value>
</context-param>
<!--在web容器启动时,告诉容器加载ApplicationContext.xml文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3. 程序结构

4. 具体代码
UserDao.java
package com.sun.dao;
import com.sun.orm.User;
public interface UserDao {
public User getUserByName(String name);
}
UserDaoImpl.java
package com.sun.dao.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sun.dao.UserDao;
import com.sun.orm.User;
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{
public User getUserByName(String name) {
// TODO Auto-generated method stub
String hql = "from User as u where u.name=?";
return (User) getHibernateTemplate().find(hql,name).get(0);
}
}
UserService.java
package com.sun.service;
import com.sun.orm.User;
public interface UserService {
public User getUserByName(String name);
}
UserServiceImpl.java
package com.sun.service.impl;
import com.sun.dao.UserDao;
import com.sun.orm.User;
import com.sun.service.UserService;
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public User getUserByName(String name) {
// TODO Auto-generated method stub
return userDao.getUserByName(name);
}
}
UserAction.java
package com.sun.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.orm.User;
import com.sun.service.UserService;
public class UserAction extends ActionSupport{
private UserService userService;
private String name;
private String password;
private User user;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
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 User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
System.out.println("name is " + name);
Map map = new HashMap();
user = userService.getUserByName(name);
System.out.println("password is " + user.getPassword());
map.put("result", "true");
return "result";
}
}
其中User.java和User.hbm.xml用MyEclipse可以自动生成,若用Eclipse可以自己编写
MyEclipse自动生成方法:
切换到MyEclispe Database Explore视图,选择要持久化的数据库表

提示:附件EclipseSSH.rar为SSH的一个例子,ssh.rar为ssh所需要的jar包
呵呵,好东东,能加个好友吗602091629一起学习
不好意思,之前的东西没有保留,毕竟已经很长时间了,你可以直接从这里下载啊
哥们,能给我发一份吗?qq 314501792 时间长了,这些我都快忘了。十分感谢。
不错,让我巩固了一下ssh