问题描述
我刚学马士兵struts,里面的bbs2009,看着做但是发现最后的结果不一样,结果没有出来,初步看是没传到页面上。数据库已经是是有数据的。页面的list没有数据如下图:部分代码如下mysql的:publicclassDB{publicstaticConnectioncreateConn(){Connectionconn=null;try{Class.forName("com.mysql.jdbc.Driver");conn=DriverManager.getConnection("jdbc:mysql://localhost/bbs2009","root","123456");}catch(ClassNotFoundExceptione){e.printStackTrace();}catch(SQLExceptione){e.printStackTrace();}returnconn;}publicstaticPreparedStatementprepare(Connectionconn,Stringsql){PreparedStatementps=null;try{ps=conn.prepareStatement(sql);}catch(SQLExceptione){e.printStackTrace();}returnps;}publicstaticvoidclose(Connectionconn){try{conn.close();conn=null;}catch(SQLExceptione){e.printStackTrace();}}publicstaticvoidclose(Statementstmt){try{stmt.close();stmt=null;}catch(SQLExceptione){e.printStackTrace();}}publicstaticvoidclose(ResultSetrs){try{rs.close();rs=null;}catch(SQLExceptione){e.printStackTrace();}}}
service层的publicList<Category>list(){Connectionconn=DB.createConn();Stringsql="select*from_category";PreparedStatementps=DB.prepare(conn,sql);List<Category>categories=newArrayList<Category>();try{ResultSetrs=ps.executeQuery();Categoryc=null;while(rs.next()){c=newCategory();c.setId(rs.getInt("id"));c.setName(rs.getString("name"));c.setDescription(rs.getString("descrtiption"));categories.add(c);}}catch(SQLExceptione){e.printStackTrace();}DB.close(ps);DB.close(conn);returncategories;}
action的publicclassCategoryActionextendsActionSupport{privateList<Category>categories;privateCategoryServicecategoryService=newCategoryService();privateCategorycategory;privateintid;publicStringlist(){categories=categoryService.list();returnSUCCESS;}publicList<Category>getCategories(){returncategories;}publicvoidsetCategories(List<Category>categories){this.categories=categories;}
model层的publicclassCategory{privateintid;privateStringname;privateStringdescription;publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetDescription(){returndescription;}publicvoidsetDescription(Stringdescription){this.description=description;}}
前台的<body>Category_list<ahref="admin/Category-addInput">添加Category</a><ahref="admin/Category-updateInput">更新Category</a><hr/><s:iteratorvalue="categories"var="c"><s:propertyvalue="#c.name"/>|<s:propertyvalue="#c.description"/>|<ahref="admin/Category-delete?id=<s:propertyvalue="#c.id"/>">删除Category</a>|<ahref="admin/Category-updateInput?id=<s:propertyvalue="#c.id"/>">更新Category</a><br/></s:iterator><s:debug></s:debug></body>
求各位大神解决!在线等,需要提供什么代码都可以随时贴上
解决方案
解决方案二:
publicStringlist(){categories=categoryService.list();returnSUCCESS;}
在这边加个断点调试下,前台写的没有问题应该是这边没取到值或者根本就没进这个方法(这个可能就和你配置的有关了)