1. 程式人生 > >eclipse搭建struts2環境及所遇到的問題

eclipse搭建struts2環境及所遇到的問題

頭文件 exceptio 訪問 ble standard filters patch ner direct

最近幾天一直在搭建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默認的過濾器。這樣就會報這個錯誤了。

eclipse搭建struts2環境及所遇到的問題