Configure a welcome page in Struts

Every website need a welcome or default page as an entry point. Here’s 3 ways to configure a welcome page in Struts.

1. index.jsp

The simplest way is create a “index.jsp” page and put it same level with the WEB-INF folder, project root folder.

Access the project root

http://localhost:8080/StrutsExample/

It will default to index.jsp internally.

http://localhost:8080/StrutsExample/index.jsp

2. web.xml welcome file

Declare a welcome-file in web.xml file.

  <welcome-file-list>
<welcome-file>
/pages/Welcome.jsp
</welcome-file>
</welcome-file-list>

Access the project root

http://localhost:8080/StrutsExample/

It will redirect to welcome.jsp file internally.

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 Forward

Create a “index.jsp” file as stated in method 1, and define a JSP forward tag to redirect it to another Struts action.

index.jsp

Declare a /Welcome web path, with a ForwardAction type to forward it to another JSP file.

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>
上一篇:SolrCloud7.4(Jetty容器)+mysql oracle 部署与应用


下一篇:markdown常用语法使用笔记+使用技巧(持续更新......)