0

ユーザーがjspで選択したカテゴリに基づいて一連の値を取得する必要があるjdoクエリを作成しようとしています...私のクエリは次のようになります

Query query2 = pm.newQuery("select from " + ProductDB.class.getName()+ "where pCategory = " document.getElementById("cname").text);

jsp ページには、動的なドロップダウン ボックスがあり、タグには id タグを「cname」として指定しています。したがって、上記のクエリを実行すると、ユーザーが選択したカテゴリが取得されることを期待しています。

しかし、私はこのエラーが発生しています:

Syntax error on token "document", delete this token

私のselectタグは次のようになります:

<select name = "cname" id="cname">
.
.
.
</select>

ここで何が欠けていますか?

更新

jsp ファイルのコード全体を以下に示します。

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.jdo.Query"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="java.net.*"%>
<%@ page import="javax.servlet.http.HttpServletRequest"%>
<%@ page import="com.nerdy.needs.*"%>
<html>
<head>
<title>Product Inventory</title>
<META HTTP-EQUIV="Refresh" CONTENT="450">
<link rel="stylesheet" href="login.css" type="text/css" />
</head>
<h1 align="center">Product Inventory</h1>
<body>
<form>
<table>
    <tr>
        <td>View</td>
        <td><select name="cname" id="cname">
            <option value="all">All</option>
            <%
                PersistenceManager pm = PMF.get().getPersistenceManager();
                Query query = pm.newQuery("select cname from "
                        + CategoryDB.class.getName());
                List<String> categories = new ArrayList<String>();
                categories = (List<String>) query.execute();
                String[] c = categories.toArray(new String[categories.size()]);
                for (int i = 0; i < c.length; i++) {
                    String s = c[i];
            %>
            <option value="<%=s%>"><%=s%></option>
            <%
                }
            %>
        </select></td>
        <td>Products</td>
    </tr>
</table>
</form>
<%
    if (document.getElementById("cname").value == "all") {
        PersistenceManager pm1 = PMF.get().getPersistenceManager();
        Query query1 = pm1.newQuery("select * from "
                + ProductDB.class.getName());
        List<ProductDB> prods1 = (List<ProductDB>) query1.execute();
        if (prods1.isEmpty()) {
%>
<table class="items">
    <tr>
        <th class="main">Image</th>
        <th class="main">Category</th>
        <th class="main">Name</th>
        <th class="main">Price</th>
        <th class="main">Description</th>
    </tr>
    <tr class="lightBlue">
        <td class="actions" colspan=100%>
        <p>No items were found.</p>
        </td>
    </tr>
</table>
<%
    } else {
%>
<table class="topics">
    <tr>
        <th class="main">Image</th>
        <th class="main">Category</th>
        <th class="main">Name</th>
        <th class="main">Price</th>
        <th class="main">Description</th>
    </tr>
    <%
        for (ProductDB p : prods1) {
    %>
    <tr>
        <td>
        <p><b> <img width="100" height="100"
            src="http://localhost:8888/serve?id= <%=p.getProductImage()%>">
        </b></p>
        </td>
        <td>
        <p><b><%=p.getProductCategory()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductName()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductPrice()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductDescription()%></b></p>
        </td>
    </tr>
    <%
        }
    %>
</table>
<%
    pm1.close();
        }
    } else {
        PersistenceManager pm2 = PMF.get().getPersistenceManager();
        Query query2 = pm.newQuery("select * from "
                + ProductDB.class.getName() + "where pCategory = "
                + document.getElementById("cname").value);
        List<ProductDB> prods2 = (List<ProductDB>) query2.execute();
        if (prods2.isEmpty()) {
%>
<table class="items">
    <tr>
        <th class="main">Image</th>
        <th class="main">Category</th>
        <th class="main">Name</th>
        <th class="main">Price</th>
        <th class="main">Description</th>
    </tr>
    <tr class="lightBlue">
        <td class="actions" colspan=100%>
        <p>No items were found.</p>
        </td>
    </tr>
</table>
<%
    } else {
%>
<table class="topics">
    <tr>
        <th class="main">Image</th>
        <th class="main">Category</th>
        <th class="main">Name</th>
        <th class="main">Price</th>
        <th class="main">Description</th>
    </tr>
    <%
        for (ProductDB p : prods2) {
    %>
    <tr>
        <td>
        <p><b> <img width="100" height="100"
            src="http://localhost:8888/serve?id= %=p.getProductImage()%>">
        </b></p>
        </td>
        <td>
        <p><b><%=p.getProductCategory()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductName()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductPrice()%></b></p>
        </td>
        <td>
        <p><b><%=p.getProductDescription()%></b></p>
        </td>
    </tr>
    <%
        }
    %>
</table>
<%
    pm2.close();
        }
    }
