新建一个项目,添加struts支持.
新建一个JSP页面:index.jsp,body里只有一行代码,跳转页面,请求请求
<body>
<logic:redirect page="/list.do"></logic:redirect> <!-- 发送请求,到 struts-config action 的path-->
</body>
然后到在struts-config.xml中添加一个form和与之对应的action.
form代码:
<form-bean name="listForm" type="com.yourcompany.struts.form.ListForm" />
action代码:
<action
attribute="listForm"
input="/error.jsp"
name="listForm"
path="/list"
scope="request"
type="com.yourcompany.struts.action.ListAction"
validate="false">
<forward name="success" path="/list.jsp" />
</action>
接着在ListAction.java中写如下代码:
ListForm listForm = (ListForm) form; /*创造此行的代码的行为:在struts-config中创建action时跟form所对应*/
Connection conn; //设置连接对象
try {
conn = this.getDataSource(request).getConnection(); //从数据源.连接池中得到信息赋给conn
oracle = new OracleDao(conn); //new一个OracleDao类的对象oracle,并且获得conn的信息
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List list=oracle.findAll(); //调用对象方法findAll()获取信息并且赋给集合对象(List)list
if(list.size()>0){ //判断如果集合对象不为空
request.setAttribute("List",list); //将list对象以名字"List"存入request请求里
return mapping.findForward("success"); //跳转到struts-config中action跳转语句 name为"success" 所指向的页面
}else{
ActionErrors errors = new ActionErrors(); //创建一个错误集合
errors.add("error", new ActionError("litao")); //将错误"litao"以"error"的别名存入错误集合,litao可以在资源文 //件中进一步阐述
this.saveErrors(request,errors); //保存错误
return new ActionForward(mapping.getInput()); //返回到mapping的input属性指向的页面
接下来,如果操作成功,则跳转到成功页面。