Action是Struts2的核心,其中包括:部分業務邏輯,資源包獲取,表單驗證,返回視圖頁面等。jsp
Struts2不須要實現任何藉口或擴展類,只須要實現一個public String execute();方法,返回結果畫面ide
第一步:書寫action方法post
public class LoginAction{ public String execute(){ ... return "success"; //struts.xml配置文件的字符串配置一致 } }
第二步:在struts.xml中對Action進行聲明ui
能夠參照配置文件說明:struts.xmlthis
<package name="user" namespace="/User" extends="struts-default"> <action name="login" class="com.action.user.LoginAction"> <result name="success">/welcome.jsp</result> </action> </package>
如今就能夠經過一下連接進行訪問spa
.action後綴是能夠配置的,在strut.xml中配置struts.action.extension屬性code
參照配置文件說明:struts.xmlorm
雖然Action能夠不用實現任何藉口,可是通常爲了更加方便的使用Action,會去實現xml
com.opensymphony.xwork2.ActionSupport public class ActionSupport implements Action, Validateble, ValidationAware, TextProvider, LocaleProvider, Serializable{...}
ActionSupport提供了一些功能
驗證:在Action中實現一個public void validate()方法便可
文字本地化:使用gettext()方法獲取資源包的信息
public void validate(){ if("".equals(user.getUserName())){ addFieldError("username", getText("username.required")); } }
Struts2除了能夠在xml中聲明Action外,還能夠使用註解的方式來聲明。
@Namespace("/User") @ResultPath(value="/") public class LoginAction extends ActionSupport{ @Action(value="welcome", result={ @Result(name="success", location="/welcome.jsp") }) public String execute(){ return "success" } }
表單數據包含在Action中,經過Getter和Setter獲取
<!-- JSP --> <form action="login.action"method="post"> 用戶名:<input type="text"name="username"/> <br/> 密碼: <input type="password"name="password"/><br/> <input type="submit"value="登錄"/> </form> <!-- Action --> public class LoginAction{ private String username; private String password; public String execute(){ return "success"; } 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; } }
<!-- JSP --> <form action="login.action"method="post"> 用戶名:<input type="text"name="user.username"/> <br/> 密碼: <input type="password"name="user.password"/><br/> <input type="submit"value="登錄"/> </form> <!-- Bean --> public class User{ private String username; private String password; 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; } } <!-- Action --> public class LoginAction{ private User user; public String execute(){ return "success"; } public User getUser(){ return user; } public void setUser(User user){ this.user = user; } }
<!-- JSP --> <form action="login.action"method="post"> 用戶名:<input type="text"name="username"/> <br/> 密碼: <input type="password"name="password"/><br/> <input type="submit"value="登錄"/> </form> <!-- Action : Action類要實現一個泛型接口 --> public class LoginAction extends ActionSupport implements ModeDriven<User>{ private User user = new User(); public String execute(){ return "success"; } public User getModel(){ return User; } }
<s:if test="hasActionErrors()"> <div class="error"> <s:actionerror/> </div> </s:if>
<s:if test="hasActionErrors()"> <div class="message"> <s:actionmessage/> </div> </s:if>
JAVA代碼部分:經過addActionError()及addActionMessage()方法來設置信息。