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>

Struts02---实现struts2的三种方式的更多相关文章

  1. Struts2方法调用的三种方式

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  2. Struts2获取Session的三种方式

    1.Map<String,Object> session =  ActionContext.getContext().getSession(); session.put("cod ...

  3. 【深入Struts2】获取ServletAPI的三种方式

    一:获取servletAPI的三种方法 在传统的Web开发中,经常会用到Servlet API中的HttpServletRequest.HttpSession和ServletContext.Strut ...

  4. 【Struts2】Struts2获取session的三种方式

    1.Map<String,Object> map =  ActionContext.getContext().getSession(); 2.HttpSession session = S ...

  5. Struts2方法调用的三种方式(有新的!调用方法的说明)

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="heroAction" class="com.ABC ...

  6. Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)

    一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  7. JavaWeb_(Struts2框架)Struts创建Action的三种方式

    此系列博文基于同一个项目已上传至github 传送门 JavaWeb_(Struts2框架)Struts创建Action的三种方式 传送门 JavaWeb_(Struts2框架)struts.xml核 ...

  8. struts2 action 页面与action参数的传递的三种方式

    第一种: 初始页面: <form action="LoginAction.action" method="post"> 用户名:<input ...

  9. struts2简单入门-参数传递的三种方式

    三种方式的简单说明 属性传递 把参数定义为属性提供get/set方法. 使用情况 参数少,不需要共享. 演示代码 public class LoginAction extends ActionSupp ...

随机推荐

  1. 生信笔记-mooc【武大】

    .DNA拓扑学 在拓扑结构的限制下,DNA进行复制等过程.还有连环数=扭转数+缠绕数. 2.拓扑异构酶 DNA变性破坏了两条链之间碱基形成的氢键.和拓扑异构酶是不同的. 3.RNA的组成和结构特点 R ...

  2. Ubuntu学习笔记2-网络部分

    Ubuntu server配置IP地址 第一种方法:常规方法 1.登录Ubuntu Server,然后通过“sudo -s” 切换到root用户. 2.输入“cd  /etc/network/”,回车 ...

  3. python代码结构

    1. 使用#单行注释,使用'''...'''多行注释 2. 使用连接符\来把一行过长的代码分为多行 3. 用缩进来控制代码块,推荐使用PEP8缩进风格,即四个空格 4. if ...: elif... ...

  4. Java架构搜集

    1. 2.

  5. 经典iOS第三方库源码分析 - YYModel

    YYModel介绍 YYModel是一个针对iOS/OSX平台的高性能的Model解析库,是属于YYKit的一个组件,创建是ibireme. 其实在YYModel出现之前,已经有非常多的Model解析 ...

  6. Bootstrap总结二

    参考我的博客:http://www.isedwardtang.com/2017/09/01/bootstrap-primer-2/

  7. 【BZOJ4671】(斯特林反演)

    题目 [BZOJ4671]异或图 很有意思的题 做法 直接处理显然很难,我们考虑范围扩大以求容斥或反演这类的帮助 \(f_i\)表示至少有\(i\)个联通块的方案,形如设立\(i\)个联通块轮廓,联通 ...

  8. Java:执行jar文件命令

    Java:执行jar文件命令 执行jar文件命令: java -jar test.jar win7系统切换目录命令: cd /d d:/test

  9. oracle中INSTR函数的用法

    今天有个同学问我这个INSTR函数,我也不太清楚就上网查了查做一个小小的记录吧 INSTR(C1,C2,I,J) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置; C1 被搜索的字符串 C2  ...

  10. 【bzoj4806~bzoj4808】炮车马后——象棋四连击

    bzoj4806——炮 题目传送门:bzoj4806 这种题一看就是dp...我们可以设$ f[i][j][k] $表示处理到第$ i $行,有$ j $列没放炮,$ k $列只放了一个炮.接着分情况 ...