OGNL表达式 struts2


package com.cola.action;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cola.bean.User;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

 * @注释: jsp<------->Action 传值:将待传递的对象通过set[A-->J] get[J-->A]方法进行传递 页面使用struts2

 * 标签 和OGNL表达式 web对象不用setget方法 jsp页面 value="#session.[键值对的键名]" 2015年12月10日 jing

 */

@SuppressWarnings("all")

public class OGNLAction extends ActionSupport {

    private User stu = new User(); // 传递对象

    private User techer = new User();

    private User user = new User();

    private List<User> list = new ArrayList<User>(); // 传递List

    private Map session; //由sruts 封装通过ActionContext取出

    private Map request; //由sruts 封装通过ActionContext取出

    private HttpServletRequest httprequest; //web 内置对象 直接使用

    private Map<String,String> map = new HashMap<String,String>();

    public OGNLAction() {

        // 获得session 防止每次载入当前Action中的execute()的方法而清空session,特此放入构造函数中

        session = ActionContext.getContext().getSession();

        //获得request ActionContext.getContext.get() 返回类型为Object

        request = (Map) ActionContext.getContext().get("request");

        //获得httprequest

        httprequest = ServletActionContext.getRequest();

    }

    @Override

    public String execute() throws Exception {

        // TODO Auto-generated method stub

        System.out.println("***************************");

        user.setUsername("James");

        user.setPassword("admin");

        stu.setUsername("xiaoming");

        stu.setPassword("123");

        techer.setUsername("zhang");

        techer.setPassword("123");

        list.add(user);

        list.add(stu);

        list.add(techer);

        // 给session放入值 该session 类型为Map

        session.put("sesskey", "fool");

        request.put("reqkey", "toofool");

        httprequest.setAttribute("httpreqkey", "clever");

        //map key可重复

        map.put("1", "hello");

        map.put("2", "hello");

        map.put("3", "hello");

        return SUCCESS;

    }

    public Map<String, String> getMap() {

        return map;

    }

    public void setMap(Map<String, String> map) {

        this.map = map;

    }

    public List<User> getList() {

        return list;

    }

    public User getUser() {

        return user;

    }

    public void setList(List<User> list) {

        this.list = list;

    }

    public void setUser(User user) {

        this.user = user;

    }

    public String show() {

        // 传递Action内的方法

        return "this is show() of function!";

    }

}

<%@ page language ="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@taglib uri= "/struts-tags" prefix ="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content= "text/html; charset=utf-8">

<title> Insert title here</ title>

</head>

<body>

<hr />

<h1>this is OGNL jsp! </h1 >

访问普通属性: <s:property value ="username" /> <br>

访问普通属性方法: <s:property value ="username.length()" /> <br >

访问对象:<s:property value ="user"/>&nbsp &nbsp &nbsp< a> 已重写toString</a> <br>

访问对象属性: <s:property value ="user.username"/> < br>

访问对象属性: <s:property value ="user.password"/> < br>

访问对象的方法: <s:property value ="user.fun()"/> <br>

访问action的方法: <s:property value ="show()"/> <br>

访问集合属性: <s:property value ="list"/> <br>

访问集合大小: <s:property value ="list.ssize()"/> <br>

遍历整个集合: <br >

<s:iterator value= "list">

     <s:property /> <br >

</s:iterator>< br>

<br>

遍历集合中放入对象的 username属性(法一): <br >

<s:iterator value= "list" >

     <s:property value ="username"/><br>

</s:iterator>< br>

<br>

遍历集合中放入对象的 username属性(法二): <br >

<s:iterator value= "list" var="v" >

     <s:property value ="#v.username"/>< br>

</s:iterator>< br>

<br>

遍历集合中放入对象的password属性: <br >

<s:iterator value= "list" >

     <s:property value ="password"/><br>

</s:iterator>< br>

取得session:<s:property value ="#session.sesskey"/>< br>

取得request:<s:property value ="#request.reqkey"/>< br>

取得httprequest :<s:property value= "#request.httpreqkey"/><br >

<hr>

<br>

<h2>struts2标签:</ h2>

<h3>property:</ h3>< br>

<s:property value= "map"/> <br>

<s:property value= "map.keys"/> <br>

<s:property value= "map.values"/> <br>

<s:iterator value= "map">

     <s:property value ="key"/> <---------------> <s:property value= "value"/><br >

</s:iterator>


<h3>set:[不常用]</ h3>

<a>注意value=""中还有单引号,因为""里面放的是OGNL表达式,字符串需跟引号,否则为变量 </a >

<s:set var="adminName" value= "'admins'" scope="session" /> <br>

<s:set var="adminName" value= "'admin'" scope="request" /> <br>

<s:property value= "#session.adminName"/> <br>

<s:property value= "#request.adminName"/> <br>

<s:property value= "#attr.adminName"/> <br>


<h3>s:bean------->相当于实例化bean</h3 ><br >

<s:bean name= "com.cola.bean.User" var="doctor" >

     <s:param name ="username" value="'cola'" />

     <s:param name ="password" value="'123'" />

</s:bean>

bean:< s:property value= "#doctor"/> <br> <br>


<h3>if:</ h3>

<s:if test="3>2"> true</ s:if>

<s:elseif test= "2==2">==</s:elseif >

<s:else> false</ s:else> < br>

<a>打印 value stack【值栈表】 中的Action: </a ><br >

<s:property value= "[0]"/>

<s:debug></ s:debug>

</body>

</html>


你可能感兴趣的:(struts2,ognl表达式)