虽然Struts 2.x的Action在技术上不需要实现任何接口或继承任何类型,但是,大多情况下我们都会出于方便的原因,使Action类继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法以实现相关功能。

本文是一个HelloWorld级别的action示范程序。

1. 修改web.xml

    <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

加入上述代码的作用是添加过滤器,拦截所有请求,将由struts来处理;具体由下面的struts.xml配置决定。

2. 创建struts.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> <constant name="struts.action.extension" value="action"></constant>
</struts>

注意:

  • 此文件存放于WEB-INF/classes目录下面。
  • 最后一行为我本机环境加入的,含义是只拦截后缀为action的请求。

3. 在struts.xml配置文件中注册Action和result

    <package name="myStruts" extends="struts-default">
<action name="hello" class="com.clzhang.ssh.demo.action.HelloAction">
<result>/ssh/demo1/hello.jsp</result>
</action>
</package>

action的name决定调用时的名称;class为实现类名;result的值为action执行完成后转向何页面。如果没有为result指定name名称,默认值为success

4. 创建action处理类

package com.clzhang.ssh.demo.action;

import java.util.Date;
import java.text.SimpleDateFormat; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport {
private String message; public String getMessage() {
return message;
} @Override
public String execute() {
message = "Hello there, now is: "
+ new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date()); return SUCCESS;
}
}

关于ActionSupport类的更多内容,请参考:http://struts.apache.org/release/2.0.x/struts2-core/apidocs/index.html?overview-summary.html

5. 创建显示JSP文件

<%@page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello there!</title>
</head>
<body>
<h2><s:property value="message"/></h2>
</body>
</html>

JSP文件中用到了struts标签,以后的章节中会详细描述。

6. 打开IE,测试

输入地址:http://127.0.0.1:8080/st/hello.action,回车结果为:

Hello there, now is: 2013-11-18 11:13

图解:

struts2:图解action之HelloWorld示范(从action转到JSP)的更多相关文章

  1. struts2视频学习笔记 07-08(为Action的属性注入值,指定需要Struts 2处理的请求后缀,常用常量)

    课时7 为Action的属性注入值(增加灵活性,适用于经常更改的参数) Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入 ...

  2. 【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型

    一.Action缺省配置值 <span style="font-size:18px;"><package name="itcast" name ...

  3. struts2中错误There is no Action mapped for namespace [/] and action name [] associated with context path

    1 There is no Action mapped for namespace [/] and action name [] associated with context path [/Stru ...

  4. Struts2从一个action转到另一个action的两种方法

    在Struts2中,Action处理完用户请求后,将会返回一个字符串对象,这个字符串对象就是一个逻辑视图名.Struts 2通过配置逻辑视图名和物理视图之间的映射关系,一旦系统收到Action返回的某 ...

  5. Struts2中关于"There is no Action mapped for namespace / and action name"的总结

    今天在调试一个基础的Struts2框架小程序.总是提示"There is no Action mapped for namespace / and action name"的错误. ...

  6. Struts2 从一个Action跳至另一个Action

    Struts2  从一个Action跳至另一个Action 一.注解的 @Result(name=SUCCESS,type="chain", params={"actio ...

  7. struts2的action的知识点和利用action向页面注入值的操作

    1.      Action的顺序,会先搜索指定名字下的包的action,如果找不到会去搜索默认路径下的包下的action. 2.      如果没有给action设置值,那么action会有一些默认 ...

  8. struts2.1.6教程三、在Action获取Scope对象

    引言:在前面的Action操作中,关键就是Action中的exectue方法,但是此方法并没有request.session.application等对象作为参数,自然就不能利用这些对象来操作.下面我 ...

  9. Struts2基于XML配置方式实现对Action方法进行校验

    JavaWeb框架(2)  使用XML对Action方法进行校验方式有两种,一种是对Action的所有方法进行校验,另一种是对Action指定方法进行校验. 对Action的所有方法进行校验: 步骤: ...

随机推荐

  1. C语言中连接器介绍

    在C语言中.一个重要的思想就是分别编译.即若干个源程序能够在不同的时候单独进行编译.然后在恰当的时候整合到一起.可是连接器通常是与C编译器分离的,连接器怎样做到把若干个C源程序合并成一个总体呢? 典型 ...

  2. 使用 axios 详解

    Vue.js 1.0 我们常使用 vue-resource (官方ajax库), Vue 2.0 发布后作者宣告不再对 vue-resource 进行更新, 推荐我们使用 axios (基于 Prom ...

  3. [转发]jquery获取当前页面的URL信息

    以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前U ...

  4. 10个超棒jQuery表单操作代码片段

    jQuery绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的javascript,在今天这篇代码片段分享文章中,这里 ...

  5. linux下编译Zero C ICE

    0.简介 ZeroC ICE 是指ZeroC公司(www.zeroc.com)的ICE(Internet Communications Engine)中间件平台. 目前ICE平台中包括Ice,Ice- ...

  6. 转 configure: error: *** libmcrypt was not found解决方案

    安装到mcrypt的时候出现了问题./configure提示出错,首先提示*** Could not run libmcrypt test program, checking why…*** The ...

  7. docker-compose 管理多个docker容器实例

    Compose 安装 运行此命令下载最新版本的Docker Compose $ curl -L https://github.com/docker/compose/releases/download/ ...

  8. Maven实战——常用Maven插件介绍

    maven nexus 库已上传了第三方jar,但就是用mvn compile下不到本地 回答于 2013-06-04 14:40 你是通过何种方式上传到nexus的? 有给pom文件吗? 如果是单纯 ...

  9. [转载]virtual&nbsp;box如何生成新的UUID

    原文地址:virtual box如何生成新的UUID作者:阿昭 问题描述:在为Virtual Box加载多台虚拟机器(显然这些虚拟机器都来自一个模板)的时候,出现如下错误: "Cannot ...

  10. 在 iOS 中实现方法链调用

    编译:伯乐在线 - 林欣达 如有好文章投稿,请点击 → 这里了解详情 如需转载,发送「转载」二字查看说明 前言 链式调用(chained calls)是指在函数调用返回了一个对象的时候,使得这个调用链 ...