Struts2繼承ActionSupport全部過程
阿新 • • 發佈:2018-12-18
1:首先建立web專案 2:接下來需要在src中建立兩個jsp頁面,一個顯示頁面,一個登入頁面,具體操作見如下程式碼
3:接下來建立一個類和配置檔案,具體程式碼如下 3.1:這個是類
package com.hipn.show; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { private String account; private String password; private String submitFlag; public String execute() throws Exception { this.businessExecute(); return "toWelcome"; } public void validate(){ if(account==null || account.trim().length()==0){ this.addFieldError("account", "賬號不可以為空"); } if(password==null || password.trim().length()==0){ this.addFieldError("password", "密碼不可以為空"); } if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){ this.addFieldError("password", "密碼長度至少為6位"); } } /** * 示例方法,表示可以執行業務邏輯處理的方法, */ public void businessExecute(){ System.out.println("使用者輸入的引數為==="+"account="+account+",password="+password+",submitFlag="+submitFlag); } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSubmitFlag() { return submitFlag; } public void setSubmitFlag(String submitFlag) { this.submitFlag = submitFlag; } }
3.2:這個是配置檔案
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="helloworld" extends="struts-default"> <action name="helloworldAction" class="com.hipn.show.HelloWorldAction"> <result name="toWelcome">/welcome.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts>
溫馨提示:.class的路徑與上面建立的類的路徑必須一致,還有配置檔案中的action name與顯示頁面中的action也需一致。
4:下面是web.xml檔案。 5:對了,還有jar包需要加入lib列中 如下為程式碼效果 如有疑問,歡迎給我留言!