好久没有搭建框架了,今天整理下以前的知识,整合下SSH,没想到手生了,一时半会各种异常出来,经过一番挣扎后,终于搞出来了雏形,
下面是我做整合框架的笔记,如果大家开发过程中又遇到的情况,可以参考下
首先是包的结构,(命名不算正规哈~,临时写的仓促了点)
框架是基于JAR包的基础上构建的,所以必须必备的jar包必须先下好,
如图:
没有的可以在本文源代码里下:
搭建框架:
修改web.xml, 加入 struts配置
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">
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
创建struts .xml
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
index.jsp
login.jsp
login.jsp
接下来我们创建一个 测试用的ACTION类,loginAction
package test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String username;
public String password;
public String execute(){
if(!username.equals("admin")){
super.addFieldError("username", "用户名错误!");
return ERROR;
}
if(!password.equals("001")){
super.addFieldError("password", "密码错误!");
return ERROR;
}
return SUCCESS;
}
public void validate(){
if(username==null||username.length()==0){
super.addActionError("用户名不能为空");
}
if(password==null||password.length()==0){
super.addActionError("密码不能为空");
}
}
}
为了方便我们调试strut是否有效,我们创建一个login.jsp 用于测试
修改下index.jsp 页面 登陆成功则友好提示一下
">
login success.
使用tomcat加载项目,如果启动时候不爆出异常,说明strust 基本配置完成,我们进入页面
如果登陆成功则转到
Index.jsp
接下配置spring 和hibernate
重新回归到 web.xml
我们在其中添加,spring的相关配置
contextConfigLocation
classpath:spring/applicationContext*.xml
org.springframework.web.context.ContextLoaderListener
创建applicationContext.xml 文件 我们在此使用注解驱动(其实说真的,spring到后面不用注解去写,也就失去它本身的意义,精髓就在于注解的简洁和易用)
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
classpath:spring/hibernate.cfg.xml
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
创建hibernate.cfg.xml (用户名密码,可以自己修改一下)
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost/test?characterEncoding=utf-8
com.mysql.jdbc.Driver
root
123456
org.hibernate.dialect.MySQLDialect
true
创建相关DTO类,以及hbm.xml 配置文件
TUser.java
package test.hibernate;
public class TUser implements java.io.Serializable {
private int userid ;
private String username;
private String allname;
private String address;
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAllname() {
return this.allname;
}
public void setAllname(String allname) {
this.allname = allname;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
}
TUser.hbm.xml
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
由于测试关系,我们在这里就省略了DAO层,直接写service层类去测试 ,采用注解标记
创建相关service 接口以及实现类
IUserService.java
package test.service.imp;
import java.util.List;
import test.hibernate.TUser;
public interface IUserService {
public void saveTuser(TUser user);
public List getUserById( String id);
}
UserServiceImpl.java
package test.service.impl;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test.hibernate.TUser;
import test.service.imp.IUserService;
@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {
@Resource(name = "hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public void saveTuser(TUser user) {
hibernateTemplate.save(user);
hibernateTemplate.flush();
}
@SuppressWarnings("unchecked")
@Transactional(propagation=Propagation.REQUIRED)
public List getUserById(String id) {
final String ids = id;
//List list = hibernateTemplate.find("from TUser where userid = ?");
List list =hibernateTemplate.executeFind(new HibernateCallback() {
public List doInHibernate(Session session) throws HibernateException,
SQLException {
Query query = (Query) session.createQuery("from TUser where userid = ? ");
query.setString(0, ids);
return query.list() ;
}
});
return list;
}
}
接下来我们写一个main 主类来测试我们的spring 和hibernate
SpringTest.java
package test.spring;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import test.hibernate.TUser;
import test.service.imp.IUserService;
@Component("springTest")
public class SpringTest {
@Resource(name = "userServiceImpl")
private IUserService userService ;
public static void main( String[] args ) {
//加载spring配置文件,初始化IoC容器
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
//从容器 接管Bean
// TUser user = (TUser) ac.getBean("TUser");
//输出欢迎信息
// System.out.println( "Hello:" + user.getUsername() + ";u is in " + user.getAddress() + " ; and b is " + user.getAllname() );
// SessionFactory ss = (SessionFactory) ac.getBean("sessionFactory");
// HibernateTemplate ht = new HibernateTemplate(ss);
// HibernateTemplate ht = (HibernateTemplate) ac.getBean("hibernateTemplate");
//
// List list = ht.find("from TUser ");
SpringTest st =(SpringTest) ac.getBean("springTest");
// TUser tu = new TUser();
// tu.setAddress("河西");
// tu.setAllname("河西走廊");
// tu.setUserid(45);
// tu.setUsername("故乡");
//
// st.userService.saveTuser(tu);
List list = st.userService.getUserById("4");//ID在数据库内找到相应的
for(TUser xx : list){
System.out.println(xx.getAddress()+"||"+xx.getAllname()+"||"+xx.getUsername());
}
}
}
如果出现下面提示,说明你的配置已经完成