免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Struts2框架中為什么要繼承ActionSupport類,以及實(shí)現(xiàn)過(guò)程

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

ActionSupport類的作用:

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

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

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

  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. 賬號(hào):<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.實(shí)現(xiàn)action類封裝HTTP請(qǐng)求參數(shù),類里面應(yīng)該包含與請(qǐng)求參數(shù)對(duì)應(yīng)的屬性,并為屬性提供get,set方法,再說(shuō)一次,在繼承ActionSupport的情況下,必須有無(wú)參構(gòu)造函數(shù)。

validate方法內(nèi)部,對(duì)請(qǐng)求傳遞過(guò)來(lái)的數(shù)據(jù)進(jìn)行校驗(yàn),而且我們也能看出來(lái)同一個(gè)數(shù)據(jù)可以進(jìn)行多方面的驗(yàn)證,如果不滿足要求,內(nèi)容將會(huì)在頁(yè)面上直接顯示。里面重寫了 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", "賬號(hào)不可以為空");
  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", "密碼長(zhǎng)度至少為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.從上面我們也可以看出來(lái),validate方法是沒(méi)有返回值的,如果驗(yàn)證不成功的話,錯(cuò)誤信息該怎么在頁(yè)面上顯示出來(lái)呢?我們需要在struts.xml中的Action配置里面,添加一個(gè)名稱為input的result配置,沒(méi)有通過(guò)驗(yàn)證,那么會(huì)自動(dòng)跳轉(zhuǎn)回到該action中名稱為input的result所配置的頁(yè)面。

  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.apache.org/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>

下面我們來(lái)看一下代碼效果圖:

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

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Struts2 繼承ActionSupport的用處
第一次接觸struts2.......
2. struts2中一個(gè)Action處理多個(gè)方法
Struts2教程
Struts2入門教程-Hello World
Struts2多文件下載
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服