【web开发学习笔记】Structs2 OGNL学习笔记

第一部分:代码

//前端
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
		<title>Insert title here</title>
	</head>
	<body>
		访问属性<br/>
		<a href="<%=contextPath %>/ognl.action?username=u&password=p">ognl</a>
	</body>
</html>
//web.xml
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
	<include file="/com/struts2/ognl/ognl.xml"/>
</struts>
//ognl.xml
<struts>
    <package name="ognl" extends="struts-default">
        <action name="ognl" class="com.struts2.ognl.OgnlAction">
            <result>/ognl.jsp</result>
        </action>
        <action name="test" class="com.struts2.ognl.TestAction">
            <result type="chain">ognl</result>
        </action>
    </package>
</struts>
//类包
//ognl.java
package com.struts2.ognl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
public class OgnlAction extends ActionSupport {
	private String password;
	private String username;

	private User user;
	private Cat cat;

	private List<User> users = new ArrayList<User>();
	private Set<Dog> dogs = new HashSet<Dog>();
	private Map<String, Dog> dogMap = new HashMap<String, Dog>();

	public OgnlAction() {
		users.add(new User(1));
		users.add(new User(2));
		users.add(new User(3));

		dogs.add(new Dog("dog1"));
		dogs.add(new Dog("dog2"));
		dogs.add(new Dog("dog3"));

		dogMap.put("dog100", new Dog("dog100"));
		dogMap.put("dog101", new Dog("dog101"));
		dogMap.put("dog102", new Dog("dog102"));
	}

	public String execute() {
		return SUCCESS;
	}

	public String getPassword() {
		return password;
	}

	public String getUsername() {
		return username;
	}

	public User getUser() {
		return user;
	}

	public Cat getCat() {
		return cat;
	}

	public List<User> getUsers() {
		return users;
	}
	public Map<String, Dog> getDogMap() {
		return dogMap;
	}

	public Set<Dog> getDogs() {
		return dogs;
	}

