|
|
|
วอนผู้รู้สอบถามเรื่องการทำ dropdown list กับ jsp ajax ดึงข้อมูลจาก database ที่มีความสัมพันธ์กัน จะต้องทำอย่างไร ค่ะช่วยดูโค้ดให้หน่อยค่ะ pleasessss |
|
|
|
|
|
|
|
หน้า search.jsp
Code (JavaScript)
<script type="text/javascript">
var xmlReq ;
function gotoDDL_G () {
document.frmasset.method = "post";
document.frmasset.action = "/${strPath}/servlet/SearchServlet?action=DDLG";
document.frmasset.submit();
}
function selectGroup() {
var selected_group = document.getElementById("ddlGroup").value;
alert(selected_group);
var complete_url = "/${strPath}/servlet/SelectionServlet?action=TDA&DURART_GROUP_ID="+selected_group;
if(window.XMLHttpRequest){
xmlReq = new XMLHttpRequest();
}else {
xmlReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlReq.open("GET",complete_url,true);
xmlReq.send(null);
}
function callback(){
if(xmlReq.readyState === 4){
if(xmlReq.status === 200) {
var list_type = document.getElementById("ddlType");
xmlReq.open("GET","/${strPath}/js/Searching/ddlTypeDurArt.jsp");
}
}
}
</script>
Code
<ul class="smenu">
<li><a onclick="javascrip:gotoDDL_G();">ค้นหาครุภัณฑ์</a></li>
</ul>
<dl class="dl-horizontal">
<dt>กลุ่มครุภัณฑ์หลัก </dt>
<dd>
<select class="input-xlarge" name="ddlGroup" id ="ddlGroup" onchange="selectGroup();">
<option value="0">(ทั้งหมด)</option>
<c:choose>
<c:when test= "${lst>0}">
<c:forEach var="i" items="${result}" varStatus="status">
<option value="${i.DURART_GROUP_ID}">${i.DURART_GROUP_ID} ${i.DURART_GROUP}</option>
</c:forEach>
</c:when>
</c:choose>
</select>
</dd>
<button class="btn btn-success th_baijam center" id="button" type="submit" onclick="javascript:gotoSearch();" >
<b>แสดงข้อมูลตามเงื่อนไขที่ระบุ</b>
</button>
</dd>
</dl>
หน้า servlet ; SearchServlet.java
Code (Java)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rmuti.surin.DurableArticles.servlet;
import com.intcom.utils.StringUtil;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rmuti.surin.DurableArticles.stringUtil;
import java.util.*;
import com.rmuti.surin.DurableArticles.query.*;
import javax.servlet.RequestDispatcher;
/**
*
* @author Asus-A42J
*/
public class SearchServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("Run Search Servlet");
String[] values;
//Text Search
String textSeh = "";
values = request.getParameterValues("textSeh");
//
if(values!=null) {
textSeh = new String(values[0]);
}
System.out.println("value textSeh :"+textSeh);
textSeh = StringUtil.convertToThai(textSeh);
System.out.println("textSeh Convert :"+textSeh);
//Action
String strAction = "";
values = request.getParameterValues("action");
if(values != null) {
strAction = new String(values[0]);
}
strAction = com.rmuti.surin.DurableArticles.stringUtil.convertToThai(strAction);
//DropDown List GroupID
String DURART_GROUP_ID = "";
values = request.getParameterValues("DURART_GROUP_ID");
if(values!=null) {
DURART_GROUP_ID = new String(values[0]);
}
System.out.println("value DURART_GROUP_ID :"+DURART_GROUP_ID);
textSeh = StringUtil.convertToThai(DURART_GROUP_ID);
System.out.println("DURART_GROUP_ID Convert :"+DURART_GROUP_ID);
//Action Search
if(strAction.equals("SEH")){
try {
/* TODO output your page here. You may use following sample code. */
System.out.println("Sevlet Action SEH");
List lstResult = new ArrayList();
qry_search qrySeh = new qry_search ();
lstResult = qrySeh.selectState1(textSeh);
request.setAttribute("result",lstResult);
request.setAttribute("lst",Integer.toString(lstResult.size()));
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/ResultSeh.jsp");
dispatcher.forward(request, response);
System.out.println("end Servlet");
} catch (Throwable e) {
out.close();
System.out.println("error : "+e.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/search.jsp");
}
}
//Action Dropdown List Group DurArt
if(strAction.equals("DDLG")) {
try {
System.out.println("Servlet Action DDL_G");
List lstResult = new ArrayList();
qry_search qrySeh = new qry_search();
lstResult = qrySeh.selectGroupDA();
request.setAttribute("result",lstResult);
request.setAttribute("lst",Integer.toString(lstResult.size()));
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/search.jsp");
dispatcher.forward(request, response);
System.out.println("end Servlet");
}catch (Throwable e) {
out.close();
System.out.println("error : "+e.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/search.jsp");
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
หน้า servlet ; selectionServlet.java
Code (Java)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rmuti.surin.DurableArticles.servlet;
import com.intcom.utils.StringUtil;
import com.rmuti.surin.DurableArticles.query.qry_selections;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
/**
*
* @author Asus-A42J
*/
public class SelectionServlet extends HttpServlet {
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("Run Selections Servlet");
String [] values;
//Selected Group
//DropDown List GroupID
String DURART_GROUP_ID = "";
values = request.getParameterValues("DURART_GROUP_ID");
if(values!=null) {
DURART_GROUP_ID = new String(values[0]);
}
System.out.println("value DURART_GROUP_ID :"+DURART_GROUP_ID);
DURART_GROUP_ID = StringUtil.convertToThai(DURART_GROUP_ID);
System.out.println("DURART_GROUP_ID Convert :"+DURART_GROUP_ID);
//Selection Type DurArt
//Action
String strAction = "";
values = request.getParameterValues("action");
if(values != null) {
strAction = new String(values[0]);
}
strAction = com.rmuti.surin.DurableArticles.stringUtil.convertToThai(strAction);
/*
* DA = Durable Articles
* T = Type
* BT = Budget type
* OF = office
* C = campus
* ORG = organize
* sORG = sub oragnize
* PG = programs
* ST = section
*/
//Action Selection Type Durable Articles
if (strAction.equals("TDA")){
try {
/* TODO output your page here. You may use following sample code. */
System.out.println("Servlet Action TDA");
List lstResult = new ArrayList();
qry_selections ddl_type = new qry_selections();
lstResult = ddl_type.select_TypeDA(DURART_GROUP_ID);
request.setAttribute("result",lstResult);
request.setAttribute("lst",Integer.toString(lstResult.size()));
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/ddlTypeDurArt.jsp");
dispatcher.forward(request, response);
System.out.println("end Servlet");
} catch (Throwable e) {
out.close();
System.out.println("error : "+e.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/Search.jsp");
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
หน้า ddlTypeDurArt.jsp
Code
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ddlType DurArt Page</title>
</head>
<body>
<select>
<c:choose>
<c:when test= "${lst>0}">
<c:forEach var="i" items="${result}">
<option value="${i}">${i.DURART_TYPE_ID} ${i.DURART_TYPE}</option>
</c:forEach>
</c:when>
</c:choose>
</select>
</body>
</html>
อยากให้ dropdownlist หน้า ddlTypeDurArt.jsp ไปเด้งอยู่หน้า search.jsp เวลาเลือกครุภัณหลัก แล้วให้ เด้งครุภัณย่อยของครุภัณหลักนั้นๆ ต้องทำยังไงค่ะ มาแนวนี้ถูกหรือเปล่า ค่ะ รบกวนช่วยด้วยน่ะค่ะ ไม่ค่อยรู้เรื่อง ajax เท่าไหร่ค่ะ ช่วยด้วยน่ะ ค่ะ ขอบคุณค่ะ
Tag : Oracle, Ajax, JSP
|
|
|
|
|
|
Date :
2014-01-23 00:05:59 |
By :
งงมว๊าก |
View :
1796 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากช่วยน่ะครับ แต่ Code เยอะเกินไปครับ
|
|
|
|
|
Date :
2014-01-23 10:15:19 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
โห่พี่ช่วยหน่อยเถอะ TT
ตัดออกบ้างก็ได้ค้ะช่วยหน่อยค้ะ
หน้า search.jsp กดlink <li><a onclick="javascrip:gotoDDL_G();">ค้นหาครุภัณฑ์</a></li>
Code (JavaScript)
<script type="text/javascript">
var xmlReq ;
function gotoDDL_G () {
document.frmasset.method = "post";
document.frmasset.action = "/${strPath}/servlet/SearchServlet?action=DDLG";
document.frmasset.submit();
}
function selectGroup() {
var selected_group = document.getElementById("ddlGroup").value;
alert(selected_group);
var complete_url = "/${strPath}/servlet/SelectionServlet?action=TDA&DURART_GROUP_ID="+selected_group;
xmlReq.onreadystatechange = callback;
xmlReq.open("GET",complete_url,true);
xmlReq.send(null);
}
function callback(){
if(xmlReq.readyState === 4){
if(xmlReq.status === 200) {
var list_type = document.getElementById("ddlType");
xmlReq.open("GET","/${strPath}/js/Searching/ddlTypeDurArt.jsp");
}
}
}
</script>
<ul class="smenu">
<li><a onclick="javascrip:gotoDDL_G();">ค้นหาครุภัณฑ์</a></li>
</ul>
<i class="icon-search icon-white"></i> <b><font color="#fff">การค้นหาครุภัณฑ์</font></b>
<br/>กลุ่มครุภัณฑ์หลัก
<select class="input-xlarge" name="ddlGroup" id ="ddlGroup" onchange="selectGroup();">
<option value="0">(ทั้งหมด)</option>
<c:choose>
<c:when test= "${lst>0}">
<c:forEach var="i" items="${result}" varStatus="status">
<option value="${i.DURART_GROUP_ID}">${i.DURART_GROUP_ID} ${i.DURART_GROUP}</option>
</c:forEach>
</c:when>
</c:choose>
</select>
หน้า searchServlet.java
Code (Java)
if(strAction.equals("DDLG")) {
try {
System.out.println("Servlet Action DDL_G");
List lstResult = new ArrayList();
qry_search qrySeh = new qry_search();
lstResult = qrySeh.selectGroupDA();
request.setAttribute("result",lstResult);
request.setAttribute("lst",Integer.toString(lstResult.size()));
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/search.jsp");
dispatcher.forward(request, response);
System.out.println("end Servlet");
}catch (Throwable e) {
out.close();
System.out.println("error : "+e.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/search.jsp");
}
}
หน้า qry_search.java
Code (Java)
public List selectGroupDA() throws Exception {
System.out.println("Run qry_search : Select Group DA") ;
myConnection myConn = new myConnection();
Connection connec = myConn.getConNotPoll();
String strSql = "select * from group_durart";
Statement stmt = connec.createStatement();
ResultSet rst = stmt.executeQuery(strSql);
List listGDA = new ArrayList();
groupDA_bean objResult;
while(rst.next()){
objResult = new groupDA_bean();
objResult.setDURART_GROUP_ID(rst.getString("DURART_GROUP_ID"));
objResult.setDURART_GROUP(rst.getString("DURART_GROUP"));
listGDA.add(objResult);
System.out.println(listGDA);
}
System.out.println("select Passes");
rst.close();
connec.close();
return listGDA;
}
หน้า selectionservlet.java
Code (Java)
//Action Selection Type Durable Articles
if (strAction.equals("TDA")){
try {
/* TODO output your page here. You may use following sample code. */
System.out.println("Servlet Action TDA");
List lstResult = new ArrayList();
qry_selections ddl_type = new qry_selections();
lstResult = ddl_type.select_TypeDA(DURART_GROUP_ID);
request.setAttribute("result",lstResult);
request.setAttribute("lst",Integer.toString(lstResult.size()));
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/ddlTypeDurArt.jsp");
dispatcher.forward(request, response);
System.out.println("end Servlet");
} catch (Throwable e) {
out.close();
System.out.println("error : "+e.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Searching/Search.jsp");
}
}
หน้า qry_selections.java
Code (Java)
public List select_TypeDA(String DURART_GROUP_ID) throws Exception {
System.out.println("Run qrySelection : select_typeDA");
myConnection myConn = new myConnection();
Connection connec = myConn.getConNotPoll();
String strSql = "select td.durart_group_id,td.durart_type_id, td.durart_type from group_durart gd \n" +
"join type_durart td on (gd.durart_group_id = td.durart_group_id) \n" +
"where td.durart_group_id ='"+DURART_GROUP_ID+"'";
Statement stmt = connec.createStatement();
ResultSet rst = stmt.executeQuery(strSql);
List listTDA = new ArrayList() ;
typeDA_bean objResult;
while (rst.next()){
objResult = new typeDA_bean();
objResult.setDURART_TYPE_ID(rst.getString("DURART_TYPE_ID"));
objResult.setDURART_GROUP_ID(rst.getString("DURART_GROUP_ID"));
objResult.setDURART_TYPE(rst.getString("DURART_TYPE"));
listTDA.add(objResult);
System.out.println(listTDA);
}
System.out.println(strSql);
System.out.println("select Passes");
rst.close();
connec.close();
return listTDA ;
}
หน้า ddlTypeDurArt.jsp อยากให้หน้านี้ไปโผล่อยู่หน้า search.jsp ค่ะ
Code (JavaScript)
<select>
<c:choose>
<c:when test= "${lst>0}">
<c:forEach var="i" items="${result}">
<option value="${i}">${i.DURART_TYPE_ID} ${i.DURART_TYPE}</option>
</c:forEach>
</c:when>
</c:choose>
</select>
ช่วยหนูหน่อยเถอะค่ะ ไม่รู้จะถามใครแล้วTT
|
|
|
|
|
Date :
2014-01-23 18:07:18 |
By :
งงมว๊าก |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|