SSH项目搭建(五)——web.xml文件配置

时间:2023-03-08 22:02:17

上一章写到pom.xml有一个报错,说找不到web.xml文件。确实是这样的,因为我们用maven搭建的web层里就是没有这个文件。我们能看到,webapp文件夹里是空的。

没有,就想办法把它弄出来。

一、右键项目>>>>Java EE Tools>>>>Generate Deployment Descriptor Stub

  SSH项目搭建(五)——web.xml文件配置

  然后就出来了。

  参考: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再写一遍。

  SSH项目搭建(五)——web.xml文件配置

  4、这个时候就能运行成功了。


  但我还是想笑一下/捂脸,说好是ssh项目的搭建嘛,到时候肯定用的struts2啊,servlet就不需要了,以上操作就白废了。

  

三、配置web.xml

  阿豪聊干货:

  https://www.cnblogs.com/hafiz/p/5715523.html

  下面的东西先配着,下一章再配SSH

  1、配Spring初始化参数

     <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

  2、配openSessionInView

     <filter>
<filter-name>openSessionInView</filter-name>
<filter-class>common.toolkit.java.orm.hibernate.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</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、配监听

     <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

  5、配session时长(30分钟)

     <session-config>
<session-timeout>30</session-timeout>
</session-config>

  6、配置错误页面

     <error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>

  

  当然,前面明明是可以跑起来的,配完这些就跑不起来了,那是因为这里配了很多文件,但是这些文件都是找不到的,我们还没配嘛。

  下一章,配SSH整合。