struts2继承ActionSupport类例子

struts2继承ActionSupport类例子

实现方法基于struts2框架
Struts2中,Action可以不实现任何特殊的接口或者继承特殊的类,仅仅是一个POJO就可以;也可以实现Xwork2中的Action接口;ActionSupport类本身实现了Action接口,所以继承ActionSupport类就相当于实现了Action接口。

基本数据验证

要实现数据验证的功能,只需要在Action类中覆盖实现validate方法即可;在validate方法内部,对请求传递过来的数据进行校验,如果不满足要求,那么添加例外信息到父类用于存放例外的集合中。示例代码如下:

package cn.javass.hello.struts2impl.action;  

import com.opensymphony.xwork2.ActionSupport;
  
public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    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;
    }  
    
}  

validate方法是没有返回值的,那么当验证后,如果有数据没有通过验证,该返回到什么页面呢?这就需要在struts.xml中的Action配置里面,添加一个名称为input的result配置,也就是说,如果validate方法中,有数据没有通过验证,那么会自动跳转回到该action中名称为input的result所配置的页面。示例如下:

<?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>  
    <constant name="struts.devMode" value="true" />        <!-- 设置了程序的运行模式 -->
    <constant name="struts.locale" value="zh_CN"/>         <!-- 设置程序运行所使用的locale -->
    <constant name="struts.i18n.encoding" value="utf-8"/>  <!-- 设置程序运行时用的编码方式 -->
    <!-- 正确设置后面两个参数,就可以解决Struts2的中文问题了。 -->
    
  
    <package name="helloworld"  extends="struts-default">  
        <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/login.jsp</result>   
        </action>  
    </package>  
    
</struts> 

当输入信息不满足条件的时候,将错误信息显示在前台页面上,代码如下:

<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; utf-8">  
<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>  

在JSP页面中利用<s:fielderror/>标签在相应的字段处输出错误信息。但是,在实际开发中,<s:fielderror/>它会输出全部的错误信息内容。而如果想选择性地输出指定错误信息。我们可以使用如下代码解决:

<!-- 方法一 -->
<s:fielderror>  
  <s:param>username</s:param> <!--显示指定的 username字段的 错误消息-->  
<s:fielderror/>

<!-- 方法二 -->
<s:fielderror fieldName="username"/> <!--显示指定的 username字段的 错误消息-->  

以下为实际代码:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
声明:部分转载于博客园JNing!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值