struts2 Action 接收參數的三種方法

剛學Struts2 時 你們可能遇到過不少問題,這裏我講一下Action 接收參數的三種方法,我曾經在這上面摔過一回。因此要警醒一下本身。。。。。服務器

第一種:Action裏聲明屬性,樣例:accountName,password,message,而且要寫get(),set() 方法。。jsp

public class MessageAction extends ActionSupport {ide

// 接受客戶端兩個文本框的值
private String accountName;
private String password;this

// 將服務器端處理結果返回給客戶端
private String message;spa

public String getAccountName() {
return accountName;
}orm

public void setAccountName(String accountName) {
this.accountName = accountName;
}xml

public String getPassword() {
return password;
}jwt

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

public String getMessage() {
return message;
}接口

public void setMessage(String message) {
this.message = message;
}

@Action(value = "messageAction", results = {
@Result(name = "success", type = "dispatcher", location = "/signin.jsp"),
@Result(name = "error", type = "dispatcher", location = "/signin.jsp") })

//上面是註解,就是至關於在struts.xml裏配置Action

<!--  

<action name="messageAction" class="org.orclwdp.jwt.action.MessageAction" >
<result name="success" type="dispatcher" >/signin.jsp</result>
<result name="error" type="dispatcher">/signin.jsp</result>
</action>

 -->
public String execute() {
if ("admin".equals(this.accountName)) {
if ("111111".equals(this.password)) {
message = "登陸成功";
return SUCCESS;
} else {
message = "密碼錯誤";
return ERROR;
}
} else {
message = "賬號不存在";
return ERROR;
}
}
}

第二種:聲明一個對象,固然這個對象是已經有了的,即accountName,password,message。固然這些須要用到的參數屬性是這個對象有的。聲明對象要也要寫get(),set() 方法。。但在前臺頁面裏form表單中input輸入框name屬性:

<input class="m-wrap" id="editAccountName" name="userInfo.accountName" type="text" value="" placeholder="請輸入用戶名" />

 

public class MessageAction extends ActionSupport {

private UserInfo userInfo = new UserInfo();

public UserInfo getUserInfo() {
return userInfo;
}

public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}

@Action(value = "messageAction", results = {
@Result(name = "success", type = "dispatcher", location = "/signin.jsp"),
@Result(name = "error", type = "dispatcher", location = "/signin.jsp") })

 

public String execute() {
if ("admin".equals(userInfo.getAccountName()) {
if ("111111".equals(userInfo.getPassword()) {
message = "登陸成功";
return SUCCESS;
} else {
message = "密碼錯誤";
return ERROR;
}
} else {
message = "賬號不存在";
return ERROR;
}
}
}

第三種:實現ModelDriven<UserInfo>接口,UserInfo不須要寫get(),set() 方法。實現 ModelDriven<UserInfo>

 

public class HelloAction extends ActionSupport implements ModelDriven<UserInfo> {
  private UserInfo userInfo=new UserInfo();

  

  

@Override
public UserInfo getModel() {
return userInfo;
}

  ..................同上

 

}

相關文章
相關標籤/搜索