Struts 2 Framework -- the third day

Keywords: JSP Struts Attribute Java

How to deal with struts 2 and how to implement action

1. Attribute driven

jsp page:

 <form action="register" method="post"> 
  	//User name: < input type = "text" name = "name" / > < br > 
  	//Password: < input type = "password" name = "PWD" / > < br > 
  	//Age: < input type = "text" name = "age" / > < br > 
  	//Email: < input type = "text" name = "email" / > < br > 
  	<input type="submit" value="Submission" /> 
  </form>

action code:

public class Login extends ActionSupport{
	private String name;
	private String pwd;
	
	
	@Override
	public String execute() throws Exception {
		if ("admin".equals(name)&&"123".equals(pwd)) {
			return "success";
		} else {
			return "fail";
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}

 

2. Driving image

jsp page:

<form action="register" method="post"> 
  	//User name: < input type = "text" name = "user. Name" / > < br > 
  	//Password: < input type = "password" name = "user. PWD" / > < br > 
  	//Age: < input type = "text" name = "user. Age" / > < br > 
  	//Email: < input type = "text" name = "user. Email" / > < br > 
  	<input type="submit" value="Submission" /> 
  </form>

action class:

public class Login extends ActionSupport{
	private User user; //register
	public String register(){
		System.out.println(user); 
		return SUCCESS; 
		}
	public User getUser() { 
		return user; 
		}
	public void setUser(User user) { 
		this.user = user; 
		} 
	}

userBean class:

public class User { 
	private String name; 
	private String pwd; 
	private int age; 
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	} 
}

3. Mode drive

jsp page:

 <form action="register" method="post"> 
  	//User name: < input type = "text" name = "name" / > < br > 
  	//Password: < input type = "password" name = "PWD" / > < br > 
  	//Age: < input type = "text" name = "age" / > < br > 
  	//Email: < input type = "text" name = "email" / > < br > 
  	<input type="submit" value="Submission" /> 
  </form>

       action:

import com.opensymphony.xwork2.ModelDriven;

public class Login extends ActionSupport{
	public class UserDrivenAction implements ModelDriven<User>{
		private User user = new User(); //register 
		public String register(){ 
			System.out.println(user); 
			return SUCCESS; 
			}
		public User getModel() { 
			return user; 
			}
		public User getUser() { 
			return user; 
			}public void setUser(User user) { 
				this.user = user; 
				} 
			}
		}

uesrBean class:

public class User { 
	private String name; 
	private String pwd; 
	private int age; 
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	} 
}

4. Define a pojo class

Advantage: just customize a common java class, no intrusive

public class PojoAction { 
    public String execute(){ 
    System.out.println("pojo action"); 
    return "success"; 
    }
}

5. Implement Action interface

Benefit: make our code more standard

public class InterfaceAction implements Action{ 
    public String execute() throws Exception { 
        System.out.println("interface action");
        return SUCCESS; 
    } 
}

6. Inherit ActionSupport class

Benefits: it can inherit some actionsupport implementation functions, such as: verification; officially recommended

public class ExtendsAction extends ActionSupport{ 
    @Override 
    public String execute() throws Exception { 
        System.out.println("extends action"); 
        return SUCCESS; 
    } 
}

 

Published 7 original articles, won praise 6, visited 4380
Private letter follow

Posted by badman on Sat, 18 Jan 2020 08:19:26 -0800