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. [原]逆向iOS SDK -- +[UIImage imageNamed:] 的实现

    汇编代码: ; Dump of assembler code for function +[UIImage imageNamed:] ; R0 = UIImage, R1 = "imageN ...

  2. 线性判别分析算法(LDA)

    1. 问题 之前我们讨论的PCA.ICA也好,对样本数据来言,可以是没有类别标签y的.回想我们做回归时,如果特征太多,那么会产生不相关特征引入.过度拟合等问题.我们可以使用PCA来降维,但PCA没有将 ...

  3. springMVC3学习(十)--注解式控制器

    Spring2.5引入注解式处理器支持,通过@Controller和@RequestMapping注解定义 我们的处理器类.并且提供了一组强大的注解 需要通过处理器映射DefaultAnnotatio ...

  4. 专为webkit内核而生的javascript库mango正式发布

    专为webkit内核而生的javascript库mango正式发布 Mango(芒果) javascript库 求fork https://github.com/willian12345/mango ...

  5. MSBuild是什么?

    MSBuild入门 MSBuild是什么? MSBuild全称(Microsoft Build Engine),是用来生成.NET程序的平台.您可能不知道它,但是如果您在使用VS做开发,那么一定时时刻 ...

  6. flowplayer视频播放插件

    flowplayer视频播放插件 最近项目中需要添加播放视频的功能,视频文件是flv格式的.在网上找了一些jQuery视频播放插件,还是觉得“flowplayer”要好一些.特将使用方法记录一下. f ...

  7. hdu 1998 奇数阶魔方(找规律+模拟)

    应该不算太水吧. 17  24   1   8  15   23   5   7  14  16    4   6  13  20  22   10  12  19  21   3   11  18 ...

  8. mongodb两次被黑后......

    先说说事情的经过...... 2017年1月8号星期天,在家翻头条无意中看到一条新闻说很多用户的mongodb被黑了,数据都被删了.当时想着公司的爬虫用的也是mongodb做存储,应该不会被黑吧,不可 ...

  9. Java项目中打开本地文件的方法

    1:其中saveAddress 为已知本地文件全路径: Desktop.getDesktop().open(new File(saveAddress));

  10. 日志快速筛选 之 linux命令grep|uniq|wc|awk

    以前我个人的观念是,在线上运行的东西尽量不要记什么流水日志. 但是后来我变了,发现在线上记日志是一个绝对有必要的东西,尤其是在当下很流行的微服务的推动下,没有日志的帮助,犹如一个睁眼瞎,排查问题基本靠 ...