继承ActionSupport

首先还是新建一个web项目,具体方法在上一篇博客已经写出,直接进入正题

先新建一个class,取名HelloWorldAction

然后写方法和参数,实现验证功能,示例代码如下:

package com.hngy;
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;
    }  
    
}  

 

在项目下的src里新建一个struts.xml(注意这里的命名用小写)

然后就要配置struts.xml文件了,代码如下:

<?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">  
        <action name="helloworldAction" class="com.hngy.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/login.jsp</result>   
        </action>  
    </package>
</struts>

配置完成后在项目的WebRoot里面新建两个jsp页面,新建过程参照上图新建的struts.xml

分别取名login.jsp和Welcome.jsp

login.jsp代码如下:

<%@ 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>登陆页面</title>  
<style type="text/css">
ul,li {
    list-style-type:none;
    margin:0px;
    float:left;
}
</style>
</head>  
<body>  
   
<form action="/struts2Deepen2/helloworldAction.action" 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>

效果图如下所示:

如果需要添加jar包和Jquery请参照上一篇博客,里面有详细的过程

以上就是根据本人对继承ActionSupport的理解,若有不足之处请及时指出,感谢!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值