	public String m() {
		return "hello";
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	public void setDogMap(Map<String, Dog> dogMap) {
		this.dogMap = dogMap;
	}

	public void setDogs(Set<Dog> dogs) {
		this.dogs = dogs;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
}

//user.java
package com.struts2.ognl;
public class User {
	private int age = 8;

	public User() {
	}

	public User(int age) {
		super();
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "user" + age;
	}
}
//cat.java
package com.struts2.ognl;
public class Cat {
	private Dog friend;

	public Dog getFriend() {
		return friend;
	}

	public void setFriend(Dog friend) {
		this.friend = friend;
	}

	public String miaomiao() {
		return "miaomiao";
	}
}

//dog.java
package com.struts2.ognl;
public class Cat {
	private Dog friend;

	public Dog getFriend() {
		return friend;
	}

	public void setFriend(Dog friend) {
		this.friend = friend;
	}

	public String miaomiao() {
		return "miaomiao";
	}
}
//展示
</head>
<body>
<ol>
	<li>访问值栈中的action的普通属性: username = <s:property value="username"/> </li>
	<li>访问值栈中对象的普通属性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li>
	<li>访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/></li>
	<li>访问值栈中对象的普通方法:<s:property value="password.length()"/></li>
	<li>访问值栈中对象的普通方法:<s:property value="cat.miaomiao()" /></li>
	<li>访问值栈中action的普通方法:<s:property value="m()" /></li>
	<hr />
	<li>访问静态方法:<s:property value="@[email protected]()"/></li>
	<li>访问静态属性:<s:property value="@[email protected]"/></li>
	<li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li>
	<hr />
	<li>访问普通类的构造方法:<s:property value="new com.struts2.ognl.User(8)"/></li>
	<hr />
	<li>访问List:<s:property value="users"/></li>
	<li>访问List中某个元素:<s:property value="users[1]"/></li>
	<li>访问List中元素某个属性的集合:<s:property value="users.{age}"/></li>
	<li>访问List中元素某个属性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li>
	<li>访问Set:<s:property value="dogs"/></li>
	<li>访问Set中某个元素:<s:property value="dogs[1]"/></li>
	<li>访问Map:<s:property value="dogMap"/></li>
	<li>访问Map中某个元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li>
	<li>访问Map中所有的key:<s:property value="dogMap.keys"/></li>
	<li>访问Map中所有的value:<s:property value="dogMap.values"/></li>
	<li>访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>
	<hr />
	<li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>
	<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
	<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
	<li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>
	<hr />
	<li>[]:<s:property value="[0].username"/></li>
</ol>
<s:debug></s:debug>
</body>
</html>

第二部分:分析

<a href="<%=contextPath %>/ognl.action?username=u&password=p">ognl</a>

-> 点击之后,根据配置文件里里面的配置,找到文件包含<include file="/com/struts2/ognl/ognl.xml"/>

-> 查找olnl.xml文件

->

<struts>
		<package name="ognl" extends="struts-default">
			<action name="ognl" class="com.struts2.ognl.OgnlAction">
				<result>/ognl.jsp</result>
			</action>
			<action name="test" class="com.struts2.ognl.TestAction">
				<result type="chain">ognl</result>
			</action>
		</package>
	   </struts>

-> 根据xml配置文件里面的action关联的类,构造类对象;

-> 根据ognl.java构造ognl对象;

-> 展示阶段进行取值显示,有几种访问方式需要注意:

-> 访问值栈中属性,值栈中对象的方法,静态方法,静态属性,容器对象;

访问值栈中的action的普通属性

值栈中对象的普通属性

值栈中对象的普通方法

值栈中action的普通方法

静态方法访问方式@类名@方法名

需要在web.xml中设置<constant name="struts.ognl.allowStaticMethodAccess"

value="true"></constant>

查看默认设置的方法:/Struts2_1900_OGNL / WebRoot / WEB-INF / lib / struts2-core-2.1.6.jar /default.properties

访问集合

-> 调用方法

http://localhost:8080/Struts2_1900_OGNL/ognl.action?username=u&password=p&user.age = 19&cat.friend.name =oudy

第三部分;讨论

Object Graph Navigation Language

对象图导航语言

第四部分:有关投影的讨论

投影即过滤功能。

过滤写法:

			<li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>
			<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
			<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
			<li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>

以? ^ $ 开头加上过滤条件,和正则表达式不相同;

this表示循环过程中的对象,帮助你做一些循环;

<li>投影(过滤):<s:property value="users.{?#this.age==1}"/></li>表示从从users中取出age=1的那一个,结果是一个集合;

<li>投影(过滤):<s:property value="users.{?#this.age==1}[0]"/></li>表示从从users中取出age=1的第一个,结果是一个;<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>表示从从users中取出age>1的集合结尾的那一个;

【web开发学习笔记】Structs2 OGNL学习笔记

时间: 2024-08-24 13:06:01

【web开发学习笔记】Structs2 OGNL学习笔记的相关文章

Java Web开发及应用软件方向的学习计划

从接触计算机以来,一直抱有很浓厚的兴趣.我并不擅长与人交际,与机器对话可能更有性格方面的优势.虽然我很想做出一些改变,但总得需要时间和历练,暂时也只能这样了~ 一直很向往代码的神秘,在梦之站待过两年时间后更是对程序猿的生活抱有幻想,我觉得我适合做这件事情,也很有兴趣.中途我去为考研准备了一段时间,但后来我发现我对读研似乎没有很大的愿望,不是因为考研难,只是遵循了自己真正的想法.没有更高的学历,又出身一个不太牛逼的一本,将来的生活是无法预测的,我根本不确定是否能像某人说得那样走向成功.但现实如此,

全端Web开发 使用JavaScript与Java 阅读笔记

计算机科学领域只有两大难题:缓存失效和命名. REST(Representational State Transfer)技术,他建议一种Web服务消息传递的风格. 第一章:因变而变 企业家总在寻求变化,他们适应变化,并把它当作一种机遇. J2EE和JSP经过完善变成了JEE和JSF. 现代的客户端-服务器架构里,服务器更大程度上负责相应客户端的请求,提供资源的访问方式(通常使用XML 和JSON交换信息).在过去的服务器驱动模型中,页面(和与之相关的数据)都在服务器端生成完毕,一起返回客户端在浏

spring boot框架学习6-spring boot的web开发(2)

本章节主要内容: 通过前面的学习,我们了解并快速完成了spring boot第一个应用.spring boot企业级框架,那么spring boot怎么读取静态资源?如js文件夹,css文件以及png/jpg图片呢?怎么自定义消息转换器呢?怎么自定义spring mvc的配置呢?这些我们在公司都需要用的.这些怎么解决呢?在接下来的小节详细讲解这些.好了,现在开启spring boot的web开发第一节 本节主要: 1:InternalResourceViewResolver讲解 2:自动配置静态

spring boot框架学习7-spring boot的web开发(3)-自定义消息转换器

本章节主要内容: 通过前面的学习,我们了解并快速完成了spring boot第一个应用.spring boot企业级框架,那么spring boot怎么读取静态资源?如js文件夹,css文件以及png/jpg图片呢?怎么自定义消息转换器呢?怎么自定义spring mvc的配置呢?这些我们在公司都需要用的.这些怎么解决呢?在接下来的小节详细讲解这些.好了,现在开启spring boot的web开发第一节 本节主要: 1:自定义消息转换器 本文是<凯哥陪你学系列-框架学习之spring boot框架

想做web开发 就学JavaScript

有一天我被问到,为了快速地在 web 开发工作上增加优势,应该学习什么语言.我的思绪回到了大学,那时候我用 Pascal.Fortran.C和汇编语言,不过那个时候有不同的目标. 鉴于当前的状况和趋势,答案相对容易给出来:学习 JavaScript.四周看看,曾经低端的浏览器脚本语言现在变得随处可见,从服务器端到客户端,每天好像有更多的选择. 出身低微 我记得数年前投入 web 开发,学习了 HTML.用 Perl 开发 CGI 脚本.Netscape 在其 2.0 版本的浏览器里引入了一种叫做

学JavaScript,做web开发

有一天我被问到,为了快速地在 web 开发工作上增加优势,应该学习什么语言.我的思绪回到了在麦子学院学习的时光,那时候我用 Pascal.Fortran.C和汇编语言,不过那个时候有不同的目标. 想做web开发,就学JavaScript 鉴于当前的状况和趋势,答案相对容易给出来:学习 JavaScript.四周看看,曾经低端的浏览器脚本语言现在变得随处可见,从服务器端到客户端,每天好像有更多的选择. 出身低微 我记得数年前投入 web 开发,学习了 HTML.用 Perl 开发 CGI 脚本.N

HTML5移动Web开发实战 PDF扫描版?

<HTML5移动Web开发实战>提供了应对这一挑战的解决方案.通过阅读本书,你将了解如何有效地利用最新的HTML5的那些针对移动网站的功能,横跨多个移动平台.全书共分10章,从移动Web.设备端配置和优化,变互.响应式设计.设备访问,调试.性能测试.富媒体等角度出发,包含了60多个实用的示倒,详细阐释如何构建快速.响应式的HTML5移动网站,适用于iOS.Android.WindowsPhone和BlackBerry等众多主流移动应用平台.  <HTML5移动Web开发实战>作者是

【web开发学习笔记】Structs2 Action学习笔记(一)

1.org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter准备和执行 2. <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> url-pattern约定熟成只写/*,没必要写*.action 3. <

【web开发学习笔记】Structs2 Action学习笔记(二)

action学习笔记2-有关于action method的讨论 Action执行的时候并不一定要执行execute方法,可以在配置文件中配置Action的时候用method=来指定执行哪个方法 也可以在url地址中动态指定(动态方法调用DMI)(推荐) 方法一 <struts> <constant name="struts.devMode" value="true" /> <package name="user" e