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 小结



原创粉丝点击