问题描述
初学struts,想用一个display.jsp来显示数据库表中的数据,一个Form,一个方法类,一个action,没有input,如果action执行成功则显示display.jsp 我想直接通过在myeclipse中进入display页面就显示数据,但是好像页面并没有执行到action,所以数据也显示不出来,请教高手怎么能够成功显示数据 啊,卡了一天了。。希望帮忙解决。。。 action的配置: <action attribute="assetsaddForm" name="assetsaddForm" path="/assetsdisplay" scope="request" type="com.school.struts.action.AssetsdisplayAction"> <forward name="dissuccess" path="/assetsdis.jsp" /> <forward name="failure" path="/login_failure.jsp" /> </action> assetsdisplay.jsp代码: <%@ page language="java" pageEncoding="UTF-8"%> <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%@ page import="com.school.struts.model.*" %> <%@ page import="com.school.struts.form.*" %> <%@ page import="java.util.*"%> <html> <% Collection coll=(Collection)request.getAttribute("display"); %> <head> <title>资产信息显示</title> <meta http-equiv='refresh' content='1;url=assetsdisplay'> </head> <body> <% if(coll==null || coll.isEmpty()){ %> <table width="100%" height="30" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="36" align="center">暂无资产信息!</td> </tr> </table> <% }else{ //通过迭代方式显示数据 Iterator it=coll.iterator(); int assetsid = 0; String assetsname = ""; String assetscategory = ""; String assetstype = ""; String assetsfactory = ""; int assetsnum = 0; String assetsbuydate = ""; String assetsstate = ""; String assetssource = ""; float assetsvalue = 0; float assetsresidualrate = 0; int assetsage = 0; %> <table align="center" border="4" bordercolor="#00a0be"> <tr class="title"> <td>资产编号</td> <td>资产名称</td> <td>资产类别</td> <td>资产型号</td> <td>生产厂家</td> <td>数量</td> <td>购买日期</td> <td>资产状态</td> <td>资产来源</td> <td>资产原值</td> <td>残值率</td> <td>使用年限</td> </tr> <% while(it.hasNext()){ AssetsaddForm assetsaddForm=(AssetsaddForm)it.next(); assetsid = assetsaddForm.getAssetsid(); assetsname = assetsaddForm.getAssetsname(); assetscategory = assetsaddForm.getAssetscategory(); assetstype = assetsaddForm.getAssetstype(); assetsfactory = assetsaddForm.getAssetsfactory(); assetsnum = assetsaddForm.getAssetsnum(); assetsbuydate = assetsaddForm.getAssetsbuydate(); assetsstate = assetsaddForm.getAssetsstate(); assetssource = assetsaddForm.getAssetssource(); assetsvalue = assetsaddForm.getAssetsvalue(); assetsresidualrate = assetsaddForm.getAssetsresidualrate(); assetsage = assetsaddForm.getAssetsage(); %> <tr> <td style="padding:5px;"> <%=assetsid%></td> <td style="padding:5px;"> <%=assetsname%></td> <td style="padding:5px;"> <%=assetscategory%></td> <td style="padding:5px;"> <%=assetstype%></td> <td style="padding:5px;"> <%=assetsfactory%></td> <td style="padding:5px;"> <%=assetsnum%></td> <td style="padding:5px;"> <%=assetsbuydate%></td> <td style="padding:5px;"> <%=assetsstate%></td> <td style="padding:5px;"> <%=assetsresidualrate%></td> <td style="padding:5px;"> <%=assetsvalue%></td> <td style="padding:5px;"> <%=assetssource%></td> <td style="padding:5px;"> <%=assetsage%></td> </tr> <% } } %> </table> </body> </html> action代码如下: public class AssetsdisplayAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { String str = null; System.out.println("dfoiguj"); request.setAttribute("display", Assetsdatamodel.display(str)); return mapping.findForward("dissuccess"); } 方法如下:public static Collection display(String queryif){ AssetsaddForm assetsaddForm = null; Collection assetscoll = new ArrayList(); Statement stmt = null; ResultSet rs = null; try{//捕捉异常信息Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/logistics","root","123456"); stmt = conn.createStatement(); String sql = ""; if (queryif == null || queryif == "" || queryif == "all") { //当参数queryif的值为null、all或空时查询全部数据 sql = "select * from assetsdata"; } //sql = "select * from assetsdata"; rs = stmt.executeQuery(sql); //assets = new ArrayList<AssetsaddForm>(); while (rs.next()){ assetsaddForm = new AssetsaddForm(); assetsaddForm.setAssetsid(rs.getInt("assetsid")); assetsaddForm.setAssetsname(rs.getString("assetsname")); assetsaddForm.setAssetscategory(rs.getString("assetscategory")); assetsaddForm.setAssetstype(rs.getString("assetstype")); assetsaddForm.setAssetsfactory(rs.getString("assetsfactory")); assetsaddForm.setAssetsnum(rs.getInt("assetsnum")); assetsaddForm.setAssetsbuydate(rs.getString("assetsbuydate")); assetsaddForm.setAssetsstate(rs.getString("assetsstate")); assetsaddForm.setAssetssource(rs.getString("assetssource")); assetsaddForm.setAssetsvalue(rs.getFloat("assetsvalue")); assetsaddForm.setAssetsresidualrate(rs.getFloat("assetsresidualrate")); assetsaddForm.setAssetsage(rs.getInt("assetsage")); System.out.println(rs.getInt("assetsid")); assetscoll.add(assetsaddForm);//把查询结果保存到collection集合中} }catch(Exception ex){ System.out.println(ex.getMessage()); }finally{ try{ rs.close(); stmt.close(); }catch(SQLException e){ e.printStackTrace(); } } System.out.println("dfoiguj"); return assetscoll; //返回查询结果}
解决方案
直接访问jsp是不行的,你在浏览器里边这件访问这个action就好了:http://****:***/**/assetsdisplay.do另外说一下,struts1在设计上和易用性上不如struts2,既然都没有用过,还是直接用struts2的好。
解决方案二:
只有执行了action,然后在action里面把list数据放到request.setAttribute中,jsp才能像你这样访问。如果不走action,那就把action里面的代码copy到jsp中。建议你先学习jsp基础知识,再学习servlet、mvc相关知识,到时候你就知道你现在这种做法是有问题的了
解决方案三:
建议直接用struts2的demo先练练手,了解一下mvc的概念及架构,http://www.blogjava.net/nokiaguy/archive/2008/04/15/193229.html
解决方案四:
display页面就显示数据,但是好像页面并没有执行到action,所以数据也显示不出来,因为数据是通过action设置的 所以必须走action才有数据 直接访问jsp不会有数据的 而且也不应该让用户直接访问jsp (MVC的好处就没了)