ssh還是畢業的時候自覺了下下,對這個框架本不是很了解,加上出來工作用的一直是play框架,今天閒得慌就寫了個ssh的demo,途中遇到了各種一大堆的錯誤,大部不外乎是缺少jar包造成的,到整合struts2的時候我不想把struts.xml文件放在默認目錄src下,就改變了它的放置目錄,放到WEB-INF\xml。
啟動tomcat時也沒有報錯,然後訪問配置好的action,結果當然是404,然後仔細看下tomcat的打印信息我發現了這樣一行信息:
Unable to locate configuration files of the name struts.xml 直譯過來是:沒有找到名字為struts的配置文件。
才知道是struts.xml沒有被加載,因為我把struts.xml文件放在WEB-INF\xml下了,tomcat的默認初始化路徑是項目的src目錄,當然找不到struts的配置文件。
我反編譯org.apache.struts2.dispatcher.FilterDispatcher類發現它有一個init方法,又去網上找了些相關資料看下,找到了個解決方案,把web.xml文件改成:
- <filter>
- <filter-name>struts</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/xml/struts.xml</param-value>
- </init-param>
- </filter>
好,重啟服務器,tomcat直接報錯:
- Caused by: com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException:
- No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default']
- in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.setObjectFactory(com.opensymphony.xwork2.ObjectFactory).
- at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:144)
- at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)
- at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)
- at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:86)
- at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)
- at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:67)
- at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
- at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
- at java.util.concurrent.FutureTask.run(FutureTask.java:138)
- at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
- at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
- at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:490)
- at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:530)
- at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:528)
- at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:580)
- at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:528)
- at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:233)
- at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
- at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
- at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
- ... 28 more
沒有找到依賴配置文件com.opensymphony.xwork2.ObjectFactory。
雖然web.xml中指明了struts的配置文件的路徑,但是他依賴的如struts-default.xml沒有初始化,那就把它加入進去:
- <filter>
- <filter-name>struts</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>struts-default.xml,struts-plugin.xml,../xml/struts.xml</param-value>
- </init-param>
- </filter>
注意後面的:"../xml/struts.xml",為什麼這樣寫呢,因為加載struts.xml時並不是加載的項目中存放配置文件的絕對路徑(web_inf/xml),而是src目錄的也就是classes目錄下,所以就寫相對路徑。
以下是struts.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.devMode" value="true" />
- <!-- 配置struts2的常量,指定Struts的工廠類,托管給Spring -->
- <constant name="struts.objectFactory" value="spring" />
-
- <!-- 引入配置文件 -->
- <include file="../xml/struts/login.xml"/>
-
-
- </struts>
login.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
-
- <constant name="struts.action.extension" value="do,h"/>
- <package name="loginPackage" namespace="/" extends="struts-default">
-
- <action name="login_*" method="{1}" class="www.com.controller.LogigAction">
- <result name="SUCCESS">/index.jsp</result>
- <result name="ERR">/index.jsp</result>
- </action>
-
- </package>
-
- </struts>
上面的過程是親身並測試通過,希望這個總結對遇到同樣錯誤的新手有幫助!