我是靠谱客的博主 搞怪灯泡,这篇文章主要介绍练习OGNL 与 Struts2的使用(一)练习OGNL 与 Struts2的使用(一),现在分享给大家,希望可以做个参考。

练习OGNL 与 Struts2的使用(一)

添加stuts的支持

建表

   对配置文件的修改
复制代码
1
2
3
4
5
6
7
8
9
10
11
<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  的实体映射
复制代码
1
2
3
4
private String stuname; // 下面是新增的字段 private Date birthday; //java.util.Date 类型 // 使用 MyEclipse 环境生成 birthday 的 的 getter 和 和 setter
Struts.xml 
复制代码
1
2
3
4
5
6
7
<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 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@ 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的名字也就是
复制代码
1
2
<action name="stu" class="org.sf.action.StudentAction"> <result name="stu_list">/stu_list.jsp</result>
而list是指这个类中,list这个方法,中间用!分隔,这样就能直接访问java类






最后

以上就是搞怪灯泡最近收集整理的关于练习OGNL 与 Struts2的使用(一)练习OGNL 与 Struts2的使用(一)的全部内容,更多相关练习OGNL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部