succon
succon
2013-04-23 01:40

struts中直接通过jsp来如何调用action

已采纳

初学struts,想用一个display.jsp来显示数据库表中的数据,一个Form,一个方法类,一个action,没有input,如果action执行成功则显示display.jsp 我想直接通过在myeclipse中进入display页面就显示数据,但是好像页面并没有执行到action,所以数据也显示不出来,请教高手怎么能够成功显示数据 啊,卡了一天了。。希望帮忙解决。。。
action的配置:
attribute="assetsaddForm"
name="assetsaddForm"
path="/assetsdisplay"
scope="request"
type="com.school.struts.action.AssetsdisplayAction">

 </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.*"%>


<%
Collection coll=(Collection)request.getAttribute("display");
%>


资产信息显示



<%
if(coll==null || coll.isEmpty()){
%>




暂无资产信息!

<%
}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;
%>















<%
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();
%>















<%
}
}
%>
资产编号 资产名称 资产类别 资产型号 生产厂家 数量 购买日期 资产状态 资产来源 资产原值 残值率 使用年限
 <%=assetsid%>  <%=assetsname%>  <%=assetscategory%>  <%=assetstype%>  <%=assetsfactory%>  <%=assetsnum%>  <%=assetsbuydate%>  <%=assetsstate%>  <%=assetsresidualrate%>  <%=assetsvalue%>  <%=assetssource%>  <%=assetsage%>


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();
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; //返回查询结果
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • guazixing guazixing 8年前

    直接访问jsp是不行的,你在浏览器里边这件访问这个action就好了:

    [code="java"]http://****:***/**/assetsdisplay.do[/code]

    另外说一下,struts1在设计上和易用性上不如struts2,既然都没有用过,还是直接用struts2的好。

    点赞 1 评论 复制链接分享
  • Dead_Knight Dead_Knight 8年前

    只有执行了action,然后在action里面把list数据放到request.setAttribute中,jsp才能像你这样访问。如果不走action,那就把action里面的代码copy到jsp中。

    建议你先学习jsp基础知识,再学习servlet、mvc相关知识,到时候你就知道你现在这种做法是有问题的了

    点赞 评论 复制链接分享
  • zyn010101 zyn010101 8年前

    建议直接用struts2的demo先练练手,了解一下mvc的概念及架构,[url]http://www.blogjava.net/nokiaguy/archive/2008/04/15/193229.html[/url]

    点赞 评论 复制链接分享
  • jinnianshilongnian jinnianshilongnian 8年前

    display页面就显示数据,但是好像页面并没有执行到action,所以数据也显示不出来,

    因为数据是通过action设置的 所以必须走action才有数据 直接访问jsp不会有数据的 而且也不应该让用户直接访问jsp (MVC的好处就没了)

    点赞 评论 复制链接分享

相关推荐