1 OGNL为struts2的表达式。是可以在网页上使用的表达式。有#、%等用法。#表示ActionContext.getContext(),访问非根对象。
#的用法。可以通过#request.bean.xxxx。表示action对象中的bean属性(必须有get和set方法)的xxx属性。在页面上通过s:property标签来展现出来。
如:
前台:
a:<s:property value="#request.bean.sexSer"/>
后台:
//action类中必须有bean属性。必须有bean的get和set方法。然后给bean进行赋值。
public class ExampleAction extends ActionSupport{
private Example bean;
public Example getBean() {
return bean;
}
public void setBean(Example bean) {
this.bean = bean;
}
public void init(){
xxxxxxxx。。。
}
2 还有一个是简单的属性。就是action中有一个属性。该属性也必须有get和set方法。然在一个方法中给其设置。在网页中也可以通过OGNL表达式得到值。
如:a:<s:property value="#request.ceshi"/>
后台:
public class ExampleAction extends ActionSupport{
private String ceshi;
public String getCeshi() {
return ceshi;
}
public void setCeshi(String ceshi) {
this.ceshi = ceshi;
}
public String testOGNL(){
System.out.println("进入了testOGNL");
this.ceshi="123-456";
return "success";
}
3 #可以访问非根对象,struts中的值栈为根对象。所以访问struts中的属性。可以不用#,但是使用#也能访问。
如:a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
该ceshi对象。是action类中的属性,必须添加get和set方法。
4 在页面的ognl表达式必须通过s:property标签来展现出来。s:property标签时struts的显示属性标签。
如:a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
5 在普通的标签内,可以通过EL表达式得到值。
如c:<input type="text" value="${ceshi}"/>
6 OGNL表达式中有%,%可用于在标签的属性值中,在字符串中用%告诉网页引擎%{}括号内的是OGNL表达式。
如:<input type="text" value="<s:property value='%{#request.ceshi}'/>" />
完整的例子:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ 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 'index.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">
<script type="text/javascript" language="javascript" src="<%=basePath %>common/WDatePicker/WdatePicker.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="<%=basePath %>common/jquery/jquery-easyui-1.3.2/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=basePath %>common/jquery/jquery-easyui-1.3.2/themes/default/easyui.css">
<script type="text/javascript" language="javascript" src="<%=basePath %>common/jquery/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>
<script type="text/javascript" language="javascript" src="<%=basePath %>common/jquery/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>
<script type="text/javascript" language="javascript" src="<%=basePath %>common/json2.js"></script>
<style type="text/css">
.table_css{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
.table_css tr{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
.table_css tr td{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
</style>
</head>
<script language="javascript">
function btn_btn1_onclick(){
document.form1.action="userAddAction!add";
document.form1.submit();
}
</script>
<body>
<fieldset>
<legend><s:property value="@com.util.Lan@getString('chaxun')"/></legend>
<form name="form1" action="exampleQuery!list" method="post">
<table>
<tr>
<td><s:property value="@com.util.Lan@getString('sex')"/></td>
<td>
<s:select name="bean.sexSer" list="#request.bean.list" listKey="sexKey" listValue="sexValue" headerKey="-1" headerValue="===%{@com.util.Lan@getString('qingxuanze')}===" value="#request.bean.sexSer" theme="simple"/>
</td>
<td><s:property value="@com.util.Lan@getString('createDate')"/></td>
<td>
<input type="text" id="bean.creatDateSer" name="bean.createDateSer" class="Wdate" value="${bean.createDateSer}" οnfοcus="WdatePicker({isShowWeek:true,dateFmt:'yyyy-MM-dd'})"/>
</td>
<td>
<input type="button" name="btn_query" id="btn_query" value="<s:property value='@com.util.Lan@getString(\"chaxun\")'/>" οnclick="btn_query_onclick()"/>
<input type="button" name="btn_testAjax" id="btn_testAjax" value="测试" οnclick="test_btn_test_ajax()"/>
<input type="button" name="btn_testOGNL" id="btn_testOGNL" value="测试OGNL" οnclick="test_btn_testOGNL()"/>
</td>
</tr>
</table>
</form>
</fieldset>
<%--<s:select name="bean.sexSer" list="#{0:'男',1:'女'}" listKey="key" listValue="value" headerKey="-1" headerValue="==请选择1==" value="#request.bean.sexSer"/>--%>
<%--
<input type="text" value="${bean.sexSer}"/>
a:<s:property value="#request.bean.sexSer"/>
b:<s:property value="%{bean.sexSer}"/>
c:<s:property value="bean.sexSer"/>
d:<s:property value="#request.bean.sexSer"/>
e:<s:property value="%{#request.bean.sexSer}"/>
--%>
a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
c:<input type="text" value="${ceshi}"/>
d:<input type="text" value="<s:property value='%{#request.ceshi}'/>" />
e:<s:property value="#request.xmltest"/>
f:<s:property value="xmltest"/>
g:<input type="text" value="123<s:property value='#request.xmltest'/>" />
<table class="table_css">
<tr>
<td>id</td>
<td>name</td>
<td>sex</td>
<td>remark</td>
</tr>
<s:if test="#request.bean.resultLists!=null&&#request.bean.resultLists.size()>0">
<s:iterator id="exampleLists" value="#request.bean.resultLists" status="">
<tr>
<td>
<s:property value="#exampleLists.id"/>
</td>
<td>
<s:property value="#exampleLists.name"/>
</td>
<td>
<s:if test="#exampleLists.sex==0">
男
</s:if>
<s:if test="#exampleLists.sex==1">
女
</s:if>
</td>
<td>
<s:property value="#exampleLists.remark"/>
</td>
</tr>
</s:iterator>
</s:if>
</table>
</body>
<script type="text/javascript" language="javascript">
function btn_query_onclick(){
//alert("111");
var sex=document.getElementById("bean.sexSer").value;
//alert(sex);
document.form1.action="exampleQuery!list";
document.form1.submit();
}
function test_btn_test_ajax(){
//alert("111");
var url="http://localhost:8080/test20150411/testAjax!ajax";
$.ajax({
url:url,
type:"post",
dateType:"json",
success:function(data){
alert(JSON.stringify(data));
}
})
}
function test_btn_testOGNL(){
//alert("111");
document.form1.action="exampleQuery!testOGNL";
document.form1.submit();
}
</script>
</html>
后台:
package com.example.action;
import java.util.ArrayList;
import java.util.List;
import com.common.lists.SexBean;
import com.example.bean.Example;
import com.example.service.interfaces.ExampleService;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleAction extends ActionSupport{
private Example bean;
private ExampleService exampleService;
private String ceshi;
private String xmltest;
public ExampleAction() {
super();
// TODO Auto-generated constructor stub
}
public void init(){
System.out.println("action初始化....");
SexBean sex0=new SexBean(0,"男");
SexBean sex1=new SexBean(1,"女");
List<SexBean> sexList=new ArrayList<SexBean>();
sexList.add(sex0);
sexList.add(sex1);
bean.setList(sexList);
}
public Example getBean() {
return bean;
}
public void setBean(Example bean) {
this.bean = bean;
}
public ExampleService getExampleService() {
return exampleService;
}
public void setExampleService(ExampleService exampleService) {
this.exampleService = exampleService;
}
public String query(){
init();
System.out.println("query");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String queryMethod(){
init();
System.out.println("queryMethod");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String list(){
init();
System.out.println("list");
String sexSer=bean.getSexSer();
if(sexSer.equals("-1")){
bean.setSexSer(null);
}
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String getCeshi() {
return ceshi;
}
public void setCeshi(String ceshi) {
this.ceshi = ceshi;
}
public String getXmltest() {
return xmltest;
}
public void setXmltest(String xmltest) {
this.xmltest = xmltest;
}
public String testOGNL(){
System.out.println("进入了testOGNL");
this.ceshi="123-456";
this.xmltest="name1111";
return "success";
}
}
#的用法。可以通过#request.bean.xxxx。表示action对象中的bean属性(必须有get和set方法)的xxx属性。在页面上通过s:property标签来展现出来。
如:
前台:
a:<s:property value="#request.bean.sexSer"/>
后台:
//action类中必须有bean属性。必须有bean的get和set方法。然后给bean进行赋值。
public class ExampleAction extends ActionSupport{
private Example bean;
public Example getBean() {
return bean;
}
public void setBean(Example bean) {
this.bean = bean;
}
public void init(){
xxxxxxxx。。。
}
2 还有一个是简单的属性。就是action中有一个属性。该属性也必须有get和set方法。然在一个方法中给其设置。在网页中也可以通过OGNL表达式得到值。
如:a:<s:property value="#request.ceshi"/>
后台:
public class ExampleAction extends ActionSupport{
private String ceshi;
public String getCeshi() {
return ceshi;
}
public void setCeshi(String ceshi) {
this.ceshi = ceshi;
}
public String testOGNL(){
System.out.println("进入了testOGNL");
this.ceshi="123-456";
return "success";
}
3 #可以访问非根对象,struts中的值栈为根对象。所以访问struts中的属性。可以不用#,但是使用#也能访问。
如:a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
该ceshi对象。是action类中的属性,必须添加get和set方法。
4 在页面的ognl表达式必须通过s:property标签来展现出来。s:property标签时struts的显示属性标签。
如:a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
5 在普通的标签内,可以通过EL表达式得到值。
如c:<input type="text" value="${ceshi}"/>
6 OGNL表达式中有%,%可用于在标签的属性值中,在字符串中用%告诉网页引擎%{}括号内的是OGNL表达式。
如:<input type="text" value="<s:property value='%{#request.ceshi}'/>" />
完整的例子:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ 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 'index.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">
<script type="text/javascript" language="javascript" src="<%=basePath %>common/WDatePicker/WdatePicker.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<link rel="stylesheet" type="text/css" href="<%=basePath %>common/jquery/jquery-easyui-1.3.2/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=basePath %>common/jquery/jquery-easyui-1.3.2/themes/default/easyui.css">
<script type="text/javascript" language="javascript" src="<%=basePath %>common/jquery/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>
<script type="text/javascript" language="javascript" src="<%=basePath %>common/jquery/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>
<script type="text/javascript" language="javascript" src="<%=basePath %>common/json2.js"></script>
<style type="text/css">
.table_css{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
.table_css tr{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
.table_css tr td{
border:#666666 solid 1px;
border-color:#666666 solid 1px;
}
</style>
</head>
<script language="javascript">
function btn_btn1_onclick(){
document.form1.action="userAddAction!add";
document.form1.submit();
}
</script>
<body>
<fieldset>
<legend><s:property value="@com.util.Lan@getString('chaxun')"/></legend>
<form name="form1" action="exampleQuery!list" method="post">
<table>
<tr>
<td><s:property value="@com.util.Lan@getString('sex')"/></td>
<td>
<s:select name="bean.sexSer" list="#request.bean.list" listKey="sexKey" listValue="sexValue" headerKey="-1" headerValue="===%{@com.util.Lan@getString('qingxuanze')}===" value="#request.bean.sexSer" theme="simple"/>
</td>
<td><s:property value="@com.util.Lan@getString('createDate')"/></td>
<td>
<input type="text" id="bean.creatDateSer" name="bean.createDateSer" class="Wdate" value="${bean.createDateSer}" οnfοcus="WdatePicker({isShowWeek:true,dateFmt:'yyyy-MM-dd'})"/>
</td>
<td>
<input type="button" name="btn_query" id="btn_query" value="<s:property value='@com.util.Lan@getString(\"chaxun\")'/>" οnclick="btn_query_onclick()"/>
<input type="button" name="btn_testAjax" id="btn_testAjax" value="测试" οnclick="test_btn_test_ajax()"/>
<input type="button" name="btn_testOGNL" id="btn_testOGNL" value="测试OGNL" οnclick="test_btn_testOGNL()"/>
</td>
</tr>
</table>
</form>
</fieldset>
<%--<s:select name="bean.sexSer" list="#{0:'男',1:'女'}" listKey="key" listValue="value" headerKey="-1" headerValue="==请选择1==" value="#request.bean.sexSer"/>--%>
<%--
<input type="text" value="${bean.sexSer}"/>
a:<s:property value="#request.bean.sexSer"/>
b:<s:property value="%{bean.sexSer}"/>
c:<s:property value="bean.sexSer"/>
d:<s:property value="#request.bean.sexSer"/>
e:<s:property value="%{#request.bean.sexSer}"/>
--%>
a:<s:property value="#request.ceshi"/>
b:<s:property value="ceshi"/>
c:<input type="text" value="${ceshi}"/>
d:<input type="text" value="<s:property value='%{#request.ceshi}'/>" />
e:<s:property value="#request.xmltest"/>
f:<s:property value="xmltest"/>
g:<input type="text" value="123<s:property value='#request.xmltest'/>" />
<table class="table_css">
<tr>
<td>id</td>
<td>name</td>
<td>sex</td>
<td>remark</td>
</tr>
<s:if test="#request.bean.resultLists!=null&&#request.bean.resultLists.size()>0">
<s:iterator id="exampleLists" value="#request.bean.resultLists" status="">
<tr>
<td>
<s:property value="#exampleLists.id"/>
</td>
<td>
<s:property value="#exampleLists.name"/>
</td>
<td>
<s:if test="#exampleLists.sex==0">
男
</s:if>
<s:if test="#exampleLists.sex==1">
女
</s:if>
</td>
<td>
<s:property value="#exampleLists.remark"/>
</td>
</tr>
</s:iterator>
</s:if>
</table>
</body>
<script type="text/javascript" language="javascript">
function btn_query_onclick(){
//alert("111");
var sex=document.getElementById("bean.sexSer").value;
//alert(sex);
document.form1.action="exampleQuery!list";
document.form1.submit();
}
function test_btn_test_ajax(){
//alert("111");
var url="http://localhost:8080/test20150411/testAjax!ajax";
$.ajax({
url:url,
type:"post",
dateType:"json",
success:function(data){
alert(JSON.stringify(data));
}
})
}
function test_btn_testOGNL(){
//alert("111");
document.form1.action="exampleQuery!testOGNL";
document.form1.submit();
}
</script>
</html>
后台:
package com.example.action;
import java.util.ArrayList;
import java.util.List;
import com.common.lists.SexBean;
import com.example.bean.Example;
import com.example.service.interfaces.ExampleService;
import com.opensymphony.xwork2.ActionSupport;
public class ExampleAction extends ActionSupport{
private Example bean;
private ExampleService exampleService;
private String ceshi;
private String xmltest;
public ExampleAction() {
super();
// TODO Auto-generated constructor stub
}
public void init(){
System.out.println("action初始化....");
SexBean sex0=new SexBean(0,"男");
SexBean sex1=new SexBean(1,"女");
List<SexBean> sexList=new ArrayList<SexBean>();
sexList.add(sex0);
sexList.add(sex1);
bean.setList(sexList);
}
public Example getBean() {
return bean;
}
public void setBean(Example bean) {
this.bean = bean;
}
public ExampleService getExampleService() {
return exampleService;
}
public void setExampleService(ExampleService exampleService) {
this.exampleService = exampleService;
}
public String query(){
init();
System.out.println("query");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String queryMethod(){
init();
System.out.println("queryMethod");
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String list(){
init();
System.out.println("list");
String sexSer=bean.getSexSer();
if(sexSer.equals("-1")){
bean.setSexSer(null);
}
List<Example> lists=this.exampleService.query(bean);
System.out.println("size:"+lists.size());
if(bean!=null){
bean.setResultLists(lists);
}else{
System.out.println("bean is null");
}
return "success";
}
public String getCeshi() {
return ceshi;
}
public void setCeshi(String ceshi) {
this.ceshi = ceshi;
}
public String getXmltest() {
return xmltest;
}
public void setXmltest(String xmltest) {
this.xmltest = xmltest;
}
public String testOGNL(){
System.out.println("进入了testOGNL");
this.ceshi="123-456";
this.xmltest="name1111";
return "success";
}
}