public class GlobalForwards {
private Map<String, ActionForward> globalForwards = new HashMap<String,
ActionForward>();
public class GlobalForwards {
private Map<String, ActionForward> globalForwards = new HashMap<String,
ActionForward>();
public void addActionForward(ActionForward forward) {
globalForwards.put(forward.getName(), forward);
}
public ActionForward findActionForward(String name) {
return globalForwards.get(name);
}
public class StrutsConfig {
private ActionMappings mappings;
private GlobalForwards globalForwards;
public ActionMappings getMappings() {
return mappings;
}
public void setMappings(ActionMappings mappings) {
this.mappings = mappings;
}
public GlobalForwards getGlobalForwards() {
return globalForwards;
}
public void setGlobalForwards(GlobalForwards globalForwards) {
this.globalForwards = globalForwards;
}
rule.xml
<?xml version='1.0' encoding='UTF-8'?>
<!--
这个xml文件是由commons-digester定义用于告诉digester组件
自定义的配置文件和配置对象之间的关系,commons-digester组件了解了这 个关系后就可以将配置文件中的信息转换为配置对象
-->
<digester-rules>
<pattern value="struts-config">
<!-- GlobalForwards -->
<pattern value="global-forwards">
<object-create-rule classname="org.mystruts.config.GlobalForwards" />
<set-next-rule methodname="setGlobalForwards" />
<pattern value="forward">
<object-create-rule classname="org.mystruts.config.ActionForward" />
<set-next-rule methodname="addActionForward" />
<set-properties-rule />
</pattern>
</pattern>
<!-- ActionMappings -->
<pattern value="action-mappings">
<object-create-rule classname="org.mystruts.config.ActionMappings" />
<set-next-rule methodname="setMappings" />
<pattern value="action">
<!--每碰到一个action元素,就创建指定类的对象-->
<object-create-rule classname="org.mystruts.config.ActionMapping" />
<!--
对象创建后,调用指定的方法,
将其加入它上一级元素所对应的对象
-->
<set-next-rule methodname="addActionMapping" />
<!--
将action元素的各个属性按照相同的名称
赋值给刚刚创建的ActionMapping对象
-->
<set-properties-rule />
<pattern value="forward">
<object-create-rule classname="org.mystruts.config.ActionForward" />
<set-next-rule methodname="addActionForward" />
<set-properties-rule />
</pattern>
</pattern>
</pattern>
</pattern>
/**
* 完成配置对象的初始化
*/
private void initConfig() throws Exception {
Digester digester = DigesterLoader.createDigester(ActionServlet.class
.getClassLoader().getResource("org/mystruts/action/rule.xml"));
config = new StrutsConfig();
digester.push(config);
try {
digester.parse(ActionServlet.class.getClassLoader()
.getResourceAsStream("mystruts.xml"));
mappings = config.getMappings();
globalForwards = config.getGlobalForwards();
} catch (IOException e) {
throw new IOException(e.getMessage());
} catch (SAXException e) {
throw new SAXException(e.getMessage());
}
…
ActionForward forward =
mapping.findActionForward(result);
if (forward == null) {
// 若在action元素里面没有对应的forward存在,那么就去全局的global-forwards
// 寻找,即是,action里的forward优先于global-forwards里的forward
forward = globalForwards.findActionForward(result);
if (forward == null) {
throw new ServletException("forward not found");
}
上一篇:自己动手写struts1
下一篇:自己动手写struts2-2
1. 概述官网https://struts.apache.org/下载特点在用户请求,模块化处理和页面展现方面发挥
本文可作为<<自己动手写struts–构建基于MVC的Web开发框架>>一书的读书笔记。
在上一节,我们自己写的web框架,只能运行显示一个HelloWorld。现在我们对其进行一次加工
这篇文章 可作为北京尚学堂 hibernate的学习笔记再学习hibernate之前 得有一点反射的基础知识
&n
写插件刚开始以为很简单,后来发现不过就是一些面向对象和面向过程的堆积。复杂的写的思路。 以下是我学习写查看看过3个不错的链接。 http://api.jquery.com/jQuery.extend/ &
举报文章
请选择举报类型
补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M