java中struts配置_java

來源:互聯網
上載者:User

1.瞭解struts

Struts2架構中核心組件就是Action、攔截器等,Struts2架構使用包來管理Action和攔截器等。每個包就是多個Action、多個攔截器、多個攔截器引用的集合。
在struts.xml檔案中package元素用於定義包配置,每個package元素定義了一個包配置。它的常用屬性有:
l name:必填屬性,用來指定包的名字。
l extends:可選屬性,用來指定該包繼承其他包。繼承其它包,可以繼承其它包中的Action定義、攔截器定義等。
l namespace:可選屬性,用來指定該包的命名空間。

2.配置struts

  首先建立一個web項目,在右擊一個項目,選擇myeclipse下add struts
在選擇 struts2.1 單擊下一步在選擇自己所需要的包 在儲存

3.修改使用者登入驗證樣本,多增加一個註冊使用者功能。

1.       修改Action類:

package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport; /***@authorqiujy*@version1.0*/publicclass LoginAction extends ActionSupport{  private String userName;  private String password;    private String msg; //結果資訊屬性    /**   *@returnthemsg   */  public String getMsg() {    returnmsg;  }  /**   *@parammsgthemsgtoset   */  publicvoid setMsg(String msg) {    this.msg = msg;  }  /**   *@returntheuserName   */  public String getUserName() {    returnuserName;  }  /**   *@paramuserNametheuserNametoset   */  publicvoid setUserName(String userName) {    this.userName = userName;  }  /**   *@returnthepassword   */  public String getPassword() {    returnpassword;  }  /**   *@parampasswordthepasswordtoset   */  publicvoid setPassword(String password) {    this.password = password;  }    /**   *處理使用者請求的login()方法   *@return結果導航字串   *@throwsException   */  public String login() throws Exception{    if("test".equals(123) && "test".equals(123)){      msg = "登入成功,歡迎" + 123;      //擷取ActionContext執行個體,通過它來訪問Servlet API      ActionContext context = ActionContext.getContext();      //看session中是否已經存放了使用者名稱,如果存放了:說明已經登入了;//否則說明是第一次登入成功      if(null != context.getSession().get("uName")){        msg = this.userName + ":你已經登入過了!!!";      }else{        context.getSession().put("uName", this.userName);      }            returnthis.SUCCESS;    }else{      msg = "登入失敗,使用者名稱或密碼錯";      returnthis.ERROR;    }  }    public String regist() throws Exception{    //將使用者名稱,密碼添加到資料庫中    //...    msg = "註冊成功。";    returnthis.SUCCESS;  }}

2.       struts.xml檔案:沒有什麼變化,跟以前一樣配置

<!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>  <package name="my" extends="struts-default" namespace="/manage">  <!-- 定義處理請求URL為login.action的Action -->    <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">    <!-- 定義處理結果字串和資源之間的映射關係 -->      <result name="success">/success.jsp</result>      <result name="error">/error.jsp</result>    </action>  </package></struts>

3.       頁面:
index.jsp

<%@ page language="java" pageEncoding="UTF-8"%><html><head>  <title>使用者登入頁面</title></head> <body> <h2>使用者入口</h2> <hr>  <form action="manage/userOpt!login.action" method="post">  <table border="1">     <tr>       <td>使用者名稱:</td>       <td><input type="text" name="userName"/></td>     </tr>     <tr>       <td>密碼:</td>       <td><input type="password" name="password"/></td>     </tr>     <tr>       <td colspan="2">         <input type="submit" value=" 確定 "/>       </td>     </tr>  </table>  </form></body></html>

regist.jsp

<%@ page language="java" pageEncoding="UTF-8"%><html><head>  <title>使用者註冊頁面</title></head> <body> <h2>使用者註冊</h2> <hr>  <form action="manage/userOpt!regist.action" method="post">  <table border="1">     <tr>       <td>使用者名稱:</td>       <td><input type="text" name="userName"/></td>     </tr>     <tr>       <td>密碼:</td>       <td><input type="password" name="password"/></td>     </tr>     <tr>       <td colspan="2">         <input type="submit" value=" 註冊 "/>       </td>     </tr>  </table>  </form></body></html>

現在就可以使用sturts。

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.