1

私は JSP、サーブレット、および Java Bean の分野の初心者です。ユーザーのグループを作成する html フォームを含む jsp ファイルに取り組んでいます。MVC モデルを使用してこのフォームを開発しようとしています。このフォームには、2 つのテキスト ボックスと 2 つの選択ボックスが含まれています。これらの選択ボックスから、ユーザーを含む 1 つの選択ボックスで複数の値を選択できます。jsp ページに最初の呼び出しが行われるたびに、このユーザー選択ボックスは次のように入力されます。

データベースから取得したユーザー名。フォーム機能は、フォームが送信されるたびに、フォームのアクションメソッドで言及されているサーブレットが呼び出されるようなものです。このサーブレットは、ユーザーがデータベースに既に存在するかどうかをチェックします。グループの場合

name がデータベースに存在しない場合、グループ名とその他のフォーム データがデータベースに保存され、応答属性が値「Success」で jsp ページに送り返されます。グループ名がすでに存在する場合は、返信属性が送信されます

値「重複」でjspページに戻り、残りのフォームデータもjspページに送り返されるため、このフォームデータはフォームに埋め戻されます。

これが私のJSPファイルコードです。

<%@ page import="java.util.*" %>
<jsp:useBean id="objbean" class="com.techspeed.user.CreateUserBean" />
<jsp:useBean id="objdao" class="com.techspeed.user.CreateUserDAO" />
<head>
   <title>Untitled Document</title>
</head>

<body>
   <%
      if (null != request) {
         if (request.getAttribute("reply") != null) {
            if (request.getAttribute("reply").toString().equalsIgnoreCase("Duplicate")) {
               out.println("The entered Username is already exists!");
            } else if (request.getAttribute("reply").toString().equalsIgnoreCase("failed")) {
               out.println("User creation process terminated due to technical fault. Please try again later!");
            } else {
               out.println("The User created successfully!");
            }
            //out.println(request.getAttribute("reply"));
         }
      }
   %>
   <form name="creategroupform" method="post" action="Creategroup">
      <table width="422" border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#FFFFFF">
         <tr>
            <th width="128" nowrap scope="col">&nbsp;</th>
            <th width="280" scope="col">&nbsp;</th>
         </tr>
         <tr>
            <th nowrap scope="col">&nbsp;</th>
            <th scope="col">&nbsp;</th>
         </tr>
         <tr>
            <th nowrap scope="col">
         <div align="left" class="grayBodyText style14">
            Group Name : 
         </div>
         </th>
         <th scope="col"><div align="left">
            <input type="text" name="username" value="">
         </div></th>
         </tr>
         <tr>
            <td>Description<span class="style14">: </span></td>
            <!--I used JSTL to fill the value sent by servlet  -->
            <td><input type="text" name="lname" value=${lname}></td> 
         </tr>
         <tr>
            <td><span class="style14">Group Type :</span></td>
            <td>
               <select name="role">
                  <!-- 
                  Here the JSTL is used to show a value as selected in 
                  select box which was selected by the user when form is submitted.
                  -->
                  <option value="Beginner" ${role == Beginner ? 'selected' : ''}>
                     Beginner
                  </option> 
                  <option value="Moderate" ${role == Moderate ? 'selected' : ''}>
                     Moderate
                  </option>
                  <option value="Expert" ${role == Expert ? 'selected' : ''}>
                     Expert
                  </option>
               </select>                          </td>
         </tr>
         <tr>
            <td><span class="style14">List Of Users: </span></td>

            <!-- Major problem starts here below -->
            <td><select name="groups" size="5" multiple>
                  <%
                     //objbean=objdao.createHtmlOptionList(objbean);
                     //out.println(objbean.getHtmGroupOptionList());
                     System.out.println("REached here!");

                     // This calls to getGroupList of method of DAO class which 
                     // retrives username and there unique userids from 
                     // database and stores in MAP collection of bean class.
                     if (request.getAttribute("reply") == null
                             || request.getAttribute("reply").toString().equalsIgnoreCase("success")) {
                        objdao.getUserList(objbean);
                        System.out.println("REached inside if!");
                  %>
                  <jsp:getProperty name="objbean" property="htmUseroptionList" />
                  <!-- 
                  In this JSTL the htmGroupOptionList property is a string 
                  variable name in bean class   which contains the 
                  preconstructed html code of <Options> haveing 
                  userid as value and username as name in Map collection Which 
                  gets constructed in above call to function getGroupList of 
                  DAO class getGroup. As per my thought this if block is gets 
                  executed whenever there is a fresh call given to this jsp 
                  page and when the reply attribute of the 
                  servlet contains value as "success" 
                  -->
                  <%
                     } else {
                        /* As per my thought this else block is get invoked 
                         whenever the reply attribute send by the servlet 
                         contains value as "Duplicate" of "Failed"  As per my 
                         thought this if block is gets executed whenever there 
                         is a fresh call given to this jsp page and when the 
                         reply attribute of the servlet contains value as "success" */
                        if (!(request.getAttribute("reply").toString().equalsIgnoreCase("success"))) {
                           objdao.getUserList(objbean);
                           // This Map have the list of userid as key and 
                           // username as value. This map is get filled from Database.
                           Map<Integer, String> userMap
                                   = (Map<Integer, String>) objbean.getUserList();
                           // This list contains the list of selected username 
                           // by the user before submitting the form.
                           ArrayList<String> lstSelUser
                                   = (ArrayList<String>) request.getAttribute("selUser");
                           String strSel = "";
                           for (Integer key : UserMap.keySet()) {

                              for (int i = 0; i < lstSelUser.size(); i++) {
                                 if (key.toString().equalsIgnoreCase(lstSelUser.get(i))) {
                                    strSel = "selected=\"selected\"";
                                    break;
                                 } else {
                                    System.out.println("not selected");
                                    strSel = "";
                                 }
                              }
                              out.println("<option value=" + key.toString() + " "
                                      + strSel + ">" + userMap.get(key) + "</option>");
                           }
                        }
                     }
                  %>                             
               </select></td>
         </tr>
         <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
         </tr>              <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="submit" value="Submit">
               <input type="reset" name="reset" value="Reset">
               <!--<input type="reset" name="" value="Cancel"> -->
            </td>
         </tr>
      </table>
   </form>

