练习OGNL 与 Struts2的使用(一)
2016-06-14 23:03
281 查看
练习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 和 和 setterStruts.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类
相关文章推荐
- Java设计模式(六) 模版方法模式详解
- interface VS abstract class in java
- 自学javase第一天HelloWord遇到困难
- final
- Java中的堆栈存储机制详解
- 2016-6 java 学习计划
- JAVA学习随笔(1)--int转各进制的字符串
- java 堆与非堆 内存
- JAVA技术路线图
- Java Web 之 Servlet 学习总结
- 在Spring3中使用注解(@Scheduled)创建计划任务
- spring装配Bean
- Spring常用注解
- Javaweb项目构建常见问题
- Java语言的特点
- Eclipse 反编译插件安装jad
- Eclipse 反编译插件安装jad
- Eclipse 反编译插件安装jad
- java 重写&重载
- 在Eclipse中使用Maven插件 博客分类: Java相关技术