03_Action
来源:互联网 发布:淘宝店铺装修包括哪些 编辑:程序博客网 时间:2024/06/03 03:20
3.1 Action简介
要想取得http中的变量,需要在action中自定义变量,并实现setter()方法;
要想输出到http中,需要在action中自定义变量,并实现getter()方法。
3.1.1 返回类型
在com.opensymphony.xwork2.Action中有struts2框架预定义的五种返回类型:
SUCCESS,NONE,ERROR,INPUT,LOGIN
3.1.2 execute()方法
在Struts2中所有Action都要实现execute()方法,可以:
实现com.opensymphony.xwork2.Action接口;
继承com.opensymphony.xwork2.ActionSupport类。
3.2 Action接口
3.3 ActionSupport基类
ActionSupport基类实现了很多方法,而Action接口则没有
3.4 Action实例
3.4.1 实现Action接口的实例
源文件:web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Struts2</display-name> <!-- Struts2-cleanup --> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Struts2-filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- welcome --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>源文件:struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true"></constant> <package name="ActionDemo" extends="struts-default"> <!-- actions --> <action name="colorText" class="com.uikoo9.action.D01_ColorText"> <result name="success">/D02_colorful.jsp</result> </action> </package></struts>源文件:colorText.java
package com.uikoo9.action;import com.opensymphony.xwork2.Action;public class D01_ColorText implements Action {private String randomRGB;public String execute() throws Exception {int r = (int)(Math.random()*254 + 1);int g = (int)(Math.random()*254 + 1);int b = (int)(Math.random()*254 + 1);randomRGB = Integer.toHexString(r) + Integer.toHexString(g) + Integer.toHexString(b);return SUCCESS;}public String getRandomRGB() {return randomRGB;}public void setRandomRGB(String randomRGB) {this.randomRGB = randomRGB;}}源文件:colorful.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>colorful.jsp</title> </head> <body><font color="${randomRGB}">dshfalkhdlfksahdskaljhfdklsaj</font> </body></html>3.4.2 实现ActionSupport基类的实例
源文件:as.jsp
<%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>as.jsp</title> </head> <body><s:form action="prime.action"><s:textfield label="Please enter a Prime Number:" name="number"></s:textfield><s:submit></s:submit></s:form> </body></html>
源文件:primeAction.java
package com.uikoo9.action;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class D04_primeAction extends ActionSupport {private int num;private String number;@Overridepublic String execute() throws Exception {num = Integer.valueOf(number);if(!isPrimeNumber(num)){addFieldError("number","you enter a non prime number");pause(INPUT);}if(isPrimeNumber(num))return SUCCESS;return INPUT;}public boolean isPrimeNumber(int i){int m = (int)Math.sqrt(i);for(int n=2; n<=m; n++){if(i%n == 0)return false;}return true;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}}
源文件:pass.html
<!DOCTYPE html><html> <head> <title>pass.html</title> </head> <body>congratulation you passed the prime tests! </body></html>
源文件:struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <constant name="struts.devMode" value="true"></constant> <package name="ActionDemo" extends="struts-default"> <!-- actions --> <action name="prime" class="com.uikoo9.action.D04_primeAction"> <result name="success">/D05_pass.html</result> <result name="input">/D03_as.jsp</result> </action> </package></struts>
3.5 在Action中实现基本校验
3.5.1 使用Action的execute()方法
3.5.2 使用ActionSupport的validate()方法
在validate()方法中实现需要的校验,这个方法会在execute()之前执行
3.5.3 使用注释实现校验
本节有一个实例,暂时不写
3.6 ActionContext
3.7 小结
- 03_Action
- struts2_大纲03_action&result
- Struts2学习总结_Action
- 1.6_Action composition
- jbpm学习笔记(5)_ACTION
- 【Struts2】_Action 配置中的各项默认值
- 【struts2】赵雅智_Action名称的搜索顺序
- 013_Action Bar 导览 翻译学习
- Struts2学习笔记5_Action的搜索顺序
- "error":400,"reason":"Bad Request","message":"No values provided for the request parameter '_action
- 关于行为树插件Behavior Designer的学习(一)_Action动作任务
- Struts2学习(三)【主配置文件配置_常量配置_动态方法配置_action书写方式】
- SSH与SSM学习之Struts203——主配置文件配置_常量配置_动态方法配置_action书写方式
- 03
- 03
- 03
- 03
- 03
- javascript 事件冒泡
- 我眼中的工程师文化
- Microsoft Enterprise Library: Exception Handle 模块
- MFC下使用cvSetMouseCallback
- 向量几何在游戏编程中的使用3
- 03_Action
- 技术(3)—要求(或禁止)对象产生于heap之中
- 从Select语句看Oracle查询原理
- sql server 练习题
- C库函数中字符串处理函数集合
- 向量几何在游戏编程中的使用4
- 数据库系统原理教程 第一章 绪论 习题解答
- C++程序检测内存泄露
- c#检查获取内存简单办法