zoukankan      html  css  js  c++  java
  • Struts学习笔记_OGNL

    1,使用域模型时,只有传参user.age=8,才会new一个user对象,当然也可以再action中手动new一个user对象。所以初始化域模型时,可以自己构造,也可以传值,但这时必须有空的构造方法。

    2.当用chain跳转到另一个action时,值栈中会有两个action对象

    3.object graph navigation language(ognl) 对象图导航语言

    strust.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/bjsxt/struts2/ognl/ognl.xml"/>
        <!--  <include file="/testognl/testognl.xml"/>-->
    </struts>

    ognl.xml

    <struts>
    
        <package name="ognl" extends="struts-default">
    
            <action name="ognl" class="com.bjsxt.struts2.ognl.OgnlAction">
                <result>/ognl.jsp</result>
            </action>
            <action name="test" class="com.bjsxt.struts2.ognl.TestAction">
                <result type="chain">ognl</result>//调用这个时值栈中会有两个action对象
            </action>
    
        </package>
    </struts>
    OgnlAction .java
    public class OgnlAction extends ActionSupport {
        private Cat cat;
        private Map<String, Dog> dogMap = new HashMap<String, Dog>();
    
        private Set<Dog> dogs = new HashSet<Dog>();
    
        private String password;
    
        private User user;
        private String username;
    
        private List<User> users = new ArrayList<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 String execute() {
            return SUCCESS;
        }
    
        public Cat getCat() {
            return cat;
        }
        
        public Map<String, Dog> getDogMap() {
            return dogMap;
        }
    
        public Set<Dog> getDogs() {
            return dogs;
        }
        
        public String getPassword() {
            return password;
        }
        
        public User getUser() {
            return user;
        }
    
        public String getUsername() {
            return username;
        }
    
        public List<User> getUsers() {
            return users;
        }
    
        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;
        }
    }
    ognl.jsp
    <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="@com.bjsxt.struts2.ognl.S@s()"/></li> //@类名@静态方法 此时需要在struts.xml中配置 <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
            <li>访问静态属性:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/></li> //@类名@静态属性
            <li>访问Math类的静态方法:<s:property value="@@max(2,3)" /></li>
            <hr />
            <li>访问普通类的构造方法:<s:property value="new com.bjsxt.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>//set集合是无序的,所以这样取值不可以
            <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>//返回FALSE
            <hr />
            <li>[]:<s:property value="[0].username"/></li>//[0]指的是从0开始的所有对象
            
        </ol>
        
        <s:debug></s:debug>
    </body>
  • 相关阅读:
    (双指针 二分) leetcode 167. Two Sum II
    (双指针) leetcode 485. Max Consecutive Ones
    (双指针) leetcode 27. Remove Element
    (String) leetcode 67. Add Binary
    (数组) leetcode 66. Plus One
    (N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal
    (N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal
    (N叉树 递归) leetcode589. N-ary Tree Preorder Traversal
    (N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree
    (BST 递归) leetcode98. Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/enjoy-life-clh/p/4085299.html
Copyright © 2011-2022 走看看