1.首先建立maven-webapp项目,选择如下选项,
点击NEXT,填写项目信息,之后默认next,直至finish
项目建好后,项目结构如下:
可在main目录下手动建立项目所需目录,如下:
并右键点击Java文件夹,标记为:(Sources root 文件夹用来存放Java class文件,若不标记,在new文件时没有Java class选项)
并右键点击resources文件夹,标记为:(Resources Root用来存放资源文件,如XML,properties之类的)
标记后目录如下:
2.在POM.xml里添加依赖(不用maven时其实就是导入jar包),所需的各种依赖可去https://mvnrepository.com/找得。
pom.xml添加依赖后如图(这是我个人添加的依赖,也可以根据自己的需要去上面链接找寻依赖,在pom.xml添加依赖后,会自动从网上下载相关jar包存放在external libraries里,无需自己导入jar包):
4.0.0
com.lxw
infoManeger
1.0-SNAPSHOT
war
infoManeger Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
junit
junit
4.11
test
org.springframework
spring-context
5.1.4.RELEASE
org.springframework
spring-core
5.1.4.RELEASE
org.springframework
spring-orm
5.1.4.RELEASE
org.springframework
spring-web
5.1.4.RELEASE
org.apache.struts
struts2-core
2.5.20
org.apache.struts
struts2-spring-plugin
2.5.20
org.hibernate
hibernate-core
5.4.1.Final
org.junit.jupiter
junit-jupiter-api
5.3.2
compile
com.mchange
c3p0
0.9.5.2
c3p0
c3p0
0.9.1.2
org.hibernate
hibernate-c3p0
5.4.1.Final
mysql
mysql-connector-java
8.0.13
infoManeger
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
3.在resources文件夹下建好spring配置文件 applicationContext.xml
4.在resources文件夹下建好struts配置文件 struts.xml
5.在web.xml里配置spring和struts
Archetype Created Web Application
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
struts
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
config
struts.xml
struts
/*
6.完善struts的配置
1)新建一个JSP,建立表单
<%@taglib uri="/struts-tags" prefix="s"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
保存信息
名称
描述
2)在Java文件夹下建立包结构以及相关类
Info类
public class Info {
private Integer pid;
private String name;
private String description;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
}
InfoAction类(这里利用modelDriven接口将JSP表单里的数据传递到Info类中)
public class InfoAction extends ActionSupport implements ModelDriven {
private Info info = new Info();
private InfoService infoService;
public void setInfoService(InfoService infoService) {
this.infoService = infoService;
}
@Override
public Info getModel() {
return info;
}
public void save(){
System.out.println("action.....");
infoService.save(info);
}
}
InfoService类
public class InfoService {
private InfoDao infoDao;
public void setInfoDao(InfoDao infoDao) {
this.infoDao = infoDao;
}
public void save(Info info){
System.out.println("service...");
infoDao.save(info);
}
}
InfoDao类
public class InfoDao {
public void save(Info info){
System.out.println("dao.......");
System.out.println(info.toString());
}
}
3)在struts.xml文件中配置数据交互
7.在applicationContext.xml文件中将spring与struts整合,即配置bean
8.配置好tomcat容器,运行JSP页面,报错:
ERROR Dispatcher Dispatcher initialization failed
com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=com.opensymphony.xwork2.FileManagerFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setFileManagerFactory(com.opensymphony.xwork2.FileManagerFactory).
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:140)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:111)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:89)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:85)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:70)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:66)
at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:155)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:79)
at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:123)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:463)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:502)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:500)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:555)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:500)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:195)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.getContainer(Dispatcher.java:957)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:463)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:496)
at org.apache.struts2.dispatcher.InitOperations.initDispatcher(InitOperations.java:73)
at org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:61)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
at org.apache.catalina.core.ApplicationFilterConfig.
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4747)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5389)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:731)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1730)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:485)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:434)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException: No mapping found for dependency [type=com.opensymphony.xwork2.FileManagerFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setFileManagerFactory(com.opensymphony.xwork2.FileManagerFactory).
at com.opensymphony.xwork2.inject.ContainerImpl.createParameterInjector(ContainerImpl.java:228)
at com.opensymphony.xwork2.inject.ContainerImpl.getParametersInjectors(ContainerImpl.java:219)
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:115)
at com.opensymphony.xwork2.inject.ContainerImpl$3.create(ContainerImpl.java:112)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:137)
... 69 more
解决措施,在web.xml中将
struts
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
config
struts.xml
改为:
struts
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
config
struts-default.xml,struts-plugin.xml,struts.xml
再次运行strutsDemo.jsp页面
点击保存:
至此spring和struts整合完成
9.利用mysql建立数据库sshdemo(暂不需要建表,hibernate映射文件会自动建表)
10.在resources文件夹下,建立jdbc.properties文件用来设置jdbc连接信息
11.在resources文件夹下,建立info.hbm.xml文件来设置类-表映射
info.hbm.xml:
12.在applicationContext.xml文件中将spring与hibernate整合
org.hibernate.dialect.MySQL5Dialect
true
true
update
info.hbm.xml
13.修改InfoDao类
public class InfoDao extends HibernateDaoSupport {
/*
在HibernateDaoSupport里有这个函数,在applicationContext.xml文件里对InfoDao注入了sessionFactory
注入之后就自动生成了hibernateTemplate,故下面save函数可以直接使用
* public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = this.createHibernateTemplate(sessionFactory);
}
}
* */
public void save(Info info){
this.getHibernateTemplate().save(info);
System.out.println("dao……");
}
}
14.在InfoService类上加上@Transactional注解
@Transactional
public class InfoService {
private InfoDao infoDao;
public void setInfoDao(InfoDao infoDao) {
this.infoDao = infoDao;
}
public void save(Info info){
System.out.println("service...");
infoDao.save(info);
}
}
15.运行tomcat,打开jsp界面 ,填写表单
后台输出如下
数据库数据如下:
至此,SSH整合完成。
如有错误和不详细的地方欢迎指出,谢谢~