上一章寫到pom.xml有一個報錯,說找不到web.xml文件。確實是這樣的,因為我們用maven搭建的web層里就是沒有這個文件。我們能看到,webapp文件夾里是空的。
沒有,就想辦法把它弄出來。
一、右鍵項目>>>>Java EE Tools>>>>Generate Deployment Descriptor Stub
然后就出來了。
參考:https://www.cnblogs.com/pjlhf/p/8782651.html
二、在webapp目錄下新建一個index.jsp
1、根據默認的路徑,就是要在這里建一個,才能讀取到。
2、會發現一個bug
Multiple annotations found at this line:
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
3、這個時候去Java Build Path 里面配置Server Runtime 為Tomcat 也可以解決問題,但是……
我們回想上一章,父項目的pom.xml 里已經配置了Servlet。
找了一下,發現這個是在dependencyManagement節點里的。
要用的話,就需要在子項目中的pom.xml再寫一遍。
或者在dependencyManagement外面的那個dependencies再寫一遍。
4、這個時候就能運行成功了。
但我還是想笑一下/捂臉,說好是ssh項目的搭建嘛,到時候肯定用的struts2啊,servlet就不需要了,以上操作就白廢了。
三、配置web.xml
阿豪聊干貨:
https://www.cnblogs.com/hafiz/p/5715523.html
下面的東西先配着,下一章再配SSH
1、配Spring初始化參數
1 <context-param> 2 <param-name>contextConfigLocation</param-name> 3 <param-value> 4 classpath:applicationContext.xml 5 </param-value> 6 </context-param>
2、配openSessionInView
1 <filter> 2 <filter-name>openSessionInView</filter-name> 3 <filter-class>common.toolkit.java.orm.hibernate.OpenSessionInViewFilter</filter-class> 4 <init-param> 5 <param-name>singleSession</param-name> 6 <param-value>true</param-value> 7 </init-param> 8 </filter> 9 <filter-mapping> 10 <filter-name>openSessionInView</filter-name> 11 <url-pattern>/*</url-pattern> 12 </filter-mapping>
3、配Struts2
1 <filter>
2 <filter-name>struts2</filter-name>
3 <filter-class>com.wisdombud.jrj.actions.filter.MyStrutsFilter
4 </filter-class>
5 </filter>
6 <filter-mapping>
7 <filter-name>struts2</filter-name>
8 <url-pattern>/*</url-pattern>
9 <dispatcher>REQUEST</dispatcher>
10 <dispatcher>FORWARD</dispatcher>
11 <dispatcher>INCLUDE</dispatcher>
12 </filter-mapping>
4、配監聽
1 <listener> 2 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 3 </listener> 4 <listener> 5 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 6 </listener>
5、配session時長(30分鍾)
1 <session-config> 2 <session-timeout>30</session-timeout> 3 </session-config>
6、配置錯誤頁面
1 <error-page> 2 <exception-type>java.lang.Throwable</exception-type> 3 <location>/500.jsp</location> 4 </error-page> 5 <error-page> 6 <error-code>500</error-code> 7 <location>/500.jsp</location> 8 </error-page> 9 <error-page> 10 <error-code>404</error-code> 11 <location>/404.jsp</location> 12 </error-page>
當然,前面明明是可以跑起來的,配完這些就跑不起來了,那是因為這里配了很多文件,但是這些文件都是找不到的,我們還沒配嘛。
下一章,配SSH整合。