本人初级程序员,今天无聊做了个demo,此demo是SSH框架。由于太久没有搭建,前前后后也出现很多琐碎的问题,特地总结配置文件的存放和配置。
项目目录如图:
.
hibernate.cfg.xml和struts.xml放在src/main/resources目录下,applicationContext.xml则在下一层的spring。
接下里是web.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value> -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>参数CharacterEncodingFilter:顾名思义就是编码过滤器,当jsp页面与后台java代码之间传值编码不一致时,过滤器就起到统一编码的作用,解决传值乱码问题。
参数struts2:spring集成struts的必写配置。
参数contextConfigLocation:配置spring配置文件,这个路径问题调试了很久,
如果applicationContext.xml放在WEB-INF目录下,参数值是<param-value>/WEB-INF/applicationContext.xml</param-value>;
如果applicationContext.xml放在src目录下,参数值是<param-value>classpath:applicationContext.xml</param-value>;
demo中我将配置文件放到src/main/resources的spring,所以参数值是classpath*:spring/*.xml,匹配编译路径(src/main/resources)到spring的所有xml文件。
applicationContext.xml:
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>配置hibernate.cfg.xml:因为hibernate配置文件放在src/main/resources,属于编译路径,直接能获取。
最后说下struts.xml,在网上查struts.xml文件应该放在哪个位置查了很久,一般都放在src路径,否则会报错如果要放在WEB-INF,需要在web.xml多加几行配置,比较麻烦,所以也是建议放在src。

本文是初级程序员对SSH框架配置的总结,重点讲述了hibernate.cfg.xml、struts.xml和applicationContext.xml的存放位置。在src/main/resources下存放hibernate.cfg.xml和struts.xml,而applicationContext.xml放在src/main/spring。关于web.xml的配置,struts2参数用于集成struts,contextConfigLocation参数配置spring文件路径,根据applicationContext.xml的实际位置,可能为<param-value>/WEB-INF/applicationContext.xml</param-value>或<param-value>classpath:applicationContext.xml</param-value>。struts.xml通常建议放在src目录下,以避免额外的web.xml配置。

1249

被折叠的 条评论
为什么被折叠?



