1. 程式人生 > >01_11_Strtus2簡單數據驗證

01_11_Strtus2簡單數據驗證

space blog del public ldr tag style open exec

01_11_Strtus2簡單數據驗證

1. 引入struts標簽

<%@taglib uri="/struts-tags" prefix="s" %>

2. 配置struts.xml文件

<package name="user" namespace="/user" extends="struts-default">

<action name="user" class="com.sumapay.user.UserAction">

<result>

/user_add_success.jsp

</result>

<result name="error"

>

/user_add_error.jsp

</result>

</action>

</package>

3. 實現Action

package com.sumapay.user;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.sumapay.domain.User;

public class UserAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private String name;

public String add() {

if(name == null || !name.equals("admin")) {

this.addFieldError("name", "name is error");

this.addFieldError("name", "name is not null");

return "error";

}

return "success";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String execute() {

return "success";

}

}

4. struts標簽獲取後端值

<s:fielderror fieldName="name" theme="simple" />

<!--獲取後端報錯的信息-->

<br />

<s:property value="errors.name[0]"/>

<!--獲取屬性值-->

<s:debug />

01_11_Strtus2簡單數據驗證