我的JSP中有一个依赖项下拉列表。我有一个json servlet,我填充第二个下拉列表。根据第一个下拉列表的选择将确定第二个下拉列表。我在jsonServlet类中有硬编码的值,但我希望能够从我的DAO方法调用查询。我该怎么做呢?
JsonServlet:
package master.service;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import master.dao.MasterDataDao;
import master.dto.SiteDto;
import com.google.gson.Gson;
/**
* Servlet implementation class JsonServlet
* @param <E>
*/
public class JsonServlet<E> extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
MasterDataDao masterDataDao = new MasterDataDao();
String divisionIdName = request.getParameter("divisionIdName");
List<String> list = new ArrayList<String>();
List<SiteDto> site = new ArrayList<SiteDto>();
String json = null;
if (divisionIdName.equals("33") || divisionIdName.equals("36")) {
try {
site.equals(masterDataDao.getAllJJSites());
for (Iterator<SiteDto> iter = site.iterator(); iter.hasNext(); ){
SiteDto element = iter.next();
list.addAll(-1, element);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Iterator<SiteDto> iter = site.iterator(); iter.hasNext(); ){
SiteDto element = iter.next();
}
} else if (divisionIdName.equals("Select Division")) {
list.add("Select Site");
} else {
try {
site.equals(masterDataDao.getAllSites());
for (Iterator<SiteDto> iter = site.iterator(); iter.hasNext(); ){
SiteDto element = iter.next();
list.addAll(-1, element);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
json = new Gson().toJson(list);
response.setContentType("application/json");
response.getWriter().write(json);
}
}
基于我的JsonServlet中的divisionID选择,如果divisionID是33或36,我想在我的MasterDataDao类中调用此方法:
public List<SiteDto> getAllJJSites() throws IOException, ClassNotFoundException, SQLException {
List<SiteDto> siteDtoList = new ArrayList<SiteDto>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "Select Distinct Name, Id From Addtl_Type Where Addtl_Type.is_active = '1' And Data_Field_Id = 3050 Order By Name";
con = getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
// System.out.println("&*******" + rs.getFetchSize());
while (rs.next()) {
SiteDto siteDto = new SiteDto();
siteDto.setId(rs.getString("Id"));
siteDto.setName(rs.getString("Name"));
siteDtoList.add(siteDto);
}
} finally {
cleanUp(con, ps, rs);
}
return siteDtoList;
}
否则如果它是另一个选择值(除了Select Division),我想用MasterDataDao类调用这个方法:
public List<SiteDto> getAllSites() throws IOException, ClassNotFoundException, SQLException {
List<SiteDto> siteDtoList = new ArrayList<SiteDto>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
String query = "Select Distinct Name, Id From Addtl_Type Where Addtl_Type.is_active = '1' And Data_Field_Id = 105 Order By Name";
con = getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
// System.out.println("&*******" + rs.getFetchSize());
while (rs.next()) {
SiteDto siteDto = new SiteDto();
siteDto.setId(rs.getString("Id"));
siteDto.setName(rs.getString("Name"));
siteDtoList.add(siteDto);
}
} finally {
cleanUp(con, ps, rs);
}
return siteDtoList;
}
这可以在JsonServlet中完成吗?如果是这样的话?提前致谢。 如果需要更多信息,请告诉我。
此外,我已将JSP包含在此处。最初我从这里打电话给DAO。我引用了一个bean。 JSP:
<script>
$(document).ready(function() {
$('#divisionId').change(function(event) {
var divisionId = $("select#divisionId").val();
$.get('JsonServlet', {
divisionIdName : divisionId
}, function(response) {
var select = $('#siteId');
select.find('option').remove();
$.each(response, function(index, value) {
$('<option>').val(value).text(value).appendTo(select);
});
});
});
});
</script>
</head>
<body>
<form name="input" action="getMasterData" method="get">
<br /> <br />
<h1 align='center'>Master Data File</h1>
<br /> <br />
<table border="0" align='center'>
<tr>
<td>
<h2>Division</h2>
</td>
<td align='left'><jsp:useBean id="masterDaoUtil"
class="master.dao.util.MasterDataConstants" />
<select name="divisionId" id="divisionId">
<option>Select Division</option>
<option value="33">
<%=MasterDataConstants.DIVISION_TYPE_AUDIT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="31">
<%=MasterDataConstants.DIVISION_TYPE_CHANGE_MANAGEMENT_GLOBAL_NAME%></option>
<option value="34">
<%=MasterDataConstants.DIVISION_TYPE_DEA_MANAGEMENT_GLOBAL_NAME%></option>
<option value="35">
<%=MasterDataConstants.DIVISION_TYPE_EHS_MANAGEMENT_GLOBAL_NAME%></option>
<option value="23">
<%=MasterDataConstants.DIVISION_TYPE_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="36">
\
<%=MasterDataConstants.DIVISION_TYPE_QUALITY_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="40">
<%=MasterDataConstants.DIVISION_TYPE_NORAMCO_AUDIT_MANAGEMENT_GLOBAL_NAME%></option>
<option value="43">
<%=MasterDataConstants.DIVISION_TYPE_NORAMCO_CHANGE_MANAGEMENT_GLOBAL_NAME%></option>
<option value="41">
<%=MasterDataConstants.DIVISION_TYPE_NORAMCO_DEA_MANAGEMENT_GLOBAL_NAME%></option>
<option value="42">
<%=MasterDataConstants.DIVISION_TYPE_NORAMCO_EHS_MANAGEMENT_GLOBAL_NAME%></option>
<option value="44">
<%=MasterDataConstants.DIVISION_TYPE_NORAMCO_EVENT_MANAGEMENT_GLOBAL_NAME%></option>
</select></td>
</tr>
<tr>
<td>
<h2>Site Name</h2>
</td>
<td align='left'>
<%-- <jsp:useBean id="masterDao"
class="master.dao.MasterDataDao" />
--%>
<select name="siteId"
id="siteId">
<option>Select Site</option>
<%-- <option value="0">ALL</option>
<c:forEach items="${masterDao.allSites}" var="siteDto">
<option value="${siteDto.id}">${siteDto.name}
</option>
</c:forEach>
--%>
</select></td>
</tr>
</table>
<br /> <br />
<div style="text-align: center">
<input type="submit" value="Submit">
</div>
</form>
<%
if (request.getAttribute("message") != null) {
%>
<p>
<font size=4 color="red"> Master_Data(timestamp).xlsx and
Consistency_Check_Data(timestamp).xlsx are located under
d:/stage/MasterDataReports <%--Master_Data(timestamp).xlsx and Consistency_Check_Data(timestamp).xlsx are located under /jsc/ets/u02/tools7/apache-tomcat-7.0.55/webapps/MasterData/MasterDataReport--%>
</font>
</p>
<%
}
%>
最初在我使用JSON和AJAX之前,我将此语句用于现已注释掉的选项。
<%-- <jsp:useBean id="masterDao"
class="master.dao.MasterDataDao" />
--%>
<select name="siteId"
id="siteId">
<option>Select Site</option>
<%-- <option value="0">ALL</option>
<c:forEach items="${masterDao.allSites}" var="siteDto">
<option value="${siteDto.id}">${siteDto.name}
有没有办法可以利用这个?也许我可以从我的servlet中调用JSP函数?我知道这不是好习惯,但我想不出另一种方式。我需要获取siteDto.id值以及siteDto.name值。 提前致谢
答案 0 :(得分:0)
创建一个界面和实现DAO
,例如IMasterDataDAO
和MasterDataDAOImpl
。
如果您将来获得更多if..else
条件,请使用switch..case而不是if..else
。
使用DAO
中的界面为servlet
创建一个对象,如下所示:
IMasterDataDAO masterDAO = new MasterDataDAOImpl()
然后根据条件调用masterDAO.getAllJSites()
或masterDAO.getAllSites()
。