%>
</body>
</html>

2 つの場所で「ドキュメントを解決できません」というエラーが表示されます。1 つは if ステートメントです。

if(document.getElementById("cname").value=="all")

もう1つはクエリステートメントで

Query query2 = pm.newQuery("select * from " + ProductDB.class.getName()+ "where pCategory = " + document.getElementById("cname").value);

誰が何が間違っているかを理解するのを手伝ってくれますか?

4

3 に答える 3

1

具体的な問題は、Java/JSP と JavaScript を混在させていることです。documentそれらが同期して実行されること、および JavaScript のオブジェクト変数が JSPスクリプトレットコードにも存在することを期待しているようです。

これは間違っています。Java/JSP は HTML コード ジェネレーターです。HTTP 要求時に Web サーバーで実行され、HTML/JS コードを生成し、HTTP 応答として Web ブラウザーに送り返します。Web ブラウザーが取得するのは、プレーンな HTML/JS コードだけです。Web ブラウザーでページを右クリックし、[ソースの表示]を実行して、自分で表示します。

あなたの具体的な機能要件は、提出された値を取得する必要があるようです

<select name="cname">

Java/JSP 側で。

でリクエストパラメータとして取得する必要がありますHttpServletRequest#getParameter()。だから、交換

<%
    if (document.getElementById("cname").value == "all") {
        // ...
    }
%>

<%
    if ("all".equals(request.getParameter("cname"))) {
        // ...
    }
%>

とはいえ、JSP ファイルに Java コードを記述することは、お勧めできませ。それにも取り組みます。この問題はJDOとは無関係であることに注意してください。

于 2012-02-16T14:36:39.660 に答える
0

この方法を試してください:-

var i = document.getElementById("cname").selectedIndex;

document.getElementById("cname").options[i].text;

また

document.getElementById("cname").value

アップデート:

列名もありません。*またはのいずれかである必要がありますspecific column names

Query query2 = pm.newQuery("select * from " + ProductDB.class.getName()+ "where pCategory = " + document.getElementById("cname").value); 

エラーの更新に従って

あなたのコードは次のようになります:-

if(document.getElementById("cname").value=="all") 
{ 
 <%
 PersistenceManager pm1 = PMF.get().getPersistenceManager(); 
 Query query1 = pm1.newQuery("select * from " + ProductDB.class.getName()); 
 List<ProductDB> prods1 = (List<ProductDB>) query1.execute(); 
%>
}

JSP タグはタグ<%%>の間で宣言する必要があり、その他の html タグは外側にする必要があります。

あなたのコードに従って:

問題はdocument.getElementById("cname").value、JSP タグ内での呼び出しです。それは間違いです。

ここでは、cname値をクエリ文字列として渡し、パラメーターを介して値を取得するか、document.getElementById("cname").value値を JSP 変数に割り当てて処理することができます。

于 2012-02-16T05:17:40.223 に答える
0

の前のプラス記号を忘れましたdocument.getElementById

正しいコードは

Query query2 = pm.newQuery("select from " + ProductDB.class.getName() + "where pCategory = " + document.getElementById("cname").text);

また、構文エラーは修正されますが、コードはまだ機能しません。W3C DOM 仕様によると、要素には属性<select/>がありません。代わりに、 またはを組み合わせてtext使用​​する必要があります。valueselectedIndexoptions

于 2012-02-16T05:14:43.343 に答える