s2sh整合

一. 导包

框架的版本情况:Struts2.3.4 + hibernate3.6.0+spring3.2.5
有一些是非必需的,只不过为了功能的完整性,就全部都弄过去了,比如导出Word,Excel,还有文件上传,日志等。

antlr-2.7.6.jar
aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
c3p0-0.9.1.2.jar
commons-codec-1.6.jar
commons-collections-3.1.jar
commons-fileupload-1.2.2.jar
commons-httpclient-3.0.1.jar
commons-io-2.0.1.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
core-3.0.0.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.11.0.GA.jar
jaxen-1.1-beta-6.jar
json-lib-2.4-jdk15.jar
jstl.jar
jta-1.1.jar
junit-4.12.jar
log4j-1.2.17.jar
mysql-connector-java-5.1.12-bin.jar
ognl-3.0.5.jar
poi-3.15.jar
slf4j-api-1.6.1.jar
spring-aop-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-jdbc-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
standard.jar
struts2-core-2.3.4.1.jar
struts2-spring-plugin-2.3.4.1.jar
xwork-core-2.3.4.1.jar

二. 配置web.xml



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">


struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter


struts2
/*



contextConfigLocation
classpath:bean.xml


org.springframework.web.context.ContextLoaderListener


index.jsp


三.写applicationContext.xml

















org.hibernate.dialect.MySQLDialect
true
update




classpath:com/ycTime/entity/.hbm.xml

























四.写entity

  • Notice.java

    public class Notice {
    private int id;
    private String title;
    private String content;
    private Date doTime;
    private int status;// 0表示是草稿,1表示是已经发布
    public int getStatus() {
    return status;
    }
    public void setStatus(int status) {
    this.status = status;
    }
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }
    public Date getDoTime() {
    return doTime;
    }
    public void setDoTime(Date doTime) {
    this.doTime = doTime;
    }
    }

  • Notice.hbm.xml


    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">











五. 写Dao

其中,@Repository是spring注解,上文中applicationContext.xml的扫描那部分,就是和这里配合的,配置了扫描之后,spring在加载applicationContext.xml的时候就会自动扫描带注解的类并在spring容器中创建相关的实例,以及完成依赖注入。@Repository是持久层注解,@Autowired是自动注入,NoticeDao就注入了SessionFactory。

@Repository
public class NoticeDao {
@Autowired
private SessionFactory sessionFactory;
public boolean saveNotice(Notice notice){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
try {
transaction.begin();
session.saveOrUpdate(notice);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
return false;
}finally {
session.close();
}
return true;
}
}

六.写Service

也是用了注解,类似

@Service
public class NoticeService {
@Autowired
private NoticeDao noticeDao;
public boolean saveNotice(String title, String content, Date doTime) {
Notice notice = new Notice();
notice.setContent(content);
notice.setDoTime(doTime);
notice.setTitle(title);
notice.setStatus(0);
boolean b = noticeDao.saveNotice(notice);
return b;
}
}

七. 写action


@Controller
public class NoticeAction {
@Autowired
private NoticeService noticeService;
public String addNotice(){
HttpServletRequest request = ServletActionContext.getRequest();
String title = request.getParameter("title");
String content = request.getParameter("content");
boolean b = noticeService.saveNotice(title,content,new Date());
if(b == true){
return "success";
}
reutrn "error";
}
}

八. 写Struts配置文件

Struts.xml:


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">





控制跳转的struts配置文件 NoticeStruts.xml:

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



success.jsp
error.jsp


九.写jsp页面


标题:

内容:



至此,ssh整合完成。

你可能感兴趣的:(s2sh整合)