Struts2中配置默认Action

1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作;
2.配置方法:
    在struts.xml文件中的<package>下添加如下内容:
        <default-action-ref name="index"></default-action-ref>
    其中index为默认Action的name属性值;
3.配置默认Action后,相应的namespace下不存在要访问的Action时,自动跳转到默认Action处理。

实例:

web.xml:

01. <?xml version="1.0" encoding="UTF-8"?>
02. <web-app version="2.5" 
03.     xmlns="http://java.sun.com/xml/ns/javaee" 
04.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
05.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ;
07.   <welcome-file-list>
08.     <welcome-file>hello.jsp</welcome-file>
09.   </welcome-file-list>
10.   <filter>
11.     <filter-name>struts2</filter-name>
12.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13.   </filter>
14.   <filter-mapping>
15.     <filter-name>struts2</filter-name>
16.     <url-pattern>/*</url-pattern>
17.   </filter-mapping>
18. </web-app>



struts.xml:

01. <?xml version="1.0" encoding="UTF-8" ?>
02. <!DOCTYPE struts PUBLIC
03.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
05.   
06. <struts>
07.     <!-- 
08.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
09.     <constant name="struts.devMode" value="false" />
10.   
11.     <include file="example.xml"/>
12.   
13.   
14.   
15.     <package name="default" namespace="/" extends="struts-default">
16.         <default-action-ref name="index" />
17.         <action name="index">
18.             <result type="redirectAction">
19.                 <param name="actionName">HelloWorld</param>
20.                 <param name="namespace">/example</param>
21.             </result>
22.         </action>
23.     </package>
24.      -->
25.   
27.     <constant name="struts.devMode" value="true" />
28.     <constant name="struts.i18n.encoding" value="GBK"></constant>
26.     <!-- 注意添加在这里-->
29.      <package name="user" namespace="/" extends="struts-default">
30.          <default-action-ref name="index"></default-action-ref>
31.         <action name="index">
32.             <result>/index.jsp</result>
33.         </action>
34.     </package>
35. </struts>



index.jsp:

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
       result结果类型<br>
       <ol>
          <li><a href="r/r1">dispatcher</a></li>
          <li><a href="r/r2">redirect</a></li>
          <li><a href="r/r3">chain</a></li>
          <li><a href="r/r4">redirectAction</a></li>
       </ol>
     
  </body>
</html>

截图:
 
 
好了!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值