練功房推薦書單

  • 猛虎出柙雙劍合璧版--最新 OCA / OCP Java SE 7 Programmer 專業認證 (電子書)
  • 流浪教師存零股存到3000萬(全新增修版)(書+DVD)
  • 開始在關西自助旅行(京都‧大阪‧神戶‧奈良)(全新增訂版)
  • 不敗教主的300張股票存股術

Help me level menu ... RSS feed
討論區首頁 » 網頁程式設計 Web Development
發表人 內容
newbiejforum

九級學員

註冊時間: 2011/4/22
文章: 10
離線
I have problem, this is my code :

file GenericCategories.java
package com.menu.dao.generic;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.menu.entities.Categories;
import com.menu.entities.Menu;
import com.menu.includes.DBConnect;

public class GenericCategories {

public List SelectAllC() throws Exception {
List sac = new ArrayList();

PreparedStatement ps = null;
ResultSet rs = null;
PreparedStatement ps1 = null;
ResultSet rs1 = null;
DBConnect db = new DBConnect();
Connection con = db.getConnect();

String sql = "SELECT id, category FROM categories";

try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();

while(rs.next()){
Categories c = new Categories();

c.setCategory(rs.getString("category"));
c.setId(rs.getInt("id"));

String sql2 = "SELECT m.id, m.menu FROM menu m, categories c WHERE c.id = m.cat AND m.cat = ?";
ps1 = con.prepareStatement(sql2);
ps1.setInt(1, c.getId());
rs1 = ps1.executeQuery();

while(rs1.next()) {
Menu m = new Menu();

m.setMenuid(rs1.getInt("id"));
m.setMenu(rs1.getString("menu"));

///// // How can i code here ... ////////////

}

sac.add(c);
}
} finally {

}

return sac;
}

}


and my Categories.java:

package com.menu.entities;




public class Categories {
private int id;
private String category;



public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}
}


file Menu.java
package com.menu.entities;


public class Menu {
private int menuId;
private String menu;

public int getMenuid() {
return this.menuId;
}

public void setMenuid(int menuId) {
this.menuId = menuId;
}

public String getMenu() {
return this.menu;
}

public void setMenu(String menu) {
this.menu = menu;
}
}

and index.java
package com.menu;


import java.io.*;
import java.util.*;

import freemarker.template.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.menu.dao.generic.GenericCategories;
import com.menu.dao.generic.GenericMenu;

public class Index extends HttpServlet {
private Configuration cfg;

public void init() {
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(getServletContext(), "templates");
cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
}

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
res.setCharacterEncoding("utf-8");
Template t = cfg.getTemplate("demo.html");

Map root = new HashMap();

GenericCategories gc = new GenericCategories();
GenericMenu gm = new GenericMenu();


try {
root.put("allCategories", gc.SelectAllC());
} catch (Exception e1) {
System.out.println(e1);
}

Writer out = res.getWriter();
try {
t.process(root, out);
} catch (TemplateException te) {
te.printStackTrace();
}
}
}



I want list categories two level, i need you help, thank you very much ...



bious3927

十級學員

註冊時間: 2011/6/2
文章: 5
離線
An idea is to declare another sub arraylist for storing menu items, and then put the sub arraylist into the your sac list.
Add something in the following


//line 41
List sublist = new ArrayList();

//line 48
sublist.add(m);

//line 53
sac.add(sublist);
newbiejforum

九級學員

註冊時間: 2011/4/22
文章: 10
離線



I tried .... and it does not run smilie


andowson

七段學員
[Avatar]

註冊時間: 2007/1/2
文章: 710
來自: 台北
離線
Some hints for you:
1. Change your demo.html as follows:
<#list allCategories as cat>

${cat.category}

<#list cat.getMenus() as menu>
${menu.menu}

</#list>
</#list>


2.Add a collection object like ArrayList for menus in Categories class

package com.menu.entities;

public class Categories {
private int id;
private String category;
private List menus = new ArrayList();

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public List getMenus() {
retrun menus;
}

public void addMenu(Menu menu) {
this.menus.add(menu);
}
}

分享經驗 累積智慧
[WWW]
newbiejforum

九級學員

註冊時間: 2011/4/22
文章: 10
離線
Wow, thank you very much andowson smilie
 
討論區首頁 » 網頁程式設計 Web Development
前往:   
行動版