struts2之Action

(1)具体的视图的返回可以由用户自己定义的action来决定

(2)具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容

(3)具体的action的实现:

(a)可以是一个普通的java类,里面有publicString execute()方法

<span style="font-size:18px;">package com.dqpi.eonline;
/**
 * <span style="color:#ff0000;">action的实现方式之一:一个普通的java类,里面含有一个返回值为String类型的execute的共有方法</span>
 * @author Administrator
 *
 */
public class IndexAction1 {
	public String execute() {
		return "success";
	}
}</span>

 

(b)者实现了Action接口

 

<span style="font-size:18px;">package com.dqpi.eonline;

import com.opensymphony.xwork2.Action;
/**
 * <span style="color:#ff0000;">action的实现方式之二:实现action接口</span>
 * @author Administrator
 *
 */
public class IndexAction2 implements Action{

	@Override
	public String execute() throws Exception {
		return "success";
	}

}</span>

 

(c)最常用的是从ActionSupport继承,好处在于可以直接使用struts2封装好的方法。

 

<span style="font-size:18px;">package com.dqpi.eonline;

import com.opensymphony.xwork2.ActionSupport;
/**
 * <span style="color:#ff0000;">action的第三种实现方式:继承ActionSupport</span>
 * @author Administrator
 *
 */
public class IndexAction3 extends ActionSupport{

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

}</span>

 

(4)Action接口属于xwork中的,如果要查看源码,需要将xwork的源码添加进去,方法和添加2的jar的方法一致。

(5)ActionSupport是实现了Action,Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable这些的接口,里面封装了很多的方法,所以推荐使用,我们可以直接调用这些方法,ActionSupport也是属于xwork中的。(有次电话面试,应该是久其软件吧,问道我框架,然后问我实现方式,我说了三种,接着又问那ActionSupport的父类是什么,我如实的回答不知道,那个人就告诉我学习的时候需要追究原因,读读源码就知道了)

struts.xml

 

<span style="font-size:18px;"><?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>
    <!-- Add packages here -->
     <constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
	<!--通过改变class中的类的名字就可以将action的三种实现全部尝试  -->
        <action name="actiontest" class="com.dqpi.eonline.IndexAction2">
            <result>
            	/ActionIntroduction.jsp
            </result>
        </action>
    </package>
</struts></span>

 

 

 

ActionIntroduction.jsp

 

<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>ActionIntroduction</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    ActionIntroduction <br>
  </body>
</html>
</span>

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值