您的位置:首页 > 其它

OGNL表达式2

2015-10-30 15:12 176 查看
问题:OGNL常用相关标签的使用,OGNL表达式2的使用

案例:下载地点:点击打开链接 注:做该案例,前提是struts2的环境





1.struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="checkbox" namespace="/test" extends="struts-default">
<action name="checkboxAction" class="cn.itcast.web.domain.CheckBoxAction">
<result name="success">/form.jsp</result>
</action>
</package>
</struts>


两个action动作类

package cn.itcast.web.domain;

import java.io.Serializable;

import com.opensymphony.xwork2.ActionSupport;

public class CheckBoxAction extends ActionSupport implements Serializable {
private String[] hobby1={"学习","打代码","程序"};//输入到jsp页面的选中值
private String[] hobby2;//得到的实际值
public String[] getHobby1() {
return hobby1;
}
public void setHobby1(String[] hobby1) {
this.hobby1 = hobby1;
}
public String[] getHobby2() {
return hobby2;
}
public void setHobby2(String[] hobby2) {
this.hobby2 = hobby2;
}

public String execute(){
hobby2=new String[]{"程序"};
return SUCCESS;
}

}


package cn.itcast.web.domain;

import java.io.Serializable;

public class Book implements Serializable {
private String name;
private float price;

public Book(){

}

public Book(String name, float price) {
super();
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}


form.jsp页面:

<%@page import="cn.itcast.web.domain.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>checkboxsList的用法</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>
<s:form action="checkboxAction" namespace="/test">
<s:textfield name="username"></s:textfield>
<s:password name="password"></s:password>
<s:checkboxlist name="hobby" list="{'吃饭','睡觉','打豆豆'}" value="{'吃饭','打豆豆'}" ></s:checkboxlist>
<h3>从action动作类传过来的checkbox值</h3>
<s:checkboxlist list="hobby1" name="hh" value="hobby2"></s:checkboxlist>
<h3>map类型的设定,listkey和listvalue反过来取值了。</h3>
<s:checkboxlist list="#{'北京':'0','上海':'1','贵州':'2'}" name="province" listKey="value" listValue="key" value="{'2'}"></s:checkboxlist>
<h3>从bean类book.class获取值</h3>
<%
List ps=new ArrayList();
ps.add(new Book("书1",2f));
ps.add(new Book("书2",3f));
ps.add(new Book("书3",4f));
request.setAttribute("ps", ps);
%>
<s:checkboxlist list="#request.ps" name="b" listKey="price" listValue="name"></s:checkboxlist>
<!-- listKey="price" listValue="name" 这个写不写都可以。默认选上 -->
<s:radio list="#{'0':'女','1':'男'}" listKey="key" listValue="value" ></s:radio>
<s:select list="#{'12':'设定','13':'聚焦','14':'三农' }" value="'13'"></s:select>
<!-- 这个要注意的是双引号里面到底当成的是字符串还是表达式不清楚的情况下,要是字符串,加上单引号即可 -->
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: