struts2:图解action之HelloWorld示范(从action转到JSP)
虽然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)的更多相关文章
- struts2视频学习笔记 07-08(为Action的属性注入值,指定需要Struts 2处理的请求后缀,常用常量)
课时7 为Action的属性注入值(增加灵活性,适用于经常更改的参数) Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入 ...
- 【Struts2学习笔记(2)】Action默认值和配置Action于result各种转发类型
一.Action缺省配置值 <span style="font-size:18px;"><package name="itcast" name ...
- 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 ...
- Struts2从一个action转到另一个action的两种方法
在Struts2中,Action处理完用户请求后,将会返回一个字符串对象,这个字符串对象就是一个逻辑视图名.Struts 2通过配置逻辑视图名和物理视图之间的映射关系,一旦系统收到Action返回的某 ...
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
今天在调试一个基础的Struts2框架小程序.总是提示"There is no Action mapped for namespace / and action name"的错误. ...
- Struts2 从一个Action跳至另一个Action
Struts2 从一个Action跳至另一个Action 一.注解的 @Result(name=SUCCESS,type="chain", params={"actio ...
- struts2的action的知识点和利用action向页面注入值的操作
1. Action的顺序,会先搜索指定名字下的包的action,如果找不到会去搜索默认路径下的包下的action. 2. 如果没有给action设置值,那么action会有一些默认 ...
- struts2.1.6教程三、在Action获取Scope对象
引言:在前面的Action操作中,关键就是Action中的exectue方法,但是此方法并没有request.session.application等对象作为参数,自然就不能利用这些对象来操作.下面我 ...
- Struts2基于XML配置方式实现对Action方法进行校验
JavaWeb框架(2) 使用XML对Action方法进行校验方式有两种,一种是对Action的所有方法进行校验,另一种是对Action指定方法进行校验. 对Action的所有方法进行校验: 步骤: ...
随机推荐
- 获取ul下li标签里点击的是哪一个li并获取li里a标签的值
$("ul li").click(function(){ //获取当天月份 ).text(); ,) alert(month); }); <!doctype html> ...
- Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'系列三:重置主从同步
1:停止slave服务器的主从同步 stop slave; 2:对Master数据库加锁 flush tables with read lock; 3:备份Master上的数据 mysqldump - ...
- 修改浏览器Cookie
Firefox:安装 Web Developer 插件,装完重启之后会出现一排新的工具栏,点击 Cookies -> Add Cookie… 即可增加/修改一个Cookie了. Opera:Op ...
- Inno Setup入门(三)——指定压缩方式
Setup段中的compression指定了采用的压缩方式,较高的压缩率需要较多的时间或者需要更大的内存空间,可用的值如下: zip zip/1到zip/9 bzip bzip/1 到bzip/9 l ...
- PLSQL创建DBLINK
Oracle创建dblink,多用于数据的同步机制.不建议直接用dblink对数据库频繁的操作... 00.查看创建dblink权限 select * from user_sys_privs t wh ...
- 分享一个VS2008漂亮的黑色主题
如果恰巧你也使用Visual Studio 2008这个版本, 如果你也恰巧厌烦了白色的背景, 那么推荐你使用这款纯黑色theme,搭配上DroidSansMonog这个等宽字体(打包下载),每天都可 ...
- 远程阿里云window服务器报错身份验证错误
整理文章,很久之前遇到的一个问题,一直呆在草稿箱,特发布出来,帮助可能遇到该问题的人 mstsc连接时报错如下 解决方法: 修改本地安全组策略[安全组 gpedit.msc]
- centos中执行apt-get命令提示apt-get command not found
先说结论: 在centos下用yum install xxx yum和apt-get的区别: 一般来说著名的linux系统基本上分两大类: RedHat系列:Redhat.Centos.Fedora ...
- springmvc概述及框架原理
一. 前言 MVC不是框架而是一种设计模式. MVC的全名Model View Controller,即模型-视图-控制器的缩写,这是一种设计模式,而非架构.MVC它强制的使用应用程序的输入.处理.和 ...
- 使用 numpy.random.choice随机采样
使用 numpy.random.choice随机采样: 说明: numpy.random.choice(a, size=None, replace=True, p=None) 示例: >> ...