struts2 OGNL (4)

OGNL

此部分我们使用一个 小项目来进行讲解
刚开始我们配置一下文件,包含两个JSP页面
index.jsp:
<%--
  Created by IntelliJ IDEA.
  User: Alex
  Date: 2017/5/16
  Time: 18:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>
  </head>
  <body>
  访问属性:<br/>
  <a href="ognl.action?username=alex&password=123">ognl</a>
  </body>
</html>


ognl.jsp:
<%--
  Created by IntelliJ IDEA.
  User: Alex
  Date: 2017/5/16
  Time: 18:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>OGNL表达式语言学习</title>
</head>
<body>
<ol>
在此插入例子
</ol>
<s:debug></s:debug>
</body>
</html>

以及 struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
    <include file="/ognl/ognl.xml" />
</struts>

和外部 ognl.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>
    <package name="ognl" extends="struts-default">
        <action name="ognl" class="ognl.ognlAction" >
            <result>/OGNL.jsp</result>
        </action>
    </package>
</struts>


以及JAVA类:

  1. ognlAction.java
    package ognl;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    import java.util.*;
    
    /**
     * Created by Alex on 2017/5/16.
     */
    public class ognlAction extends ActionSupport {
        private String username;
        private String password;
        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 Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        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 List<User> getUsers() {
            return users;
        }
    
        public void setUsers(List<User> users) {
            this.users = users;
        }
    
        public Set<Dog> getDogs() {
            return dogs;
        }
    
        public void setDogs(Set<Dog> dogs) {
            this.dogs = dogs;
        }
    
        public Map<String, Dog> getDogMap() {
            return dogMap;
        }
    
        public void setDogMap(Map<String, Dog> dogMap) {
            this.dogMap = dogMap;
        }
    
        @Override
        public String execute(){
            return SUCCESS;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String m(){
            return "hello!";
        }
    }
    

  2. User.java:
    package ognl;
    
    /**
     * Created by Alex on 2017/5/16.
     */
    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 ;
        }
    }
    

  3. Cat.java:
    package ognl;
    
    /**
     * Created by Alex on 2017/5/16.
     */
    public class Cat {
        private Dog friend;
    
        public Dog getFriend() {
            return friend;
        }
    
        public void setFriend(Dog friend) {
            this.friend = friend;
        }
    
        public String miaomiao(){
            return "miaomiao~";
        }
    }
    

  4. Dog.java:
    package ognl;
    
    /**
     * Created by Alex on 2017/5/16.
     */
    public class Dog {
        private String name;
        public Dog(){}
        public Dog(String name){
            super();
            this.name = name;
        };
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Dog:" + name;
        }
    }
    
    

  5. S.java:
    package ognl;
    
    /**
     * Created by Alex on 2017/5/16.
     */
    public class S {
        public static String STR="static string";
    
        public static String s(){
            return "static method";
        }
    }
    


此处我们使用迭代式开发(一点点来,步步为营,可调式,士气高涨,主流开发模式)

注意事项:
  1. OGNL全称 Object Gragh Navigation Language,对象图导航语言
  2. 1.user.xxx 只有传,才会构造(在页面内只有传了参数进去,值栈里面才会有值) 当然可以直接new对象出来,默认变量值,效果一样(domain model)。但是需要保持一个无参构造方法
ONGL如何访问对象和action的普通属性与普通方法:
    <li>访问值栈中的Action的普通属性:username = <s:property value="username"/> </li>
    <li>访问值栈中的对象的普通属性(get set方法) :<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>
如何访问静态属性与方法:
<li>访问静态方法: <s:property value="@ognl.S@s()" /></li>
<li>访问静态属性: <s:property value="@ognl.S@STR" /></li>
<li>访问Math类的静态方法: <s:property value="@@max(2,3)" /></li>
P.S: 第三种方法不常用。
如何访问普通类的构造方法:
<li>访问普通类的构造方法: <s:property value="new ognl.User(8)" /></li>
P.S:在此处可以直接在value属性里new对象出来
如何访问各种集合:
    <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[0].age" /> (最常用) ||  <s:property value="users.{age}[0]" /> </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>访问Map容器的大小: <s:property value="dogMap.size()" /></li>
    <li>访问Set容器的大小: <s:property value="dogs.size()" /></li>
    <li>访问List容器的大小: <s:property value="users.size()" /></li>
P.S:其中访问Set中的某个元素时,并取不到值,因 set集合中元素位置不固定所导致
投影:
    <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}" /></li>
    <li>投影:<s:property value="users.{$#this.age>1}.{age} == null" /></li>
P.S: ^为开头第一个,$为最后一个,而加判断式 == 输出true or false 

JSP页面:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值