简单学习struts标签中使用ognl表达式

来源:互联网 发布:上海大学乐乎论坛圈子 编辑:程序博客网 时间:2024/05/22 06:38

struts2标签:

支持ognl表达式,不能使用el;

标签属性不再是纯String类型,时Object;
标签属性的类型分为Object   String;
           1-Object可以直接写ognl;
           2-String:%{propertyName};
           3-如果不清楚类型:统统加 %{},如果是Object则忽略%;
           4-如果属性的类型时Object,而又想给一个固定值:如:%{'lisi'};

实现代码:

Action:

package com.handler;import java.util.ArrayList;import java.util.List;import java.util.Map;import com.bean.City;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class Action1 extends ActionSupport{private int id;private int age;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String show(){this.age = 24;this.name = "name2";Map<String, Object> request = (Map<String, Object>)ActionContext.getContext().get("request");request.put("name", "lisi");return SUCCESS;}}
前端页面获取值:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><%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 'show.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>  <%  //pageContext.setAttribute("name", "lisi");  //pageContext.setAttribute("name", "zhangsan", PageContext.SESSION_SCOPE);  String str = "name1";  String name1 = "aaa";   %>  <s:set var="%{name}" value="%{'wangwu1'}" scope="request"></s:set>   age=<s:property default="123" value="%{age}"/><br>   name=<s:property value="#request.name"/><br>  abc: <s:property value="#request.name2"/><br>      <s:debug></s:debug>  </body></html>





原创粉丝点击