工具:Eclipse(Maven管理工具)+Tomcat+Mysql。
1.新建一个Maven工程(maven-archetype-webapp)。
打开File ——>new——>Maven Project,直接next,选择(maven-archetype-webapp).接着输入Grouid和ArtifactId,点击finish等待生成项目即可。
2.修改pom.xml文件,我这里的依赖是网上找的。保存后会自动下载jar包在你的repository文件夹中(这里要保证连网)。
4.0.0
com.gh
SSH1
war
0.0.1-SNAPSHOT
SSH1 Maven Webapp
http://maven.apache.org
UTF-8
2.5.10
4.3.8.RELEASE
4.2.4.Final
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-orm
${spring.version}
org.apache.struts
struts2-core
${struts.version}
org.apache.struts
struts2-spring-plugin
${struts.version}
org.hibernate
hibernate-core
${hibernate.version}
mysql
mysql-connector-java
5.1.42
com.mchange
c3p0
0.9.5
org.aspectj
aspectjweaver
1.8.10
org.slf4j
slf4j-log4j12
1.7.25
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.6
org.apache.taglibs
taglibs-standard-spec
1.2.5
org.apache.taglibs
taglibs-standard-impl
1.2.5
SSH1
org.apache.maven.plugins
maven-compiler-plugin
3.5.1
1.7
1.7
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
true
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
/${project.artifactId}
View Code
3.修改web.xml文件。
ssh
index.jsp
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
View Code
4.添加struts.xml文件和applicationcontext.xml文件在src目录下。
struts.xml
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
/index2.jsp
/error.jsp
View Code
applicationcontext.xml
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"default-autowire="byName">
org.hibernate.dialect.MySQLDialect
true
classpath:entity/
View Code
5.新建实体,动作类和接口等。结构如下图。
UserAction.java
packageaction;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importcom.opensymphony.xwork2.ActionSupport;importentity.User;importservice.UserService;
@Controllerpublic class UserAction extendsActionSupport{/****/
private static final long serialVersionUID = 1L;
@Autowiredprivate UserService userService;//�Զ�ע��
publicString toLogin(){
Listlist=userService.findAll();for(User user : list) {
System.out.println(user.getUserName());
}return "success";
}
}
View Code
BaseDao.java
packagecommon;importjava.util.List;public interface BaseDao{public ListfindAll();public intinsert(T t);public int update (T t,intid);public int delete(intid);public T findById(intid);
}
View Code
BaseService.java
packagecommon;importjava.util.List;public interface BaseService {public ListfindAll();public booleaninsert(T t);public boolean update (T t,intid);public boolean delete(intid);public T findById(intid);
}
View Code
UserDao.java
packagedao;importcommon.BaseDao;importentity.User;public interface UserDao extends BaseDao{
}
View Code
UserDaoimpl.java
packagedao.impl;importjava.util.List;importorg.hibernate.Query;importorg.springframework.orm.hibernate5.support.HibernateDaoSupport;importorg.springframework.stereotype.Repository;importdao.UserDao;importentity.User;
@Repositorypublic class UserDaoimpl extends HibernateDaoSupport implementsUserDao {public ListfindAll() {//TODO Auto-generated method stub
String sql="select * from user";
Query query=getSessionFactory().openSession().createSQLQuery(sql).addEntity(User.class);
List list=query.list();returnlist ;
}public intinsert(User t) {//TODO Auto-generated method stub
return 0;
}public int update(User t, intid) {//TODO Auto-generated method stub
return 0;
}public int delete(intid) {//TODO Auto-generated method stub
return 0;
}public User findById(intid) {//TODO Auto-generated method stub
return null;
}
}
View Code
UserService.java
packageservice;importcommon.BaseDao;importentity.User;public interface UserService extends BaseDao{
}
View Code
UserServiceimpl.java
packageservice.impl;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importdao.UserDao;importentity.User;importservice.UserService;
@Transactional
@Servicepublic class UserServiceimpl implementsUserService{
@AutowiredprivateUserDao userdao;public ListfindAll() {//TODO Auto-generated method stub
returnuserdao.findAll();
}public intinsert(User t) {//TODO Auto-generated method stub
return 0;
}public int update(User t, intid) {//TODO Auto-generated method stub
return 0;
}public int delete(intid) {//TODO Auto-generated method stub
return 0;
}public User findById(intid) {//TODO Auto-generated method stub
return null;
}
}
View Code
User.java
packageentity;//Generated 2018-3-29 10:41:32 by Hibernate Tools 3.4.0.CR1
/*** User generated by hbm2java*/
public class User implementsjava.io.Serializable {/****/
private static final long serialVersionUID = 1L;privateInteger userid;privateString userName;privateString pwd;publicUser() {
}publicUser(String userName, String pwd) {this.userName =userName;this.pwd =pwd;
}publicInteger getUserid() {return this.userid;
}public voidsetUserid(Integer userid) {this.userid =userid;
}publicString getUserName() {return this.userName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getPwd() {return this.pwd;
}public voidsetPwd(String pwd) {this.pwd =pwd;
}
}
View Code
User.hbm.xml
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
View Code
jsp代码
Insert title here用户名:密码:
View Code
最后部署项目下tomcat中,运行console输出如下:
总结:在这过程中遇到了不少问题。一般都是包的问题和版本问题。
1.在搭建框架运行不报错后,准备去请求数据时,却一直找不到动作类。在跳转时jsp页面一直报如下错。
解决办法:参考博客:https://blog.csdn.net/axwolfer/article/details/40164151
在struts.xml中添加如下代码:
2.Hibernate版本问题,Hibernate升级后有了不少变化,具体网上看。之前一直用Hibernate3,在搭建开始时我用的是Hibernate5,后来改成了Hibernate4。
使用5时请求数据库时报如下错:
Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext。
解决办法:将Hibernate中的依赖改成Hibernate4版本的即可。