每个网站都需要一个欢迎页面或默认页面作为入口点。 这是在Struts中配置欢迎页面的3种方法。
下载此Struts欢迎文件示例– Struts-Welcome-File-Example.zip
1. index.jsp
最简单的方法是创建一个“ index.jsp ”页面,并将其与WEB-INF文件夹,项目根文件夹放置在同一级别。
访问项目根目录
http://localhost:8080/StrutsExample/
内部默认为index.jsp。
http://localhost:8080/StrutsExample/index.jsp
2. web.xml欢迎文件
在web.xml文件中声明一个欢迎文件。
<welcome-file-list>
<welcome-file>
/pages/Welcome.jsp
</welcome-file>
</welcome-file-list>
访问项目根目录
http://localhost:8080/StrutsExample/
它将在内部重定向到welcome.jsp文件。
http://localhost:8080/StrutsExample/pages/Welcome.jsp
web.xml
<!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>
<display-name>Maven Struts Examples</display-name>
<servlet>
<servlet-name>action</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
/pages/Welcome.jsp
</welcome-file>
</welcome-file-list>
</web-app>
3. JSP转发
如方法1所述,创建一个“ index.jsp ”文件,并定义一个JSP转发标记以将其重定向到另一个Struts操作。
index.jsp
声明一个/ Welcome Web路径,使用ForwardAction类型将其转发到另一个JSP文件。
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action
path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/Welcome.jsp"/>
</action-mappings>
</struts-config>
翻译自: https://mkyong.com/struts/configure-a-welcome-page-in-struts/