熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Eclipse開發struts完全指南二(全)

2013-11-23 20:38:26  來源: Java開源技術 

  創建form數據對象
  
  打開File>new>package對話框name中輸入comisform點擊Finish按鈕在右邊的Package Explorer樹中找到剛才創建的包右鍵點擊comisform包菜單中的new>others找到Amateras>struts>Struts Action Form點擊next在對話框中name欄輸入LoginForm點擊Finish按鈕
  
  編輯LoginForm類的內容為
  
  package comisform;
  import orgapachestrutsactionActionForm;
  public class LoginForm extends ActionForm
  {
  private static final long
  serialVersionUID = L;
  private String username = ;
  private String password = ;
  /**
  * @return Returns the password
  */
  public String getPassword()
  {
  return password;
  }
  
  /**
  
  * @param password The password to set
  */
  public void setPassword(String password)
  {
  thispassword = password;
  }
  /**
  * @return Returns the username
  */
  public String getUsername()
  {
  return username;
  }
  /**
  * @param username The username to set
  */
  public void setUsername(String username)
  {
  thisusername = username;
  }
  }
  
  注意這裡的兩個屬性分別對應我們jsp中form中的兩個輸入控件的名稱為什麼這樣做可以去看struts的幫助文檔了我就不詳細說了還有form類再寫完屬性後get和set方法可以通過eclipse的source中的命令來自動生成在右鍵菜單中也不詳細說了去網上查資料吧關於eclipse的使用有很多的文檔
  
  安裝Eclipse HTML Editor插件
  
  解壓縮tleditor_zip包然後將plugins目錄拷貝至D:\eclipse目錄下覆蓋原文件夾即可到此Eclipse HTML Editor插件安裝完成
  
  安裝StrutsIDE插件
  
  解壓縮tkeclipsepluginstruts_zip包然後將plugins目錄拷貝至D:\eclipse目錄下覆蓋原文件夾即可
  
  好了到此StrutsIDE插件安裝完成
  
  創建action對象
  
  同創建form的過程相同我們只是新建一個comisaction包同樣的過程打開新建向導只是選擇Struts Action創建LoginActionjava類均選默認值我們編輯LoginAction為如下內容
  package comisaction;
  import javaxservlethttpHttpServletRequest;
  import javaxservlethttpHttpServletResponse;
  import orgapachestrutsactionAction;
  import orgapachestrutsactionActionForm;
  import orgapachestrutsactionActionForward;
  import orgapachestrutsactionActionMapping;
  
  import comisformLoginForm;
  
  public class LoginAction extends Action
  {
  private static final long serialVersionUID = L;
  
  public ActionForward execute
  (ActionMapping mapping
  ActionForm form
  HttpServletRequest request
  HttpServletResponse response)
  throws Exception {
  
  // this line is here for when the
  input page is uploadutfjsp
  
  // it sets the correct character
  encoding for the response
  
  String encoding = requestgetCharacterEncoding();
  
  if ((encoding != null) &&
  (encodingequalsIgnoreCase(GB)))
  {
  
  responsesetContentType
  (text/html; charset=GB);
  
  } else {
  
  responsesetContentType
  (text/html; charset=GBK);
  
  }
  
  try {
  
  if (form instanceof LoginForm)
  {
  
  LoginForm theForm = (LoginForm) form;
  
  if(theFormgetUsername()equals(user) &&
  
  theFormgetPassword()equals())
  {
  
  return new ActionForward(/welcomedo?type=true);
  
  }
  
  
  else {
  
  return new ActionForward(/welcomedo?type=false);
  
  }
  
  }
  } catch (Exception e)
  {
  
  }
  
  // this shouldnt happen in this example
  
  return null;
  
  }
  }
  
  注意這裡是直接用ActionForward轉向的你也可以按照struts中提供的空白例程strutsblankwar中的做法進行轉向可以比較一下會有收獲的
  
  創建登錄成功頁面
  
  同創建indexjsp頁面相同我們創建welcomejsp頁面均使用默認設置並編輯其內容如下
  
  <%@page pageEncoding=GBK
  contentType=text/html;
  charset=GBK %>
  <html>
  <head>
  <meta httpequiv=ContentType
  content=text/html;
  charset=GBK/>
  <title></title>
  </head>
  <body>
  <%
  String type = requestgetParameter(type);
  if(type!=null&&typeequals(true)){
  outprint(歡迎您的光臨!);
  
  }
  else{
  outprint(對不起你輸入的用戶名或者密碼錯誤!);
  }
  %>
  </body>
  </html>
  
  增加Strutsconfigxml中的配置
  
  添加formbean的配置在和標簽之間加入
  
  <formbean
  name=loginForm
  type=comisformLoginForm/>
  
  添加jsp文件的映射在和標簽之間加入
  
  <action
  path=/index
  forward=/indexjsp/>
  <action
  path=/welcome
  forward=/welcomejsp/>
  
  添加action文件的映射在和標簽之間加入
  
  path=/logincheck
  type=comisactionLoginAction
  name=loginForm
  scope=request
  validate=true/>
  
  修改後的strutsconfigxml大致如下形式
  
  <?xml version=?>
  <!DOCTYPE strutsconfig PUBLIC
  //Apache Software Foundation
  //DTD Struts Configuration //EN
  
  /strutsconfig__dtd>
  <strutsconfig>
  <datasources>
  </datasources>
  <formbeans>
  <formbean
  name=loginForm
  type=comisformLoginForm/>
  </formbeans>
  <globalexceptions>
  </globalexceptions>
  <globalforwards>
  </globalforwards>
  <actionmappings>
  <action
  path=/index
  forward=/indexjsp/>
  <action
  path=/welcome
  forward=/welcomejsp/>
  <action
  path=/logincheck
  type=comisactionLoginAction
  name=loginForm
  scope=request
  validate=true/>
  </actionmappings>
  <controller processorClass=
  orgapachestrutstilesTilesRequestProcessor/>
  <messageresources parameter=MessageResources/>
  <plugin className=
  orgapachestrutstilesTilesPlugin>
  <setproperty property=definitionsconfig
  value=/WEBINF/tilesdefsxml/>
  <setproperty property=moduleAware value=true/>
  </plugin>
  <plugin className=
  orgapachestrutsvalidatorValidatorPlugIn>
  <setproperty property=pathnames
  value=/WEBINF/validatorrulesxml
  /WEBINF/validationxml/>
  </plugin>
  </strutsconfig>
  
  到此我們可以運行測試程序了
  
  運行測試程序
  
  右鍵點擊testweb工程根目錄點擊菜單中的Tomcate project>update context definition將工程部署進tomcat成功後會提示操作成功
  
  點擊菜單欄中的雄貓圖標啟動tomcat然後在IE地址欄中輸入我們會看到indexjsp的頁面內容
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28811.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.