国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 15:56:52
字體:
來源:轉載
供稿:網友

  5、創建form數據對象
  
  打開File->new->package對話框,name中輸入com.is.form,點擊Finish按鈕。在右邊的Package EXPlorer樹中找到剛才創建的包,右鍵點擊com.is.form包,菜單中的new->others,找到Amateras->struts->Struts Action Form,點擊next,在對話框中name欄輸入LoginForm,點擊Finish按鈕。
  
  編輯LoginForm類的內容為:
  
  package com.is.form;
  import org.apache.struts.action.ActionForm;
  public class LoginForm extends ActionForm
  {
  PRivate static final long
  serialVersionUID = 1L;
  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)
  {
  this.password = password;
  }
  /**
  * @return Returns the username.
  */
  public String getUsername()
  {
  return username;
  }
  /**
  * @param username The username to set.
  */
  public void setUsername(String username)
  {
  this.username = username;
  }
  }
  
  注重,這里的兩個屬性分別對應我們jsp中form中的兩個輸入控件的名稱,為什么這樣做,可以去看struts的幫助文檔了,我就不具體說了,還有form類再寫完屬性后,get和set方法可以通過eclipse的source中的命令來自動生成,在右鍵菜單中,也不具體說了,去網上查資料吧,關于eclipse的使用有很多的文檔。
  
  七、安裝Eclipse Html Editor插件
  
  解壓縮tk.eclipse.plugin.htmleditor_1.6.7.zip包,然后將plugins目錄拷貝至D:/eclipse目錄下覆蓋原文件夾即可。到此Eclipse HTML Editor插件安裝完成。
  
  八、安裝StrutsIDE插件
  
  解壓縮tk.eclipse.plugin.struts_1.1.7.zip包,然后將plugins目錄拷貝至D:/eclipse目錄下覆蓋原文件夾即可。
  
  好了,到此StrutsIDE插件安裝完成。
  
  6、創建action對象
  
  同創建form的過程相同,我們只是新建一個com.is.action包,同樣的過程,打開新建向導,只是選擇Struts Action,創建LoginAction.java類,均選默認值。我們編輯LoginAction為如下內容:
  package com.is.action;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.struts.action.Action;
  import org.apache.struts.action.ActionForm;
  import org.apache.struts.action.ActionForward;
  import org.apache.struts.action.ActionMapping;
  
  import com.is.form.LoginForm;
  
  public class LoginAction extends Action
  {
  private static final long serialVersionUID = 1L;
  
  public ActionForward execute
  (ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws Exception {
  
  // this line is here for when the
  input page is upload-utf8.jsp,
  
  // it sets the correct character
  encoding for the response
  
  String encoding = request.getCharacterEncoding();
  
  if ((encoding != null) &&
  (encoding.equalsIgnoreCase("GB2312")))
  {
  
  response.setContentType
  ("text/html; charset=GB2312");
  
  } else {
  
  response.setContentType
  ("text/html; charset=GBK");
  
  }
  
  try {
  
  if (form instanceof LoginForm)
  {
  
  LoginForm theForm = (LoginForm) form;
  
  if(theForm.getUsername().equals("user") &&
  
  theForm.getPassword().equals("123456"))
  {
  
  return new ActionForward("/welcome.do?type=true");
  
  }
  
  
  else {
  
  return new ActionForward("/welcome.do?type=false");
  
  }
  
  }
  } catch (Exception e)
  {
  
  }
  
  // this shouldn't happen in this example
  
  return null;
  
  }
  }
  
  注重這里是直接用ActionForward轉向的,你也可以按照struts中提供的空白例程struts-blank.war中的做法進行轉向,可以比較一下會有收獲的。
  
  7、創建登錄成功頁面
  
  同創建index.jsp頁面相同,我們創建welcome.jsp頁面,均使用默認設置。并編輯其內容如下:
  
  <%@page pageEncoding="GBK"
  contentType="text/html;
  charset=GBK" %>
  <html>
  <head>
  <meta http-equiv="Content-Type"
  content="text/html;
  charset=GBK"/>
  <title></title>
  </head>
  <body>
  <%
  String type = request.getParameter("type");
  if(type!=null&&type.equals("true")){
  out.print("歡迎您的光臨!");
  
  }
  else{
  out.print("對不起,你輸入的用戶名或者密碼錯誤!");
  }
  %>
  </body>
  </html>
  
  8、增加Struts-config.xml中的配置
  
  添加formbean的配置,在和標簽之間加入:
  
  <form-bean
  name="loginForm"
  type="com.is.form.LoginForm"/>
  
  添加jsp文件的映射,在和標簽之間加入:
  
  <action
  path="/index"
  forward="/index.jsp"/>
  <action
  path="/welcome"
  forward="/welcome.jsp"/>
  
  添加action文件的映射,在和標簽之間加入:
  
  path="/logincheck"
  type="com.is.action.LoginAction"
  name="loginForm"
  scope="request"
  validate="true"/>
  
  修改后的struts-config.xml大致如下形式:
  
  <?xml version="1.0"?>
  <!DOCTYPE struts-config PUBLIC "-
  //Apache Software Foundation
  //DTD Struts Configuration 1.2//EN"
  "http://struts.apache.org/dtds
  /struts-config_1_2.dtd">
  <struts-config>
  <data-sources>
  </data-sources>
  <form-beans>
  <form-bean
  name="loginForm"
  type="com.is.form.LoginForm"/>
  </form-beans>
  <global-exceptions>
  </global-exceptions>
  <global-forwards>
  </global-forwards>
  <action-mappings>
  <action
  path="/index"
  forward="/index.jsp"/>
  <action
  path="/welcome"
  forward="/welcome.jsp"/>
  <action
  path="/logincheck"
  type="com.is.action.LoginAction"
  name="loginForm"
  scope="request"
  validate="true"/>
  </action-mappings>
  <controller processorClass=
  "org.apache.struts.tiles.TilesRequestProcessor"/>
  <message-resources parameter="MessageResources"/>
  <plug-in className=
  "org.apache.struts.tiles.TilesPlugin">
  <set-property property="definitions-config"
  value="/WEB-INF/tiles-defs.xml"/>
  <set-property property="moduleAware" value="true"/>
  </plug-in>
  <plug-in className=
  "org.apache.struts.validator.ValidatorPlugIn">
  <set-property property="pathnames"
  value="/WEB-INF/validator-rules.xml,
  /WEB-INF/validation.xml"/>
  </plug-in>
  </struts-config>
  
  到此我們可以運行測試程序了。
  
  9、運行測試程序
  
  右鍵點擊testweb工程根目錄,點擊菜單中的Tomcate project->update context definition,將工程部署進tomcat,成功后會提示操作成功。
  
  點擊菜單欄中的雄貓圖標啟動tomcat,然后在IE地址欄中輸入http://localhost:8080/testweb/index.do,我們會看到index.jsp的頁面內容。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舒城县| 栾城县| 夏邑县| 商南县| 大庆市| 油尖旺区| 凌源市| 天镇县| 巢湖市| 灌阳县| 黎平县| 潞西市| 米泉市| 九江市| 巴塘县| 赤壁市| 大城县| 延津县| 郁南县| 黑水县| 塔河县| 景泰县| 五河县| 新野县| 茌平县| 白水县| 理塘县| 潍坊市| 张家港市| 晋州市| 平果县| 屏山县| 庆城县| 岳池县| 七台河市| 泸水县| 尖扎县| 图片| 黑龙江省| 岳阳市| 湘乡市|