我是JSP、Servlet和Java Bean领域的初学者。我正在处理一个JSP文件,其中包含一个创建用户组的HTML表单。我正在尝试使用MVC开发此表单。
此表单包含2个文本框和2个选择框。在这些选择框中,您可以选择多个值,一个包含用户的选择框。每当第一次调用JSP页时,此用户选择框将填充从数据库中提取的用户名。
无论何时提交表单,它都会调用表单的action方法中提到的servlet。该servlet检查数据库中是否已经存在该用户。
- 如果数据库中不存在组名,则保存组名和其他表单数据,并将值为“Success”的回复属性发送回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"> </th>
<th width="280" scope="col"> </th>
</tr>
<tr>
<th nowrap scope="col"> </th>
<th scope="col"> </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> </td>
<td> </td>
</tr> <tr>
<td> </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>
以下是我的具体问题:
1.我已经编写了一些代码,用于在第一次调用JSP页面以及将控件从servlet发送回JSP页面时填充用户选择框,这些代码包含Java代码,我认为这种填充用户选择框的解决方案不适合MVC模式,也就是说,这不是一个正确的代码设计。我读到过在JSP中编写Java代码不是一个好的做法。我如何解决这个问题,避免在我的JSP中编写Java代码,以便遵守MVC模式规则?
1.关于JSP页面的userbean标记(第2行和第3行),使用这个标记创建的对象只用于填充userlist框。我认为这也不是最优的编码。我如何使这段编码简短而最优呢?
1.如果输入的组名已存在于数据库中,则servlet会将所有数据设置为请求对象的属性,并使用请求分配器将其转发到JSP页面,但在此处,当JSP页面显示在浏览器中时,如果您检查地址栏中的URL,则不会在URL中显示JSP页面名称。它会显示servlet名称。那么,如何在地址栏中重新获取JSP名称呢?
1.有没有在互联网上的链接,提供有关代码设计的信息与真实的的文字问题和例子,如上述初学者的信息?
1条答案
按热度按时间6fe3ivhb1#
1.您可以使用MVC框架作为Struts,并将所有属性与模型绑定。然后,当您显示组合框时,将自动从模型中设置值。
1.同上。
1.您可以使用重定向代替转发,但这取决于要求。
1.搜索Struts或Spring MVC。