练习OGNL 与 Struts2的使用(一)

练习OGNL 与 Struts2的使用(一)

添加stuts的支持

建表

   对配置文件的修改
<property name="stuname" type="java.lang.String">
<column name="stuname" length="50">
<comment> 姓名</comment>
</column>
</property>
<!— 下面的内 容是新增的-->
<property name="birthday" type="java.util.Date">
<column name="birthday">
<comment> 生日</comment>
</column>
</property>
.对于 Student.java  的实体映射
private String stuname;
// 下面是新增的字段
private Date birthday; //java.util.Date 类型
// 使用 MyEclipse  环境生成 birthday 的 的 getter 和 和 setter 
Struts.xml 
<struts>
<package name="mypackage" namespace="/" extends="struts-default">
<action name="stu" class="org.sf.action.StudentAction">
<result name="stu_list">/stu_list.jsp</result>
</action>
</package>
</struts>
  添加 StudentAction 
package org.sf.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.sf.dao.IStudentDao;
import org.sf.dao.impl.StudentDaoImpl;
import org.sf.entity.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
/**
*  取得学生列表的方法
* @return
*/
public String list(){
IStudentDao stuDao = new StudentDaoImpl();
List<Student> list = stuDao.getAllStudentList();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("list", list);
return "stu_list";
}
}
 Stu_list.jsp 
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> 学生信息列表</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<s:head/>
</head>
<body>
<table border="1" align="center">
<tr>
<td colspan="4" align="center">
<a href="stu_add.jsp"> 添加学生信息</a>
</td>
</tr>
<tr>
<th>id</th>
<th> 学号</th>
<th> 姓名</th>
<th> 生日</th>
</tr>
<s:iterator value="#request.list" var="stu">
<tr>
<td><s:text name="%{#stu.id}" /></td>
<td><s:text name="%{#stu.stunumber}" /></td>
<td><s:text name="%{#stu.stuname}" /></td>
<td><s:date name="%{#stu.birthday}" format="yyyy-MM-dd HH:mm:ss" /></td>
</tr>
</s:iterator>
<tr>
<td colspan="4" align="center">
<a href="stu_add.jsp"> 添加学生信息</a>
</td>
</tr>
</table>
</body>
</html>
在地址栏中输入:http://localhost:8080/stucourse/stu!list,将会看到如下结果 
注意这里的 stu!list 意思是指 stu是struts.xml中action的名字也就是
<action name="stu" class="org.sf.action.StudentAction">
<result name="stu_list">/stu_list.jsp</result>
而list是指这个类中,list这个方法,中间用!分隔,这样就能直接访问java类






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值