创建 Web 应用程序元素
最后修改时间:2023 年 6 月 26 日Web 应用程序的元素和部署设置在 Web 模块部署描述符web.xml中定义,该描述符是在您为模块启用 Web 应用程序支持时自动创建的。
为了与用户界面保持一致,模块部署描述符也称为程序集描述符。
您可以使用编辑器并使用 IntelliJ IDEA 编码辅助来编辑源代码。
定义 Web 应用程序元素的模板
IntelliJ IDEA 不提供用于创建 servlet、侦听器和过滤器的默认模板。但是,您可以自己定义这些模板。
作为 Web 应用程序一部分的Servlet是通过web.xml<servlet>
Web 应用程序部署描述符中的和<servlet-name>
元素创建和配置的。
按打开 IDE 设置,然后选择编辑器 | 文件和代码模板。CtrlAlt0S
在“文件”选项卡上,单击“创建新文件模板”。
命名新模板
Servlet
并指定Servlet
为文件名。确保扩展名是java
.将以下代码粘贴为模板主体:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet(name = "${Class_Name}", value = "/${Class_Name}") public class ${Class_Name} extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
单击“应用”保存更改并关闭对话框。
Servlet元素定义 servlet 的名称并指定实现它的编译类。或者,您可以指定 JSP,而不是指定 servlet 类。
Servlet 元素还包含初始化属性的定义。
请注意,您可以使用编辑器中的注释来映射将 servlet 与调用 servlet 的 URL 集关联起来的 URL 模式。
A listener receives notifications on any events related to the Web application, such as deployment/undeployment of the Web application, activation/deactivation of an HTTP session, as well as on adding, removing, and replacing attributes of the application/session.
Press CtrlAlt0S to open the IDE settings and then select Editor | File and Code Templates.
On the Files tab, click to create a new file template.
Name the new template
Listener
and specifyListener
as the file name. Make sure that the extension isjava
.Paste the following code as the template body:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") import javax.servlet.*; import javax.servlet.http.*; public class ${Class_Name} implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener { public ${Class_Name}() {} @Override public void contextInitialized(ServletContextEvent sce) { /* This method is called when the servlet context is initialized(when the Web application is deployed). */ } @Override public void contextDestroyed(ServletContextEvent sce) { /* This method is called when the servlet Context is undeployed or Application Server shuts down. */ } @Override public void sessionCreated(HttpSessionEvent se) { /* Session is created. */ } @Override public void sessionDestroyed(HttpSessionEvent se) { /* Session is destroyed. */ } @Override public void attributeAdded(HttpSessionBindingEvent sbe) { /* This method is called when an attribute is added to a session. */ } @Override public void attributeRemoved(HttpSessionBindingEvent sbe) { /* This method is called when an attribute is removed from a session. */ } @Override public void attributeReplaced(HttpSessionBindingEvent sbe) { /* This method is called when an attribute is replaced in a session. */ } }
Click Apply to save the changes and close the dialog.
Press CtrlAlt0S to open the IDE settings and then select Editor | File and Code Templates.
On the Files tab, click to create a new file template.
Name the new template
Filter
and specifyFilter
as the file name. Make sure that the extension isjava
.Paste the following code as the template body:
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end #parse("File Header.java") import javax.servlet.*; import javax.servlet.annotation.*; @WebFilter(filterName = "${Class_Name}") public class ${Class_Name} implements Filter { public void init(FilterConfig config) throws ServletException { } public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { chain.doFilter(request, response); }
Click Apply to save the changes and close the dialog.
添加一个元素
在添加新元素之前,请确保已为其创建文件模板。
在“项目”工具窗口(或“视图”|“工具窗口”|“项目”)中,右键单击位于Web模块中的包,然后选择Servlet、Filter或Listener。Alt01
为新元素命名并单击“确定”。
感谢您的反馈意见!