1.创建javaweb项目Struts2_Part4_OGNL并在WebRoot下的WEB-INF下的lib文件夹下添加如下jar文件

 commons-fileupload-1.2.1.jar

 commons-io-1.3.2.jar

 freemarker-2.3.15.jar

 mybatis-3.2.2.jar

 ognl-2.7.3.jar

 ojdbc14.jar

 struts2-core-2.1.8.1.jar

 xwork-core-2.1.6.jar

所需要的jar

2.在src下创建struts.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "struts-2.1.7.dtd" >
<struts>
<!-- 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 全局配置 -->
<global-results>
<result name="error">error.jsp</result>
</global-results> <!-- 时间格式化配置 -->
<action name="currentDate" class="com.action.DateFormatAction" method="getFormateDate">
<result name="success">ognldemo.jsp</result>
</action> <!-- Student的控制器的配置 -->
<action name="student" class="com.action.StudentAction" method="getInfo">
<result name="success">index.jsp</result>
</action> <!-- 详细信息的控制器的配置 -->
<action name="stuinfo" class="com.action.StuInfoAction" method="getInfo">
<result name="success">success.jsp</result>
</action>
</package>
</struts>

struts.xml

3.在src下的com.entity包下创建Student.java文件

 package com.entity;

 public class Student {
private Integer sid;
private String sname; public Student() {
}
public Student(Integer sid, String sname) {
this.sid = sid;
this.sname = sname;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + "]";
}
}

Student.java

4.在src下的com.action包下创建StudentAction.java文件

 package com.action;

 import com.entity.Student;
import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { @Override
public String execute() throws Exception { return ERROR;
} private Student stu;
public String getInfo(){
stu=new Student(1, "胡淑红");
return SUCCESS;
} public Student getStu() {
return stu;
} public void setStu(Student stu) {
this.stu = stu;
} }

StudentAction.java

5.在src下的com.action包下创建StuInfoAction.java文件

 package com.action;

 import java.util.ArrayList;
import java.util.List; import com.entity.Student;
import com.opensymphony.xwork2.ActionSupport; public class StuInfoAction extends ActionSupport{
private String[] ah;
private List<Student> list;
public String getInfo(){
if(ah!=null){
System.out.println("爱好是:");
for (int i = 0; i < ah.length; i++) {
System.out.print(ah[i]+",");
}
}else{
System.out.println("没有获取参数");
list=new ArrayList<Student>();
Student stu=new Student(1, "holly");
list.add(stu);
} return SUCCESS;
} public String[] getAh() {
return ah;
} public void setAh(String[] ah) {
this.ah = ah;
} public List<Student> getList() {
return list;
} public void setList(List<Student> list) {
this.list = list;
}
}

StuInfoAction.java

6.在src下的com.action包下创建DateFormatAction.java文件

 package com.action;

 import java.util.Date;

 import com.opensymphony.xwork2.ActionSupport;

 public class DateFormatAction extends ActionSupport {
//1.定义私有的Date类型的对象
private Date currentDate;
//2.action核心处理业务的方法
public String getFormateDate(){ //创建Date对象,也就是给成员对象赋值
currentDate=new Date();
System.out.println("系统时间:"+currentDate);
return SUCCESS;
}
//3.代理对象使用的getter和setter
public Date getCurrentDate() {
return currentDate;
}
public void setCurrentDate(Date currentDate) {
this.currentDate = currentDate;
}
}

DateFormatAction.java

7.在WebRoot下的WEB-INF下web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern> </filter-mapping>
<welcome-file-list>
<welcome-file>ognldemo.jsp</welcome-file>
</welcome-file-list>
</web-app>

web.xml

8.在WebRoot下创建ognldemo.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<!-- 在request作用域里设置age变量,值为18 -->
<s:set name="age" value="18" scope="request"/> <!-- 直接输出request作用域变量的值 -->
<s:property value="#request.age"/>
<s:property value="#attr.age"/> <!-- 在session作用域里设置name变量,值为holly -->
<s:set name="sname" value="'holly'" scope="session"/> <!-- 直接输出session作用域变量的值 -->
<s:property value="#session.sname"/>
<s:property value="#attr.sname"/> <!-- 在application作用域里设置sex变量,值为女 -->
<s:set name="sex" value="'女'" scope="application"/> <!-- 直接输出session作用域变量的值 -->
<s:property value="#application.sex"/>
<s:property value="#attr.sex"/> <!-- 格式化后台action发过来的Date类型的系统时间 -->
格式化的时间:<s:date name="currentDate" format="dd/MM/yyyy"/>
没有格式化:<s:date name="currentDate" /> </body>
</html>

ognldemo.jsp

