Struts2继承ActionSupport全部过程

1:首先创建web项目
Struts2继承ActionSupport全部过程
2:接下来需要在src中创建两个jsp页面,一个显示页面,一个登录页面,具体操作见如下代码

Struts2继承ActionSupport全部过程

Struts2继承ActionSupport全部过程
3:接下来创建一个类和配置文件,具体代码如下
3.1:这个是类

package com.hipn.show;

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;
	    }  
	    
	}  

3.2:这个是配置文件

<?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.hipn.show.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/index.jsp</result>   
        </action>  
    </package>  
</struts>

温馨提示:.class的路径与上面创建的类的路径必须一致,还有配置文件中的action name与显示页面中的action也需一致。

4:下面是web.xml文件。
Struts2继承ActionSupport全部过程
5:对了,还有jar包需要加入lib列中
Struts2继承ActionSupport全部过程
如下为代码效果
Struts2继承ActionSupport全部过程
如有疑问,欢迎给我留言!