一:Action的配置详解:
下面是一个Struts中一个空的Struts.xml的配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
<include file="example.xml"/>
<!-- Add packages here -->
</struts>
1, 包配置
<package name="default" namespace="/" extends="struts-default"> </package>
2, name 表示包的名字
3, namespace="/" ;表示访问该包下面的路劲时必须要在路径前加 /xx
也可以使用namespace="/space" 访问时加上/space/xx
<package name="default" extends="struts-default">
<action name="hello" >
<result >/login.html</result>
</action>
</package>
当namespace不存在时如何访问;
xxx/aaa/bbb/hello
aa/hello 都可以访问到
4,extends="struts-default"继承默认的标签
5,Action中的调用;
a; 普通调用
<action name="hello" >
<result >/login.html</result>
</action>
b,动态调用;
2,3可能会包错:
There is no Action mapped for namespace [/user] and action name [user!add] associated with context p
解决办法;
将DMI打开 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
6,include模块
Struts.xml可以相互引入
<include file="login.xml" /> login,xml的配置与Struts.xml相同
7,Struts中Session会话的使用例子;
保存会话
@Override
public String execute() throws Exception {
System.out.println(">"+username+"<>"+userpwd+"<");
if ("scott".equals(username) && "tiger".equals(userpwd)) {
HttpServletRequest request=(HttpServletRequest) ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("name", username);
return "hello";
} else {
return "hellow";
}
}
获取会话
<%
String name=(String)session.getAttribute("name");
%>
欢迎<a href="login.html"><%=name %>登陆</a>
9,Result的使用
<result type="redirectAction"> 重新请求action
Chain 处理action连接
Dispatcher 处理jsp页面
Redirect重定向
本文详细介绍了Struts框架的配置文件结构,包括包配置、名称、命名空间、继承默认标签、Action调用方式、DMI配置、include模块、Session会话使用、Result类型应用等核心内容,并提供了实例代码演示。
1万+

被折叠的 条评论
为什么被折叠?



