4、 配置标签库,标签库是Struts自带的一些组件库,采用JSP规范中Tag-lib的方式供大家使用,正是因为存在这么丰富的标签库,使得采用Struts的开发才显得这么方便,高效。
<web-app>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>host</param-name>
<param-value>localhost</param-value>
</init-param>
<init-param>
<param-name>port</param-name>
<param-value>7001</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
标签库采用<taglib>定义,<taglib>含有两个子元素,<taglib-uri>和<taglib-location>,<taglib-uri>用户定义标签库的唯一表示符,可以理解为名字,以后要在jsp页面中使用这个标签库,靠的就是它。<taglib-location>指明标签库存在的物理路径,当然,和配置文件一样,也是相对路径。
5、 设置welcome文件列表(可选步骤)
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
6、 设置错误处理(可选步骤),通常的http访问异常包含404 Not Found和500 Internal Error,为了提供给用户更为友好的显示,可以做如下配置:
<web-app>
<error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
</web-app>
通过如上配置,当用户访问应用中不存在的页面时,将会将用户导向到/common/404.jsp页面。同样地,当出现异常错误时,将会把/common/500.jsp显示给用户。
7、 最后,一个完整的web.xml示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>storefront</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>storefront</servlet-name>
<url-pattern>/action/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/common/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
1、 到此为止,Struts的开发环境安装算是告一段落。
本文地址:http://com.8s8s.com/it/it16554.htm