structs实现三种action的方法

本文介绍了三种Struts2框架中实现登录功能的方法,包括一般类、实现Action接口的类及继承ActionSupport抽象类的方式,并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种:一般类,带有public String execute()方法。

第二种:继承LoginActionInterface implements Action接口的类。

第三种:继承LoginActionSupport extends ActionSupport抽象类的类。


结构图



三个类的具体写法,也贴上来供大家参考

package com.test.action;

import com.test.dao.UserCheck;
import com.test.vo.User;

public class LoginAction {
	private String username;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String password;
	
	public String execute()
	{
		User u=new User();
		u.setUsername(this.getUsername());
		u.setPassword(this.getPassword());
		
		/*调用业务逻辑层代码*/
		UserCheck check=new UserCheck();
		if(check.login(u))
		{
			return "login_ok";
		}
		else
		{
			return "login_fail";
		}
		
	}
}

package com.test.action;

import com.opensymphony.xwork2.Action;
import com.test.dao.UserCheck;
import com.test.vo.User;

public class LoginActionInterface implements Action{
	private String username;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String password;
	
	public String execute()
	{
		User u=new User();
		u.setUsername(this.getUsername());
		u.setPassword(this.getPassword());
		
		/*调用业务逻辑层代码*/
		UserCheck check=new UserCheck();
		if(check.login(u))
		{
			return "login_ok";
		}
		else
		{
			return "login_fail";
		}
		
	}
}

package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.dao.UserCheck;
import com.test.vo.User;

public class LoginActionSupport extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public LoginActionSupport()
	{
		System.out.println("LoginActionSupport");
	}
	private String username;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String password;
	
	public String execute()
	{
		User u=new User();
		u.setUsername(this.getUsername());
		u.setPassword(this.getPassword());
		
		/*调用业务逻辑层代码*/
		UserCheck check=new UserCheck();
		if(check.login(u))
		{
			return "login_ok";
		}
		else
		{
			return "login_fail";
		}
		
	}
}


其实我们发现,只要具有public String execute()这个方法就行了,还要注意,在继承ActionSupport的时候,默认的execute()是返回success。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值