1

我是 JSP、Servlet 和 Java bean 领域的初学者。我正在处理一个包含创建用户组的 html 表单的 jsp 文件。我正在尝试使用 MVC 模型开发这种形式。此表单包含 2 个文本框和 2 个选择框。在这些选择框中,您可以选择多个值,一个包含用户的选择框。每当第一次调用 jsp 页面时,此用户选择框将被填充

从数据库中获取的用户名。表单功能是这样的,每当提交表单时,都会调用表单的 action 方法中提到的 servlet。这个 servlet 检查用户是否已经存在于数据库中。如果组

名称在数据库中不存在,则组名和其他表单数据将保存在数据库中,并且回复属性将发送回值为“成功”的 jsp 页面。如果组名已存在,则发送回复属性

返回值为“duplicate”的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 页面以及将控件从 servlet 发送回 jsp 页面时,我编写了一些代码来填充用户选择框。此代码包含 java 代码。我认为这种填充用户选择框的解决方案不适合按照 MVC 模式进行编码,即它不是适当的代码设计。我已经读过在 jsp 中编写 java 代码不是一个好习惯。如何使这种编码简短而优化?

  • 请在jsp代码中找到第2行和第3行,即jsp的userbean标签。使用此标签创建的对象仅用于填充用户列表框。我认为这也不是最佳编码。如何使这种编码简短而最佳?

  • 如果输入的组名已经存在于数据库中,则 servlet 将所有数据设置为请求对象的属性,并使用请求调度程序将其转发到 jsp 页面,但在这里,如果您检查地址栏中的 url,则当 jsp 页面显示时,浏览器带有预填充表单它没有在 url 中显示 jsp 页面名称。它显示 servlet 名称。那么如何让他的jsp名称回到地址栏中呢?

  • 互联网上是否有任何链接提供有关代码设计信息的信息以及真实的单词问题和像上面这样的初学者示例?

我需要一些专家的指导和建议来解决这个问题。请指导我解决这个问题。

谢谢你

4

1 回答 1

0
  1. 您可以将 MVC 框架用作 Struts 并将所有属性与模型绑定。然后,当您显示组合框时,该值将自动从您的模型中设置。
  2. 如上。
  3. 您可以使用重定向而不是转发,但这取决于要求。
  4. 搜索 Struts 或 Spring MVC。
于 2012-09-27T13:20:54.570 回答