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>

转载于:https://www.cnblogs.com/ghgyj/p/4766260.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值