在 Struts 的应用开发中,主要涉及到两个配置文件,即 web.xml 和 Struts-config.xml 其中 web.xml 是所有 Java Web 应用所需要的,而后者是 Struts 应用所专有的.
在采用 MyEclipse 开发Struts 应用时,这些配置文件一般可以由 MyEcplise 自动配置和管理,在某些特殊情况下只需稍微进行一下调整即可
在web.xml中 和 Struts-config.xml有关的配置项主要有:配置中央控制器 ActionServlet 及其初始化参数--配置欢迎文件清单--配置错误处理--配置Struts标签库
在Struts-config.xml 中主要配置以下项目: 配置数据源--配置ActionForm Bean--配置异常处理--配置全局转发关系--配置Action映射--配置消息资源
详细的配置示例如下: web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name> //配置一个 servlet 名为action,类为org.apache.struts.action.ActionServlet
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param> //配置 servlet的初始参数config参数用来设置struts-config.xml的路径
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param> //调试的详细级别,0(off),1(最不严格)直到6(最严格) 大多数Struts组件设置为级别 0 至 3
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param> //应用配置文件的Digester 的调试详细级别,级别设置情况类似 debug 参数
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param> //设置该 servlet 在web应用启动时.被加载的次序,数值越小就越先加载,若为负或没有设置,就在首次访问时再加载
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>//配置一个servlet 映射, *.do 表明 ActionServlet 只负责处理所有以 .do 结尾的 url,可以更改,但推荐默认
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>//配置欢迎文件清单,当访问Web应用的根 url 时,将按此设置依次寻找,找到后将被自动执行.
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config> //设置jsp的配置信息
<taglib>
<taglib-uri>/strust-html</taglib> //配置Struts-html 标签库
<taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/struts-bean</taglib> //配置Struts-bean标签库
<taglib-location>/WEB-INF/tld/struts-bean.tld<taglib-cation>
</taglib>
<taglib>
<taglib-uri>/struts-logic</taglib> //配置Struts-logic标签库
<taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
</taglib>
</jsp-config>
<error-page>
<error-code>404</error-code> //当容器波捕获到HTTP404错误时,用此页面来代替原始的404错误信息呈现给用户
<location>/error/404.htm</location> 起到包装信息的作用
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error/500.htm</location>
<error-page>
<exception-type>exception-type</exception-type> //当容器捕获到 exception-type 所指定的异常是,用此页面代替原始<loaction>/error/error.htm</location> 异常信息呈现给用户
</error-page>
</web-app>