struts2|action接收来自jsp的参数?

一、jsp向服务器提交参数的方法如下:

  1. Form表单提交。
  2. request.setAttribute();和request.getAttribute();
  3. URL传值。
  
    
    
  

二、action接收参数的方式有如下三种。

1. action类中设置接受参数的同名private属性,生成相关属性的set、get方法。调用set方法自动把jsp参数值传给属性。

jsp页面如下:




action类如下:

public class UserAction extends ActionSupport {
    private String username;
        private String password;
        private void setUsername(String username) {
          this.username = username;
        }
        private String getUsername() {
          return username;
        }
        private void setPassword(String password) {
          this.password = password;
        }
        private String getPassword() {
          return password;
        }
}   

不需要生成实体类。

2. 域模型(DomainModel)

jsp页面如下(参数名称改变了):




action类如下:

public class UserAction2 extends ActionSupport {
 private User;
 private void setUser(User user) {
   this.username = username;
 }
 private User getUser() {
   return user;
 }
}   

实体类如下:

public class User {
  private String username;
  private String password;
  private void setUsername(String username) {
    this.username = username;
  }
  private String getUsername() {
    return username;
  }
  private void setPassword(String password) {
    this.password = password;
  }
  private String getPassword() {
    return password;
  }
}

3. 模型驱动(ModelDriven)

体现MVC思想。action类实现ModelDriven接口。
jsp页面如下:




action类如下(实体类实例化):

public class UserAction3 extends ActionSupport implement ModelDriven {
  private User user = new User();

  @overwrite
  public User getModel() {
    return user;
  }
}   

实体类如下:

public class User {
  private String username;
  private String password;
  private void setUsername(String username) {
    this.username = username;
  }
  private String getUsername() {
    return username;
  }
  private void setPassword(String password) {
    this.password = password;
  }
  private String getPassword() {
    return password;
  }
}

你可能感兴趣的:(struts2|action接收来自jsp的参数?)