博文参考自:https://blog.csdn.net/skullFang/article/details/78202338
并根据出现的问题有所添加
最近刚开始学习struts2,配置就出现了各种问题,查阅了很多博文,花了很多时间,才看到了hello struts2!真的感动QAQ
记录一下,此次配置的全过程+问题解决
打开WEB-INF目录下的web.xml
红色那里删掉 .ng(ng是2.5以前的)
改为org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
之后,点击左上角file → project Structure → problems → [fix] → add ‘Struts 2’ to the arctifact → ok
回到index,jsp,右键Run index.jsp,如果能成功出现下面这个画面,基本上就成功了
但也可能有以下错误:
org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs.
Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them.
Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
解决:在apache目录下:apache-tomcat-9.0.11\conf修改logging.properties文件
在最后一行加入:
org.apache.jasper.servlet.TldScanner.level = FINE
或者
org.apache.jasper.servlet.TldScanner.level = SERVER
就不会再显示错误信息
或者如下错误
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
解决:此时便需要下载上述说出的log4j 2的zip文件
删除原来struts 2 library的log4j-api.jar包,在解压后的zip文件中选择log4j-api-2.11.2.jar和log4j-core-2.11.2.jar重新添加到struts2的library中
注:此处可能可以不删除原来的log4j-api.jar,但是由于api和core的版本不一样,为了避免不必要的错误,我选择加入相同版本的jar包,故删除了原来的api jar包
之后还需要对log4j 2进行配置,在src目录下建立log4j2.xml文件,添加如下内容:
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
Console>
Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
Root>
Loggers>
Configuration>
详细可参考:https://logging.apache.org/log4j/2.x/manual/configuration.html
可能还有诸如此类问题:
Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/C:/workspaces/test/test/target/work/webapp/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:29:72
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:208)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:131)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
或者
ERROR org.apache.struts2.dispatcher.Dispatcher - Dispatcher initialization failed
com.opensymphony.xwork2.config.ConfigurationException: Unable to load configuration.
请检查是否把上述提及的jar包重复放置在apache的lib文件和IDEA项目的library中了,如果是的话,删除掉apache中重复的包,运行,到这里已经解决我遇到的所有问题了
回到正题,当index.jsp页面能正常加载之后:
再进一步进行验证:
<struts>
<package name="hello" extends="struts-default">
<action name="sayHello">
<result>hello.jspresult>
action>
package>
struts>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
点击
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
hello
hello struts 2!
✌( •̀ ω •́ )y