struts2如何继承ActionSupport?

Action 跟 Actionsupport 的区别
当我们在写Action的时候,可以实现Action接口,也可以继承Actionsupport这个类,到底这两个有什么区别呢?
Action接口有:

public static final java.lang.String SUCCESS = "success";
public static final java.lang.String NONE = "none";
public static final java.lang.String ERROR = "error";
public static final java.lang.String INPUT = "input";
public static final java.lang.String LOGIN = "login";
public abstract java.lang.String execute() throws java.lang.Exception;

而Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之间只能,如校验失败,会转入input处,必须在配置该Action时配置input属性。

具体实现看接下来步骤详解。。

step1:下载Struts2jar包,下载地址在我上一个博客。。

step2:新建一个普通的javaweb项目
打开MyEclipse,点击File>New>Web Project

step3: 把下载好的jar包复制到WEB-INF目录下的lib目录里面,具体细节看我上一个博客。。。

step4:建立我们的视图层
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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> 
    <title>Insert title here</title>
  </head>
  
  <body>
    登陆成功!
  </body>
</html>

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Insert title here</title>
		<style type="text/css">
		ul,li {
    		list-style-type:none;
    		margin:0px;
    		float:left;
}
</style>
		
	</head>

	<body>
		<form action="helloworldAction" method="post">
			<input type="hidden" name="submitFlag" value="login" />
			<div>
				<font color=red><s:fielderror fieldName="account" />
				</font>
				<br />
				账号:
				<input type="text" name="account">
			</div>
			<div>
				<font color=red><s:fielderror fieldName="password" />
				</font>
				<br />
				密码:
				<input type="password" name="password">
			</div>
			<input type="submit" value="提交">
		</form>
	</body>
</html>

step5:建立控制层的ActionSupport
ActionSupport类的作用

struts2不要求我们为自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因此此类中实现了很多的实用接口,提供了很多默认方法。这样可以大大的简化我们的action的开发。


> Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且位属性提供对应的getter 和 setter方法。

在Validatable接口中定义了一个validate()方法,重写该方法,如果校验表单输入域出现错误,则将错误添加到ActionSupport类的fieldErrors域中,然后通过OGNL表达式负责输出。

话不多说,请看代码展示。。

package com.hnpi.action;


import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
	//action用于封装用户请求参数的三个属性
	private String account;  
    private String password;  
    private String submitFlag;  
    //处理用户的execute()请求
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    //完成输入验证后需要重写vaildate方法
    public void validate(){  
    	// 如果用户名为空,或者用户名为空字符串
        if(account==null || account.trim().length()==0){  
        	 // 添加表单校验错误
            this.addFieldError("account", "账号不可以为空");  
        }  
        // 当密码为空,或者密码为空字符串时,添加表单校验错误
        if(password==null || password.trim().length()==0){  
            this.addFieldError("password", "密码不可以为空");  
        }
        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
            this.addFieldError("password", "密码长度至少为6位");  
        }  
    }  
    /** 
     * 示例方法,表示可以执行业务逻辑处理的方法, 
     */  
    public void businessExecute(){  
        System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);  
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSubmitFlag() {
        return submitFlag;
    }
    public void setSubmitFlag(String submitFlag) {
        this.submitFlag = submitFlag;
    }  
	

}

step6:在src下建立struts.xml

上面的Action类重写了validate方法,该方法会在执行系统的execute方法之前执行,如果执行该方法后,Action类的fieldErrors中已经包含了数据校验错误,请求将被转发到input逻辑视图处。

下面是struts的配置:

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "http://struts.apache.org/dtds/struts-2.0.dtd">  
<struts>    
    <package name="helloworld" extends="struts-default">  
    	<!-- 定义login的Action -->
        <action name="helloworldAction"  class="com.hnpi.action.HelloWorldAction">  
        	<!-- 定义welcome的逻辑视图名,对应welcome.jsp页面 -->
            <result name="toWelcome">/index.jsp</result> 
            <!-- 定义input的逻辑视图名,对应login.jsp页面 -->	
             <result name="input">/login.jsp</result>   
        </action>  
    </package>  
    
</struts> 

step7:在web.xml中配置struts2的过滤器

1) Struts2的核心控制器FilterDispatcher被设计成了过滤器,通过标签引入。
2)/*说明所有客户端请求都经由FilterDispatcher处理,并把过滤后的请求交给Struts2进行处理

代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <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>
</web-app>

具体步骤已经实现完毕!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值