9.在WebRoot下创建index.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
学生编号:<s:property value="stu.sid"/>
学生姓名:<s:property value="stu.sname"/>
<form action="stuinfo" method="post">
爱好:
<input type="checkbox" name="ah" value="吃饭"/>吃饭
<input type="checkbox" name="ah" value="睡觉"/>睡觉
<input type="checkbox" name="ah" value="张乐素质很高"/>张乐素质很高
<input type="submit" value="提交"/>
</form>
</body>
</html>

index.jsp

10.在WebRoot下创建success.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
操作成功
属性获取:
<s:property value="ah[0]"/>
<s:property value="ah[1]"/>
<br/>
<s:iterator value="list">
sid:<s:property value="sid"/>
<br/>
sname:<s:property value="sname"/>
</s:iterator> </body>
</html>

success.jsp

11.在WebRoot下创建error.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
操作失败
</body>
</html>

error.jsp

5.Struts2的OGNL表达式的更多相关文章

  1. Struts2的OGNL表达式语言

    一.OGNL的概念 OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者 ...

  2. 初窥struts2(二)OGNL表达式

    Struts2总结 Struts2完整的处理流程: 1  客户端发送请求,交给struts2控制器(StrutsPrepareAndExecuteFilter). 2  Filter控制器进行请求过滤 ...

  3. struts2 与 OGNL 表达式,jsp中 利用ognl 在valuestack中取值

    在Struts2中,一个请求在终于到达Action的方法之前,Action对象本身会被压入ValueStack(实际上就是放到ValueStack的CompoundRoot中),所以Action对象是 ...

  4. Struts2之OGNL表达式

    OGNL(Object-Graph Navigation Language的简称),对象图导航语言,它是一门表达式语言,除了用来设置和获取Java对象的属性之外,另外提供诸如集合的投影和过滤以及lam ...

  5. struts2(四) ognl表达式、值栈、actionContext之间的关系

    今天来说说ognl表达式在struts2中的运用. --wh 一.什么是Ognl? 通过百度百科查询到的解释,其中详细的说明了OGNL的作用. 下面我们就对OGNL这5个作用进行讲解 1.存取对象的任 ...

  6. Struts2之 OGNL表达式和值栈

    技术分析之OGNL表达式概述(了解)        1. OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写        * 所谓对象图,即以任意 ...

  7. struts2:OGNL表达式,遍历List、Map集合;投影的使用

    OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存 ...

  8. struts2:OGNL表达式之#、%、$符号运用

    1. OGNL表达达符号"#" 1.1 #用于访问OGNL上下文和Action上下文,#相当于ActionContext.getContext() 注意:当系统创建了Action实 ...

  9. java之struts2之OGNL表达式

    struts2推荐使用ognl表达式 ognl: object graph navigation language 对象导航图语言 如:school.teacher.address="北京& ...

  10. Struts2中OGNL表达式的用法

    今天分享的是Struts2框架中的一种ognl表达式语言,主要分两个目标去学习    1.理解struts2传值的优先级    2.ognl与el的区别 一:ognl表达式语言简介 OGNL的全称是O ...

随机推荐

  1. DevExpress 学习使用之 PrintSystem

    这是来自群里边的一段,收集起来,碎片知识是很珍贵的.  傷心孤影(2072201)  16:14:41导出excel加标题用PrintableComponentLink小宝(462561442)  1 ...

  2. CSS属性合写

    animation:[[ animation-name ] || [ animation-duration ] || [ animation-timing-function ] || [ animat ...

  3. 继承control的自定义TextBox

    继承control的自定义TextBox 下面来介绍一下本人写的一个自定义的textbox,首先说一下写这个控件遇到的几个难点:第一.关联输入法:第二.画字符串和焦点线 先随便上两张效果图吧: 下面这 ...

  4. 64位CentOS 6.0下搭建LAMP环境

    系统环境:Centos6.0 x64 1.确认搭建LAMP所需要的环境是否已经安装 [root@centos6 ~]# rpm -q make gcc gcc-c++ zlib-devel libai ...

  5. 反向代理(Reverse Proxy)

    反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...

  6. 推荐一个很好用的HTTP操作类

    /// <summary> /// 类说明:HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理 /// 重要提示 ...

  7. linux 高精度定时器例子

    //author:DriverMonkey //phone:13410905075 //mail:bookworepeng@Hotmail.com //qq:196568501 #include &l ...

  8. Bitmap的一个简单实现

    一.Bitmap简介 Bitmap是一种常用的数据结构,其实就是一个连续的数组,主要是用于映射关系,如映射整数,一位代表一个数,即这里假设Bitmap有100Bytes * 8 这么多的位,那么这里可 ...

  9. sql数据黑马程序员——SQL入门

    最近研究sql数据,稍微总结一下,以后继续补充: ---------------------- ASP.Net+Android+IO开辟S..Net培训.等待与您交流! --------------- ...

  10. ehcache.xml配置参数

    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc ...