问题,配置文件中有哪些标签及标签的各个作用,Action的使用方法,自动更新配置文件的设置,动作名称后缀名的设置,各个result中type属性值。
struts2:到底做了什么呢?
1、编写struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts><!--这是Struts2配置文件的根元素-->
<package name="itcast" namespace="/test" extends="struts-default">
<!--
pageckage:方便管理动作元素
name:必须有。包的名称,配置文件中必须保证<span style="color:#ff0000;">唯一</span>。
namespace:该包的名称空间,一般是以"/"开头(可以不写这个namespace,但是建议写)
extends:集成的父包的名称。struts-default名称的包是struts2框架已经命名好的一个包。(在struts2-core.jar中有一个struts-default.xml中,里面是核心拦截器的实现)
abstract:是否是抽象包。没有任何action元素的包就是抽象包(java类)--> <span style="white-space:pre"> </span><action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="sayHello">
<!--
<span style="white-space:pre"> </span>action:代表一个请求动作
<span style="white-space:pre"> </span>name:同包中必须<span style="color:#ff6666;">唯一</span>。动作的名称
<span style="white-space:pre"> </span>class:负责处理的JavaBean的类全名
<span style="white-space:pre"> </span>method:JavaBean中的对应处理方法。(动作方法:特点是,public String 方法名(){},不写menthod默认走execute方法(该方法必须继承ActionSupport类不然就没有这个execute方法))
-->
<span style="white-space:pre"> </span><result name="success">/1.jsp</result>
<span style="white-space:pre"> </span><!--
<span style="white-space:pre"> </span>result:结果类型
name:动作方法返回的字符串(默认success)
主体内容:View的具体地址。
-->
</action>
</package>
</struts>2、根据配置文件,创建需要的javabean和对应的动作方法,在动作方法中完成你的逻辑调用。
package cn.itcast.action;
public class HelloWorldAction implements Serializable {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String sayHello(){
message = "helloworld by struts2";
return "success";
}
}3、编写View,显示结果
通过类实例化在拦截器中跳转的页面可以得到属性值 ${message}
4、访问helloworld动作的方式:http://localhost:8080/struts2day01/test/helloworld 应用名称/包的名称空间/动作的名称
http://localhost:8080/struts2day01/test/a/b/c/helloworld
/test/a/b/c:名称空间
helloworld:动作名称
搜索顺序:名称空间(依次查找,直到最后查找到,或者查找不到)
/test/a/b/c 没有helloworld
/test/a/b 没有helloworld
/test/a 没有helloworld
/test 有了,调用执行
五、Struts2配置文件的详解
1、struts.xml配置文件编写是没有提示的问题?
方法一:上网即可
方法二:
1、拷贝http://struts.apache.org/dtds/struts-2.1.7.dtd地址
2、Eclipse的window、preferences,搜索XML Catelog
3、点击add按钮
Location:dtd文件的路径
Key Type:URI
Key:http://struts.apache.org/dtds/struts-2.1.7.dtd
2、Struts配置文件中的各种默认值。
action:(如果不写action中的class及method,将走下面默认的实例化类及execute方法)class:默认值是com.opensymphony.xwork2.ActionSupport(这个类必须要在Javabean中继承)
常量: SUCCESS success
NONE none
ERROR error
INPUT input
LOGIN login
method:默认值是public String execute(){}
实际开发中:自己编写的动作类一般情况下继承com.opensymphony.xwork2.ActionSupport
result:
type:转到目的地的方式。默认值是转发,名称是dispatcher
(注:type的取值是定义好的,不是瞎写的。在struts-default.xml中的package中有定义)
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
dispatcher:普通的转发到某个页面(这个是默认值,我们一般用redirect)
chain:普通的抓发到某个动作名称(*这个很重要)
redirect:重定向到一个页面(*这个很重要,我们一般定义result的type用的就是这个redirect)
redirectAction:重定向到一个动作名称
plainText:以纯文本的形式输出JSP内容(jsp的页面以文本输出)
1 redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。
2 redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。
3 chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。
result元素的写法:
chain的用法
方式一:<result type="chain" name="success">a2</result>
<!--这个名字不用谢namespace名字,它直接写名字-->
方式二:<result type="chain" name="success">
<param name="namespace">/namespace1</param>
<param name="actionName">a2</param>
</result>
<!--name对应的chain的处理器中的setActionName()方法-->
注意:如果要转向的是在另外一个名称空间的动作,那么只能使用方式二
如:
<span style="white-space:pre"> </span><package name="p1" namespace="/namespace1" extends="struts-default">
<action name="a2">
<result type="dispatcher" name="success">/3.jsp</result>
</action>
</package>
<package name="p2" namespace="/namespace2" extends="struts-default">
<action name="a1">
<result type="chain" name="success">
<param name="namespace">/namespace1</param>
<param name="actionName">a2</param>
</result>
</action>
</package>注意:如果要重定向的是在另外一个名称空间的动作,那么只能使用方式二(重定向的作用就不用我说了)
<package name="itcast" namespace="/test" extends="struts-default">
<action name="hhw1">
<result type="redirectAction" name="success">
<param name="namespace">/test3</param>
<param name="actionName">a3</param>
</result>
</action>
</package>
<package name="p3" namespace="/test3" extends="struts-default">
<action name="a3">
<result type="redirectAction" name="success" >a4</result>
</action>
<action name="a4">
<result type="dispatcher" name="success">/3.jsp</result>
</action>
</package>
<span style="font-family:Microsoft YaHei;font-size:18px;"> <!--动态设置javabean中的属性值,转发后的页面就可以得到属性值-->
<action name="a4" class="com.itcast.web.domain.javabean">
<param name="name">I'm Mr_Li13</param>
<!--动态得到javabean中的属性值(这里使用的name值就是Javabean的属性)-->
<action name="a5" class="com.itcast.web.domain.javabean" method="sayHello" >
<result type="redirect" name="success">/4.jsp? msg=${name}</result>
</action>
<span style="color:#ff0000;"><!--*重点用户指定多个请求后缀,有多个逗号隔开。配置文件的动态更新-->
<constant name="struts.action.extension" value="action,,do"></constant>
<constant name="struts.devMode" value="true"></constant></span></span>
3、开发中配置文件的更改,在访问时让框架自动重新加载:
struts.devMode = false(default.properties)
<span style="font-size:18px;"><!--利用strutx.xml中的constant元素来覆盖掉default.properties默认行为-->
<struts>
<constant name="struts.devMode" value="true"></constant>
</struts></span>这里的两个模式的设置,可以按照这个设置,也可以按照下图介绍,去找相关的知识。
本文介绍了Struts2框架中的配置文件,包括struts.xml的编写,Action的使用方式,以及配置文件的自动更新设置。详细讲解了配置文件中的redirect、redirect-action和chain等result类型,并给出了result元素和chain的示例。通过设置struts.devMode可以在开发过程中实现配置文件的动态重载。
1万+

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



