succon 2013-04-23 01:40
浏览 473
已采纳

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 2013-04-23 09:04
    关注

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

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