web.xm和taglib
作servlet映射是个讨厌的工作,当你接收别的项目的时候,这个项目的servlet怎么用可能比较麻烦,可能当时web.xml的映射找不到了,这时怎么办呢?
看这个文件
package com.xdocletbook.blog.servlet; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Level; import org.apache.log4j.Logger; /** * @web.servlet * name="HomePage" * @web.servlet-init-param * name="LogLevel" * value="{LOG_LEVEL}" * @web.servlet-mapping * url-pattern="/home" * * @web.security-role *role-name="{OwnerRole}" * @web.security-role-ref *role-name="blogowner" *role-link="{OwnerRole}" */ public class HomePageServlet extends HttpServlet { private static Logger LOGGER = Logger.getLogger(HomePageServlet.class); public void init() throws ServletException { String logLevel = getInitParameter("LogLevel"); if (logLevel != null && logLevel.length() > 0) { LOGGER.setLevel(Level.toLevel(logLevel)); } } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LOGGER.debug("Displaying home page"); request.getRequestDispatcher("jsp/home.jsp").forward(request, response); } }
然后配置build.xml增加如下任务
<target name="generate-web"> <taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask" classpathref="xdoclet.classpath"/> <!-- Generate servlet and JSP Tag "stuff" --> <webdoclet destdir="gen" mergeDir="merge"> <fileset dir="src"> <include name="**/*.java" /> </fileset> <deploymentdescriptor destdir="web" distributable="false" /> </webdoclet> </target>
然后运行ant generate-web
结果就是这样一个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 > <!-- To use non XDoclet filters, create a filters.xml file that contains the additional filters (eg Sitemesh) and place it in your project's merge dir.Don't include filter-mappings in this file, include them in a file called filter-mappings.xml and put that in the same directory. --> <!-- To use non XDoclet filter-mappings, create a filter-mappings.xml file that contains the additional filter-mappings and place it in your project's merge dir. --> <!-- To use non XDoclet listeners, create a listeners.xml file that contains the additional listeners and place it in your project's merge dir. --> <servlet> <servlet-name>StrutsActionServlet</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> </servlet> <servlet> <servlet-name>HomePage</servlet-name> <servlet-class>com.xdocletbook.blog.servlet.HomePageServlet</servlet-class> <init-param> <param-name>LogLevel</param-name> <param-value>1</param-value> </init-param> <security-role-ref> <role-name>blogowner</role-name> <role-link>aOwner</role-link> </security-role-ref> </servlet> <servlet-mapping> <servlet-name>StrutsActionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>HomePage</servlet-name> <url-pattern>/home</url-pattern> </servlet-mapping> <!-- To specify mime mappings, create a file named mime-mappings.xml, put it in your project's mergedir. Organize mime-mappings.xml following this DTD slice: <!ELEMENT mime-mapping (extension, mime-type)> --> <!-- To specify error pages, create a file named error-pages.xml, put it in your project's mergedir. Organize error-pages.xml following this DTD slice: <!ELEMENT error-page ((error-code | exception-type), location)> --> <!-- To add taglibs by xml, create a file called taglibs.xml and place it in your merge dir. --> <!-- To set up security settings for your web app, create a file named web-security.xml, put it in your project's mergedir. Organize web-security.xml following this DTD slice: <!ELEMENT security-constraint (display-name?, web-resource-collection+, auth-constraint?, user-data-constraint?)> <!ELEMENT web-resource-collection (web-resource-name, description?, url-pattern*, http-method*)> <!ELEMENT web-resource-name (#PCDATA)> <!ELEMENT url-pattern (#PCDATA)> <!ELEMENT http-method (#PCDATA)> <!ELEMENT user-data-constraint (description?, transport-guarantee)> <!ELEMENT transport-guarantee (#PCDATA)> <!ELEMENT login-config (auth-method?, realm-name?, form-login-config?)> <!ELEMENT auth-method (#PCDATA)> <!ELEMENT realm-name (#PCDATA)> <!ELEMENT form-login-config (form-login-page, form-error-page)> <!ELEMENT form-login-page (#PCDATA)> <!ELEMENT form-error-page (#PCDATA)> --> <security-role> <role-name>aOwner</role-name> </security-role> </web-app>