選擇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文件為:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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"> <display-name>struts2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
現在生成的路徑還沒還沒有生成lib里面的jar包,配置IDEA生成路徑
在src目錄里新建一個處理請求的action,取名叫LoginAction.java
import com.opensymphony.xwork2.ActionSupport; /** * @author mx * @description: TODO * @date 2019-06-26 10:43 */ public class LoginAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { if (username.equals("admin") && password.equals("123456")) { return SUCCESS; } else { return LOGIN; } } }
增加sping容器默認的配置文件(也可以指定文件名,這里就直接使用默認的文件名)applicationContext.xml,在里面注入創建的action
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userClass" class="LoginAction" scope="prototype"/> </beans>
在struts.xml配置struts2的映射關系
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="Login" class="LoginAction"> <result name="success">/WEB-INF/jsp/success.jsp</result> <result name="login">/index.jsp</result> </action> </package> </struts>
在WEB-INF目錄下面新建jsp目錄,然后在創建success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>${username}登錄成功</title> </head> <body> <h1>登錄成功!</h1> </body> </html>
修改index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>用戶登錄</title> </head> <body> <h1>用戶登錄</h1> <form action="Login.action" method="post"> <table> <tr> <td>用戶名:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"></td> </tr> <tr> <td colspan="2" style="text-align: center"><input type="submit" value="登錄"></td> </tr> </table> </form> </body> </html>
本來以為在這點就配置完成了,然后配置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得到正確的頁面
輸入正確的用戶名和密碼后,跳轉