eclipse搭建struts2环境及所遇到的问题
最近几天一直在搭建struts2框架,本身struts2框架的搭建是非常简单的,但不知道为什么最近就是总是报错,报了一大串的错
首先就是每次在类的根路径下创建struts.xml时,就报错,也不知道为什么,我还把eclipse换了一遍,结果没错了,我也是非常纳闷。
报的是:
The errors below were detected when validating the file "struts-2.3.dtd" via the file "struts.xml". In most cases these errors can be detected by validating "struts-2.3.dtd" directly. However it is possible that errors will only occur when struts-2.3.dtd is validated in the context of struts.xml.
然后一直百度,也没有找到解决办法,结果今天哇塞,终于解决了,算一下应该有5-6天了。原来这个错误是因为引入的约束文件出问题了。
我当时为了配置提示,然后把这个约束就用本地的dtd文件,结果我本地的dtd文件一不小心被改了,如果不配xml提示的话,xml文件默认会去http://struts.apache.org/dtds/struts-2.3.dtd这里找约束。
而我配了本地的dtd文件的话,就会在本地找我的文件了,结果我的文件被我改成这样了。
真的谢天谢地,今天终于让我发现了这个问题,要不然我要一直郁闷下去,还不知道错哪了。
我当时为了复制xml头文件的时候,不小心在这里多复制了一份,把这里删了,改成正确的文件的时候,就没有问题了。再也不报错了,并且也有提示了。
当然删了之后,要把struts.xml的内容先剪切一下(假装删除)--->保存,不报错之后,然后再复制上去--->再保存就ok了。然后把这个文件关了,再打开。
配置一下内容
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="p1" namespace="/user" extends="struts-default"> <action name="userLogin" class="com.fzxy.cidp.UserAction" method="userLogin"> <result name="success">/success.jsp</result> </action> </package> </struts>
这里的package中的属性: name是必须写的,这里name是一个标志符,可以随便取一个只要不重复的就行,比如人一出生就要取个名字一样。那个extends=“struts-default”,就是struts框架写好的一个package的name,我们继承它,就可以用里面的属性。
这里的action就是配置了访问路径和对应的哪个类来处理这个请求name=“userLogin”
这里的namespace就是到时候访问的地址栏中的内容,比如说,我想要在com.fzxy.cidp.UserAction类中处理请求,就得
http://localhost:8080/crm_test/user/userLogin(namespace/actionName)就可以对应上了。
那么就去建一个以之对应的类
然后就ok了。
这里需要导入的jar包就是
struts官网上面下载struts-blank里面的案例,里面有所需的jar包。
另外还会报一个错误
严重: Dispatcher initialization failed
Unable to load configuration. - action - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:9:80
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
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.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5699)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
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: Error building results for action userLogin in namespace /user - action - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:9:80
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:389)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
... 16 more
Caused by: No result type specified for result named 'success', perhaps the parent package does not specify the result type? - result - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:10:27
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildResults(XmlConfigurationProvider.java:644)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:387)
... 21 more
十月 15, 2018 11:24:47 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter struts
Unable to load configuration. - action - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:9:80
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:450)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
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.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5699)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
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: Unable to load configuration. - action - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:9:80
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
... 14 more
Caused by: Error building results for action userLogin in namespace /user - action - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:9:80
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:389)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
... 16 more
Caused by: No result type specified for result named 'success', perhaps the parent package does not specify the result type? - result - file:/D:/apache-tomcat-7.0.88/webapps/struts_test/WEB-INF/classes/struts.xml:10:27
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildResults(XmlConfigurationProvider.java:644)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:387)
... 21 more
这个错误说找不到action与之对应,我开始也找了半天找不到问题所在。后面终于发现了
是这个地方没有写,没有继承struts-default的包,就没有struts2默认的过滤器。这样就会报这个错误了。