</body>
</html>

jspページの上記のコードと言及されたコメントによると、私はそれに関して次の疑問と質問があります。

  • ご覧のとおり、jsp ページが初めて呼び出されたとき、およびコントロールがサーブレットから jsp ページに送り返されたときに、ユーザー選択ボックスを埋めるコードをいくつか書きました。このコードには Java コードが含まれています。ユーザー選択ボックスを埋めるこのソリューションは、MVC パターンに従って適切なコーディングではないと思います。つまり、適切なコード設計ではありません。javaコードをjspで書くのは良い習慣ではないことを読みました.このコーディングを短く最適化するにはどうすればよいですか?

  • jsp コードの行番号 2 と 3 を見つけてください。つまり、jsp の userbean タグです。このタグを使用して作成されたオブジェクトは、ユーザーリスト ボックスを埋めるためにのみ使用されます。これも最適なコーディングではないと思います。このコーディングを短く最適化するにはどうすればよいですか?

  • 入力されたグループ名がデータベースに既に存在する場合、サーブレットはすべてのデータを属性としてリクエスト オブジェクトに設定し、リクエスト ディスパッチャを使用してそれを jsp ページに転送しますが、アドレス バーの URL を確認すると、jsp ページが事前入力されたフォームでブラウザに表示されます。 URLにjspページ名が表示されていません。サーブレット名が表示されます。では、アドレスバーに jsp 名を戻すにはどうすればよいでしょうか?

  • 初心者向けの実際の単語の問題と例を含むコード設計情報に関する情報を提供するインターネット上のリンクはありますか?

この問題を解決するには、専門家からのガイダンスと提案が必要です。この問題で私を導いてください。

ありがとうございました

4

1 に答える 1

0
  1. MVC フレームワークを Struts として使用し、すべてのプロパティをモデルにバインドできます。次に、コンボボックスを表示すると、モデルから値が自動的に設定されます。
  2. 上記のように。
  3. 転送の代わりにリダイレクトを使用できますが、要件によって異なります。
  4. Struts または Spring MVC を検索します。
于 2012-09-27T13:20:54.570 に答える