簡單學習struts標籤中使用ognl表達式

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>





發佈了46 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章