2020国产成人精品视频,性做久久久久久久久,亚洲国产成人久久综合一区,亚洲影院天堂中文av色

分享

Struts2框架中為什么要繼承ActionSupport類,以及實現(xiàn)過程

 一本正經(jīng)地胡鬧 2019-07-12

   struts可以繼承ActionSupport類,也可以不繼承,繼承的好處簡單來說就是更方便實現(xiàn)驗證,國際化等功能,與struts2的功能結(jié)合緊密,方便我們開發(fā)。

ActionSupport類的作用:

     此類實現(xiàn)了很多實用的接口,提供了很多默認的方法,這些默認方法包括國際化信息,默認的處理用戶請求的方法等,可以大大簡化action的開發(fā),在繼承ActionSupport的情況下,必須有無參構(gòu)造函數(shù)。

下面用在struts2框架搭建完成的基礎(chǔ)上,用 用戶請求的例子來實現(xiàn)ActionSupport類:

1.創(chuàng)建視圖層兩個頁面index.jsp和welcome.jsp頁面,下面只展示index.jsp頁面,welcome.jsp頁面的代碼自己簡單寫一下看一下效果就行。這個jsp頁面的<s:fielderror/>標簽會在相應(yīng)的字段處輸出錯誤信息。

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; utf-8">
  7. <title>Insert title here</title>
  8. <style type="text/css">
  9. ul,li {
  10. list-style-type:none;
  11. margin:0px;
  12. float:left;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <form action="helloworldAction" method="post">
  18. <input type="hidden" name="submitFlag" value="login"/>
  19. <div>
  20. <font color=red><s:fielderror fieldName="account"/></font>
  21. <br/>
  22. 賬號:<input type="text" name="account">
  23. </div>
  24. <div>
  25. <font color=red><s:fielderror fieldName="password"/></font>
  26. <br/>
  27. 密碼:<input type="password" name="password">
  28. </div>
  29. <input type="submit" value="提交">
  30. </form>
  31. </body>
  32. </html>

2.實現(xiàn)action類封裝HTTP請求參數(shù),類里面應(yīng)該包含與請求參數(shù)對應(yīng)的屬性,并為屬性提供get,set方法,再說一次,在繼承ActionSupport的情況下,必須有無參構(gòu)造函數(shù)。

validate方法內(nèi)部,對請求傳遞過來的數(shù)據(jù)進行校驗,而且我們也能看出來同一個數(shù)據(jù)可以進行多方面的驗證,如果不滿足要求,內(nèi)容將會在頁面上直接顯示。里面重寫了 execute() throws Exception方法,返回字符串。

  1. package com.hnpi.action;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. public class RegisterAction extends ActionSupport{
  4. private String account;
  5. private String password;
  6. private String submitFlag;
  7. public String execute() throws Exception {
  8. this.businessExecute();
  9. return "toWelcome";
  10. }
  11. public void validate(){
  12. if(account==null || account.trim().length()==0){
  13. this.addFieldError("account", "賬號不可以為空");
  14. }
  15. if(password==null || password.trim().length()==0){
  16. this.addFieldError("password", "密碼不可以為空");
  17. }
  18. if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){
  19. this.addFieldError("password", "密碼長度至少為6位");
  20. }
  21. }
  22. public void businessExecute(){
  23. System.out.println("用戶輸入的參數(shù)為==="+"account="+account+",password="+password+",submitFlag="+submitFlag);
  24. }
  25. public String getAccount() {
  26. return account;
  27. }
  28. public void setAccount(String account) {
  29. this.account = account;
  30. }
  31. public String getPassword() {
  32. return password;
  33. }
  34. public void setPassword(String password) {
  35. this.password = password;
  36. }
  37. public String getSubmitFlag() {
  38. return submitFlag;
  39. }
  40. public void setSubmitFlag(String submitFlag) {
  41. this.submitFlag = submitFlag;
  42. }
  43. }

 

3.從上面我們也可以看出來,validate方法是沒有返回值的,如果驗證不成功的話,錯誤信息該怎么在頁面上顯示出來呢?我們需要在struts.xml中的Action配置里面,添加一個名稱為input的result配置,沒有通過驗證,那么會自動跳轉(zhuǎn)回到該action中名稱為input的result所配置的頁面。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts./dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="helloworld" extends="struts-default">
  7. <action name="helloworldAction" class="com.hnpi.action.RegisterAction">
  8. <result name="toWelcome">/welcome.jsp</result>
  9. <result name="input">/index.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

下面我們來看一下代碼效果圖:

這個例子可以看出來,validate方法會先于execute方法被執(zhí)行,只有validate方法執(zhí)行后,又沒有發(fā)現(xiàn)驗證錯誤的時候,才會運行execute方法,否則會自動跳轉(zhuǎn)到你所配置的input所對應(yīng)的頁面。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多