您的位置:首页 > 编程语言 > Java开发

Struts02---实现struts2的三种方式

2017-06-30 09:51 204 查看

01.创建普通类

/**
* 01.普通类
*   写一个execute()  返回String类型值
*
*/
public class HelloAction01 {

public  String  execute(){
System.out.println("HelloAction01");
return  "success";
}

}

找到我们需要的xml文件

在 struts-core中的根目录下面,删除不需要的信息!

<?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>
<package name="default"  namespace="/" extends="struts-default">
<action name="hello1"  class="cn.bdqn.action.HelloAction01">
<result  name="success">/success.jsp</result>
</action>
</package>
</struts>

在浏览器中输入   项目名/hello1 (action节点中的name属性值)

02.创建一个类实现一个Action接口重写execute()

/**
* 02.实现Action接口
*   重写execute()  返回String类型值
*
*   Action接口中一共定义了5个静态常量和一个execute()
*     01.SUCCESS
*     02.INPUT
*     03.ERROR
*     04.LOGIN
*     05.NONE
*    这5个静态常量对应的值 都是  小写的字符串
*
*/
public class HelloAction02  implements Action{

public  String  execute(){
System.out.println("实现了Action接口的HelloAction02");
return  SUCCESS;
}

}

03.常用的方式  继承ActionSupport

/**
*03. 继承ActionSupport  我们常用的方式
*         重写execute
*/
public class HelloAction03  extends ActionSupport{

public  String  execute(){
System.out.println("继承了了ActionSupport类的HelloAction03");
return  SUCCESS;
}

}

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>

<!--
package:
name:包名,唯一的!自定义的!必选项!用于其他类的继承!
namespace:命名空间,可选项! 如果不写  默认是 "/" ,从项目的跟路径开始
extends:继承   需要继承的包名!
为什么要继承struts-default!
因为 struts-default包中有struts2框架需要的拦截器!
-->
<package name="default"  namespace="/" extends="struts-default">
<!--
action:
name:用户访问的路径,但是必须要加上 namespace!前提匹配了namespace才能找到name!
class:需要执行代码的全类名!
method:默认就是寻找execute(),
如果指定了方法名称!则去类中查询指定的方法然后执行!
-->
<action name="hello1"  class="cn.bdqn.action.HelloAction01" method="execute">
<!--
result:
name:后台指定方法的返回值,默认值就是success!
type:默认值是dispatcher!转发到jsp
-->
<result  name="success" type="">/success.jsp</result>
</action>
<action name="hello2"  class="cn.bdqn.action.HelloAction02">
<result>/success.jsp</result>
</action>
<action name="hello3"  class="cn.bdqn.action.HelloAction03">
<result>/success.jsp</result>
</action>
</package>
</struts>

struts-default.xml文件中的部分解析

<!-- struts.xml文件的 头部信息-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!--
package:包
name:唯一的标识!用于继承!我们写的struts.xml就是 extends struts-default
abstract="true":设置成了抽象包!不能定义action标签!
-->
<package name="struts-default" abstract="true">
<!--
result-types:定义了返回(结果)类型!
-->
<result-types>
<!-- 转发到action -->
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<!-- 转发到jsp -->
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<!--模版  -->
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<!--  http请求或者相应头-->
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<!-- 重定向jsp -->
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<!--重定向action -->
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
</result-types>
<!--
interceptors:定义了struts2所有的默认拦截器
name:拦截器的名称
class:对应全类名
-->
<interceptors>
<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
<interceptor name="autowiring" class="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor"/>
<interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError" class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/>
<interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
<interceptor name="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
<interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
<interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
<interceptor name="execAndWait" class="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor"/>
<interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>
<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
<interceptor name="i18n" class="com.opensymphony.xwork2.interceptor.I18nInterceptor"/>
<interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/>
<interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
<interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/>
<interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/>
<interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
<interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/>
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>
<interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
<interceptor name="token" class="org.apache.struts2.interceptor.TokenInterceptor"/>
<interceptor name="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
<interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
<interceptor name="workflow" class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
<interceptor name="store" class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
<interceptor name="checkbox" class="org.apache.struts2.interceptor.CheckboxInterceptor" />
<interceptor name="datetime" class="org.apache.struts2.interceptor.DateTextFieldInterceptor" />
<interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
<interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" />
<interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" />
<interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" />

<!--
interceptor-stack:拦截器栈
拦截器栈存放了 很多已经定义好的拦截器
struts2是通过调用拦截器栈,从而调用拦截器的!
拦截器栈中的所有拦截器的执行都是有顺序的! 就是按照存放的先后顺序执行!
-->
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
<interceptor-ref name="deprecation"/>
</interceptor-stack>

</interceptors>
<!-- 默认执行的拦截器栈 -->
<default-interceptor-ref name="defaultStack"/>
<!-- 如果在action中 没有定义对应的  执行类!  默认会执行ActionSupport这个类 -->
<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
</package>

</struts>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: