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. C++对象模型那点事儿(布局篇)

    1 前言 在C++中类的数据成员有两种:static和nonstatic.类的函数成员由三种:static,nonstatic和virtual. 上篇我们尽量说一些宏观上的东西,数据成员与函数成员在类 ...

  2. spawn 和 exec 的区别(转载)

    众所周知,Node.js在child_process模块中提供了spawn和exec这两个方法,用来开启子进程执行指定程序.这两个方法虽然目的一样,但是既然Node.js为我们提供了两个方法,那它们之 ...

  3. 为Eclipse指定JVM

    运行eclipse时,报如下错误时,可以通过修改配置文件eclipse.ini来解决. Version 1.4.1_01 of the JVM is not suitable for this pro ...

  4. oracle ORA-01704: string literal too long

    导出数据时,在SQL拼接处,提示 oracle ORA-01704: string literal too long sql:  WITH already_in AS (SELECT distinct ...

  5. java项目地址和服务器地址区分

    项目地址String filePath = request.getSession().getServletContext().getRealPath("/") + "up ...

  6. org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类

    <bean id="investorQueryConfigurer" class="org.springframework.beans.factory.config ...

  7. POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)

    题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...

  8. ruby安装神器rvm,你造吗?

    以前的一篇文章介绍过如何安装ruby,叫做:如何安装/更新ruby,安装cocoapods,为开发做好准备!(2016年12月07日更新内容) 文章中讲到的方法依然可行,但是该方法繁琐并且可能会出现各 ...

  9. redis 笔记05 Sentinel、集群

    Sentinel 1. Sentinel只是一个运行在特殊模式下的Redis服务器,它使用了和普通模式不同的命令表,所以Sentinel模式能够使用的命令和普通的Redis服务器能够使用的命令不同. ...

  10. 微信小程序:其中wxml和wxss的样式说明

    微信小程序:其中wxml和wxss的样式说明 一.简介 对于css不熟悉的Android程序员来说,开发微信小程序面临的一个比较困难的问题就是界面的排版了.微信小程序的排版就跟wxml和wxss有关了 ...