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. DOM处理

    DOM处理 这几天整理了一下思路,本来觉得DOM部分会有很多东西,但是忽然发现频繁使用的其实并不太多 class class处理部分主要有四个 hasClass:检查元素是否包含某个class add ...

  2. Changing the working directory of VIM

    Sometimes we want to open another file in the same folder with current editing file, what we can do ...

  3. JS关闭当前页面的方法

    JS关闭当前页面的方法 一.不带任何提示关闭窗口的js代码 1 <a href="javascript:window.opener=null;window.open('','_self ...

  4. Extjs的学习及MIS系统实践应用

    Extjs的学习及MIS系统实践应用(系列文章) 本系列文章从Extjs的实际运用出发,结合系统开发的实践经验,详细解释Extjs的基本控件及控件扩展的用法,和在平时的学习运用中一步一步查阅的资料.积 ...

  5. TCP/IP capture/analysis tools in Unix/Linux

    There are some useful tools in Unix/Linux to check out how the system is going on. Here is a short s ...

  6. flask 真是太棒啦,阅读手册后就能做出一个博客了

    真是很好的东西,有很多有益处的东西. 有template引擎, 有flask自己带的g (用来处理访问与数据库打开关闭的) 有flask自己的处理session的功能 自带的jinja2模板引擎也是比 ...

  7. 应聘linux/ARM嵌入式开发岗位

    **************************************************************** 因为发在中华英才和智联招聘没有人采我所以我 在这里发布我的个人简历希望 ...

  8. 在 Cordova/Phonegap for Android 中包含中文文件名的页面

    在 Cordova/Phonegap for Android 中包含中文文件名的页面 本贴首发于: http://xuekaiyuan.com/forum.php?mod=viewthread& ...

  9. JS网站当前日期在IE9、Chrome和FireFox中年份显示为113年的解决方法 getFullYear();

    JS网站当前日期在IE9.Chrome和FireFox中年份显示为113年的解决方法 getFullYear();

  10. python中函数与函数之间的调用,总是晕菜,整理如下,有不对或者补充的请提出来~

    1.python函数基础 函数名: fun 函数体:1~3行 返回值:2 调用函数:fun() ,只有见到这个括号(),程序会根据函数名从内存中找到函数体,然后执行它. 2.函数的执行顺序 下面的fu ...