Struts2中配置默认Action
2015-10-10 21:05
351 查看
1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作;
2.配置方法:
在struts.xml文件中的<package>下添加如下内容:
<default-action-refname="index"></default-action-ref>
其中index为默认Action的name属性值;
3.配置默认Action后,相应的namespace下不存在要访问的Action时,自动跳转到默认Action处理。
实例:
web.xml:
struts.xml:
index.jsp:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'index.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
result结果类型<br>
<ol>
<li><ahref="r/r1">dispatcher</a></li>
<li><ahref="r/r2">redirect</a></li>
<li><ahref="r/r3">chain</a></li>
<li><ahref="r/r4">redirectAction</a></li>
</ol>
</body>
</html>
截图:
好了!
2.配置方法:
在struts.xml文件中的<package>下添加如下内容:
<default-action-refname="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;
06.
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
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.
<!DOCTYPEstrutsPUBLIC
03.
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
04.
"http://struts.apache.org/dtds/struts-2.0.dtd">
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:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'index.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
result结果类型<br>
<ol>
<li><ahref="r/r1">dispatcher</a></li>
<li><ahref="r/r2">redirect</a></li>
<li><ahref="r/r3">chain</a></li>
<li><ahref="r/r4">redirectAction</a></li>
</ol>
</body>
</html>
截图:
好了!
相关文章推荐
- struts 2配置文件的包继承
- JAVA项目出现红色叹号
- struts 2配置两种的结果
- 内部排序(java实现)
- Spring Boot实践应用开发(7)
- Java语言的基本结构
- Java基础知识强化之集合框架笔记74:各种集合常见功能 和 遍历方式总结
- java_Collection_List
- struts 2配置默认的Action
- Java基础知识强化之集合框架笔记73:如何选择使用哪种集合
- Java声明和定义
- 一道排序笔试题,在o(n)时间内对一个数组进行排序
- Java
- java md5加密字符
- 【第4节】Java代码实现栈--小例子--分隔符匹配
- 【第4节】Java代码实现栈--小例子--单词逆序
- 【第4节】栈的Java代码实现,【数组形式存储】
- Java开发工具
- Java语言基础(数组概述和定义格式说明)
- Java语言基础(常量的概述和使用)