 |
 |
| 閲覧数:12800 |
掲示板(javaとjsp間のListの受け渡し) |
 |
| 名前 |
|
| 題名 |
javaとjsp間のListの受け渡し
|
質問内容
質問を評価する
(0ポイント)
|
お世話になります。 Eclipse3.4.2とTomcat5.5を使用しxmlから取得したデータをArrayListに格納し jspにListをforwardし一覧を表示するサーブレットを作成しようとしています。
-----------------main.java---------------------- ArrayList<String> aid = new ArrayList<String>();
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbfactory.newDocumentBuilder(); org.w3c.dom.Document doc = builder.parse(new File("log.xml")); Element root = doc.getDocumentElement(); NodeList list = root.getElementsByTagName("page");
for(int i=0; i < list.getLength() ; i++){ Element element = (Element)list.item(i); String id = element.getAttribute("id"); aid.add(id); } request.setAttribute("id",aid); RequestDispatcher rd = request.getRequestDispatcher("index.jsp"); rd.forward(request,response);
---------------------jsp--------------------------- <%@ page contentType="text/html;charset=Windows-31J" pageEncoding="Windows-31J" %> <% request.setCharacterEncoding("Shift_JIS"); String id = (String)request.getAttribute("id"); %> <html><body> <%= id %> </body></html>
--------------------------------------------------- 考えてみれば当然の事だと思うのですがこのjspではListを受け取ることができません。 解決方法や参考になるサイトがありましたらどなたかご教示ください よろしくお願いします。
|
| 質問日時 |
2009-07-22 10:07:30 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
-----------------main.java---------------------- ArrayList<String> aid = new ArrayList<String>(); //中略 request.setAttribute("id",aid);
---------------------jsp--------------------------- String id = (String)request.getAttribute("id");
main.javaでArrayListと定義しているのにjsp側でStringにキャストしてるところがわからない。 ArrayListで受け取ってループまわせばいいんじゃないのかな。
|
| 回答日時 |
2009-07-22 10:26:16 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
お世話になっております。 通行人Aさんにご教示いただいたようにjspでArrayListを取得しループするように修正途中です。 下記の物ががソースです。
-------------------index.jsp-------------------------
<%@ page contentType="text/html;charset=Windows-31J" pageEncoding="Windows-31J" import="java.io.*,java.util.*,java.text.*"%>
<% request.setCharacterEncoding("Shift_JIS"); ArrayList<String> aid = new ArrayList<String>(); aid= (ArrayList)request.getAttribute("aid"); %>
<html> <body> <%= aid%> </body> </html>
---------------------------------------------------- >>型の安全性: 型 ArrayList の式は、未検査の型変換を使用して >>ArrayList に準拠するようにする必要があります
このようなワーニングが出てしまい、実際に実行する事ができないでいます。 「jsp ArrayList」「未検出の型変換」等の単語でgoogleで検索をしておりますが、 これといった物が見つけられなかった為再度ご教示いただけたらなと思います。 よろしくお願いします。
|
| 回答日時 |
2009-07-22 13:41:54 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
aid= (ArrayList)request.getAttribute("aid");
ジェネリック使ってるんだから
aid= (ArrayList<String>)request.getAttribute("aid");
じゃないのかね。
|
| 回答日時 |
2009-07-22 13:59:00 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
Warnigは発生するが、実行はできるでしょ。 検査を抑制するしかないが、 現行のまま、無視するのをお勧めする。
|
| 回答日時 |
2009-07-22 15:00:00 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
お世話になっております 通行人Aさんにご教示頂いた書き方で成功しましたが 不良社員さんの仰るようにそのままでも実行はできていたようですね。 勘違いしておりました。
お二人のおかげでなんとか完成しましたのでお礼を申し上げます。
また質問させていただく事もあると思いますが、その時はよろしくお願いします。
------------------jsp----------------- <%@ page contentType="text/html;charset=Windows-31J" pageEncoding="Windows-31J" import="java.io.*,java.util.*,java.text.*" %> <% request.setCharacterEncoding("Shift_JIS"); ArrayList<String> aid = new ArrayList<String>(); aid= (ArrayList<String>)request.getAttribute("id"); %>
<html> <head> </head> <body> <% if(aid == null){ out.println("からっぽ"); }else{ for(int i=0; i < aid.size(); i++) { String sid = aid.get(i); out.println(i); out.println("<br>"); } }%> </body> </html>
---------------------------------------
|
| 回答日時 |
2009-07-24 10:29:03 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
お世話になっております。 前回の質問した物に追加要素を作成した時に行き詰まってしまったので、 こちらのスレで再度質問させていただきます。
---------------------log.xml-------------------------- <?xml version="1.0" encoding="Shift_JIS" standalone="no"?> <site> <count>4</count> <page id="1"> <title>てす</title> <name>山崎</name> <text>あいうえおかきくけこ</text> </page> <page id="2"> <title>tesu</title> <name>佐藤</name> <text>さしすせそなにぬねの</text> </page> <page id="3"> <title>テスト</title> <name>鈴木</name> <text>たちつてと</text> </page> </site>
-------------------------main.java(必要な部分のみ抜粋)------------------------------
public void insertData02(String idcount)//idcountはxmlの数値+1されたものが送られてくる
throws SAXException, ParserConfigurationException, IOException, TransformerException { try{ FileInputStream fi = new FileInputStream("log.xml"); // ドキュメントビルダーファクトリを生成 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); // ドキュメントビルダーを生成 DocumentBuilder builder = dbfactory.newDocumentBuilder(); // パースを実行してDocumentオブジェクトを取得 Document xtree = builder.parse(fi); NodeList sita = xtree.getElementsByTagName("sita"); // 1番目のノードを書き換える Node count = ((Element)sita.item(0)). getElementsByTagName("count").item(0); count.getFirstChild().setNodeValue("idcount"); DOMSource source= new DOMSource(xtree); File f =new File("log.xml"); FileOutputStream fo = new FileOutputStream(f); StreamResult result = new StreamResult(fo);
TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); fo.close(); System.out.println("Job End"); } catch (Exception e){ e.printStackTrace(); } }
--------------------------------------------------------
現在log.xml内のcountの値を取得し、 それに+1した値を上書きするjavaプログラムを作成しようとしております。 xmlに書き込みが成功していないのですが、エラーが出ずに終了してしまう状態です。
googleで検索しあれやこれと試してみましたが、 解決の糸口が見つからなかったのでどなたかご教示いただけたらなと思います。 よろしくお願いします。
|
| 回答日時 |
2009-07-29 17:56:45 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
前回のとまったく無関係な内容なんだから別スレ立てれば?
|
| 回答日時 |
2009-07-29 18:00:30 |
|
| 名前 |
|
回答内容
回答を評価する
(0ポイント)
|
>通行人Aさん 恐れ入ります。 まだ日があまりたっていないので、乱立させるのは問題かなと思ったのですが、こういった場合でもスレッドを立てても良いものなのですね。 早速新規に立てさせていただこうと思います。 ありがとうございました。
|
| 回答日時 |
2009-07-29 18:04:46 |
|
質問から6ヶ月以上経過しているので、回答を書き込むことはできません。
|
|
 |
|
 |
 |