选择spring和struts,注意版本,不同的struts版本,过滤器的位置不一样
选择存放位置,并点击完成创建项目,在创建过程中会自动下载相关jar
初始化完成后的目录结构为
修复生成的web.xml文件
1.检查生成的struts2过滤器和选择的版本是否一致(现在我的是不匹配的需要修改StrutsPrepareAndExecuteFilter的位置)
2.spring的监听器ContextLoaderListener,因为没有生成spring-web-4.3.18.RELEASE.jar,所有要下载spring-web-4.3.18.RELEASE.jar,spring框架下载地址https://repo.spring.io/release/org/springframework/spring/
新的web.xml文件为:
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
struts2
index.jsp
org.springframework.web.context.ContextLoaderListener
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
/*
现在生成的路径还没还没有生成lib里面的jar包,配置IDEA生成路径
在src目录里新建一个处理请求的action,取名叫LoginAction.java
importcom.opensymphony.xwork2.ActionSupport;/***@authormx
* @description: TODO
* @date 2019-06-26 10:43*/
public class LoginAction extendsActionSupport {privateString username;privateString password;publicString getUsername() {returnusername;
}public voidsetUsername(String username) {this.username =username;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}
@Overridepublic String execute() throwsException {if (username.equals("admin") && password.equals("123456")) {returnSUCCESS;
}else{returnLOGIN;
}
}
}
增加sping容器默认的配置文件(也可以指定文件名,这里就直接使用默认的文件名)applicationContext.xml,在里面注入创建的action
在struts.xml配置struts2的映射关系
/p>
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
/WEB-INF/jsp/success.jsp
/index.jsp
在WEB-INF目录下面新建jsp目录,然后在创建success.jsp
${username}登录成功登录成功!
修改index.jsp文件
用户登录用户登录
用户名: | |
密码: |
本来以为在这点就配置完成了,然后配置tomcat运行项目:
结果出现错误:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
ERROR Dispatcher Dispatcher initialization failed
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.opensymphony.xwork2.inject.ContainerImpl$MethodInjector.inject(ContainerImpl.java:289)
at com.opensymphony.xwork2.inject.ContainerImpl$ConstructorInjector.construct(ContainerImpl.java:422)
at com.opensymphony.xwork2.inject.ContainerBuilder$5.create(ContainerBuilder.java:231)
at com.opensymphony.xwork2.inject.Scope$2$1.create(Scope.java:52)
at com.opensymphony.xwork2.inject.ContainerBuilder$3.create(ContainerBuilder.java:106)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:584)
at com.opensymphony.xwork2.inject.ContainerBuilder$7.call(ContainerBuilder.java:581)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:560)
at com.opensymphony.xwork2.inject.ContainerBuilder.create(ContainerBuilder.java:581)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.createBootstrapContainer(DefaultConfiguration.java:287)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:162)
然后查阅资料,有说,添加classpath下的目录添加一个log4j2.xml,有说映入log4j-core包的,但是最后都没有解决,最新想是不是自动stuts2包的问题,就去官网https://struts.apache.org/download.cgi下载最新的试试,下载文件
先删除原来structs2 的jar
解压下载的文件,引入新的jar
然后在运行tomcat得到正确的页面
输入正确的用户名和密码后,跳转