S7.1_Struts2_OGNL OGNL对象导航图范例&OGNL方法调用范例还有集合访问的范例
来源:互联网 发布:js 怎么把值输入数据库 编辑:程序博客网 时间:2024/06/08 01:53
对象导航图 ------ 访问对象里(多层)嵌套对象的属性,被嵌套的对象也称被引用的对象
第1步:找到net.nw.vo包,创建一个联系方式实体类Contact.java,注意红色字体标识的代码,测试时需要它们
package net.nw.vo;
//联系方式类
public class Contact {
private String email;
private String phone;
private String address;
public Contact(){}
public Contact(String email,String phone,String address){
this.email = email;
this.phone = phone;
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
第2步:将Contact.java类嵌套到User.java类里面,代码如下:
package net.nw.vo;
//用户类
public class User {
private String username;//用户名称
private String password;//用户密码
private Contact contact;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
}
第3步:找到WebRoot/login.jsp页面文件,给在User类里被引用的对象Contact字段赋值时,注意html标签名称的写法 被引用的对象小写.字段名称小写
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=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>系统登录</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>
<center>
<h1>系统登录</h1>
<hr>
<pre>
<a href="<%=path%>/language.action?request_locale=en_US">English</a> <a href="<%=path%>/language.action?request_locale=zh_CN">Simplified Chinese</a>
</pre>
<s:fielderror cssStyle="color:red"/><br>
<form name="loginForm" action="" method="post" >
<s:property value="getText('label.username')"/>:<input type="text" name="username"/><br>
<s:property value="getText('label.password')"/>:<input type="password" name="password"/><br>
<s:property value="getText('label.email')"/>:<input type="text" name="contact.email"/><br>
<s:property value="getText('label.phone')"/>:<input type="text" name="contact.phone"/><br>
<s:property value="getText('label.address')"/>:<input type="text" name="contact.address"/><br>
<input type="button" value="登录1" onclick="javascript:document.loginForm.action='<%=path%>/user/login1.action';document.loginForm.submit();"/><br>
<input type="button" value="登录2" onclick="javascript:document.loginForm.action='<%=path%>/user/login2.action';document.loginForm.submit();"/><br>
<input type="button" value="登录3" onclick="javascript:document.loginForm.action='<%=path%>/user/login3.action';document.loginForm.submit();"/><br>
<input type="button" value="登录4" onclick="javascript:document.loginForm.action='<%=path%>/user/login4.action';document.loginForm.submit();"/><br>
<br>
<br><a href="<%=path%>/test_result.jsp">测试ResultType</a>
<br><a href="<%=path%>/admin/login.action">管理员登录</a>
</form>
</center>
</body>
</html>
第4步:'添加label.phone和'label.address国际化资源
App_en_US.properties
label.phone phone
label.address address
App_zh_CN.properties
label.phone 电话
label.address 住址记得参考前面一个项目介绍的唯一正确操作方法添加上面的标签国际化资源
第5步:读取嵌套对象的属性值,在WebRoot/login_success.jsp页面文件中添加红色字体标识的代码:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=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>登录成功</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>
<center>
<h1>登录成功</h1>
我们的口号是:<s:property value="#request.msg"/>(从request对象中获取)<br>
<s:property value="#session.msg"/>(从session对象中获取)<br>
<s:property value="#application.msg"/>(从application对象中获取)<br>
<s:property value="#attr.msg"/>(从attribute对象中获取)<br>
<br>
欢迎用户:<s:property value="username"/><br>
密码是:<s:property value="password"/><br>
联系方式如下:<br>
<s:property value="contact.email"/><br>
<s:property value="contact.phone"/><br>
<s:property value="contact.address"/><br>
<li><a href="<%=path%>/user/User_add.action?method=添加">添加</a></li>
<li><a href="<%=path%>/user/User_delete.action?method=删除">删除</a></li>
<li><a href="<%=path%>/user/User_modify.action?method=修改">修改</a></li>
<s:debug></s:debug><br>
</center>
</body>
</html>
第6步:在net.nw.action包里找到UserAction2-validation.xml配置文件(因为本范例验证使用这种配置文件的验证方式),
注意红色字体标识名称被引用的对象小写.字段名称小写
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<!-- <message>用户名称不能为空</message> -->
<message>
${getText("login.field.isnull",{getText("label.username")})}
</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<!-- <message>密码不能为空</message> -->
<message>
${getText("login.field.isnull",{getText("label.password")})}
</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">4</param>
<param name="maxLength">6</param>
<!-- <message>密码必须是4-6位</message> -->
<message key="login.password.lengtherror"></message>
</field-validator>
</field>
<field name="contact.email">
<field-validator type="requiredstring">
<!-- <message>电子邮件不能为空</message> -->
<message key="login.email.isnull"></message>
</field-validator>
<field-validator type="email">
<!-- <message>电子邮箱格式不正确</message> -->
<message key="login.email.formaterror"></message>
</field-validator>
</field>
</validators>
第7步:发布运行程序,测试效果截图如下:
在WebRoot/login.jsp页面文件中找到<s:debug></s:debug><br>,在该行上面插入以下3行代码测试调用普通类的普通方法和调用普通类的构造方法
<b>OGNL方法调用测试范例</b>
<li>调用普通类的普通方法:<s:property value="contact.print()" /></li>
<li>调用普通类的构造方法:<s:property value="new net.nw.vo.Contact(\"cat@136.com\",\"0755-12345678\",\"shenzhen\")" /></li>
package net.nw.vo;
//联系方式类
public class Contact {
private String email;
private String phone;
private String address;
public Contact(){}
public Contact(String email,String phone,String address){
this.email = email;
this.phone = phone;
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//调用普通类的普通方法 ------测试范例使用它
public String print(){
return "email: "+this.email +" phone: "+ this.phone + " address: "+this.address;
}
//调用普通类的构造方法默认输出的内容 ------ 测试范例使用它
public String toString(){
return "email: "+this.email +" phone: "+ this.phone + " address: "+this.address;
}
}
OGNL方法调用测试范例显示结果的页面使用WebRoot/login_success.jsp页面文件,除了以上2个范例,看看以下其它范例的调用格式
<%@ page contentType="text/html; charset=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>登录成功</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>
<center>
<h1>登录成功</h1>
我们的口号是:<s:property value="#request.msg"/>(从request对象中获取)<br>
<s:property value="#session.msg"/>(从session对象中获取)<br>
<s:property value="#application.msg"/>(从application对象中获取)<br>
<s:property value="#attr.msg"/>(从attribute对象中获取)<br>
<br>
欢迎用户:<s:property value="username"/><br>
密码是:<s:property value="password"/><br>
联系方式如下:<br>
<s:property value="contact.email"/><br>
<s:property value="contact.phone"/><br>
<s:property value="contact.address"/><br>
<li><a href="<%=path%>/user/User_add.action?method=添加">添加</a></li>
<li><a href="<%=path%>/user/User_delete.action?method=删除">删除</a></li>
<li><a href="<%=path%>/user/User_modify.action?method=修改">修改</a></li>
<b>OGNL方法调用测试范例</b>
<li>调用普通类的普通方法:<s:property value="contact.print()" /></li>
<li>调用普通类的构造方法:<s:property value="new net.nw.vo.Contact(\"cat@136.com\",\"0755-12345678\",\"shenzhen\")" /></li>
<li>调用Action类的普通方法:<s:property value="add()" /></li>
<li>调用Action类的静态方法:<s:property value="@net.nw.action.UserAction2@greet()" /></li>
<li>调用Action类的静态属性:<s:property value="@net.nw.action.UserAction2@sid" /></li>
<li>调用普通类的构造方法:<s:property value="new net.nw.vo.Contact(\"cat@136.com\",\"0755-12345678\",\"shenzhen\")" /></li>
<li>调用JDK类库的方法:<s:property value="@java.util.Calendar@getInstance()" /></li>
<li>调用JDK类库的Math类方法:<s:property value="@@random()" /></li>
<s:debug></s:debug><br>
</center>
</body>
</html>
备注:
测试<li>调用Action类的静态属性:<s:property value="@net.nw.action.UserAction2@sid" /></li>
我们需要在UserAction2中定义公有静态sid变量并赋值 ------ public static String sid = "21377137713X";
测试 <li>调用Action类的静态方法:<s:property value="@net.nw.action.UserAction2@greet()" /></li>
我们需要在UserAction2中定义greet静态方法:
public static String greet(){
return "Welcome to china.";
}
最重要的一点别忘记在struts.xml配置文件中添加红色字体标识的常量配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" ></constant>
<constant name="struts.i18n.encoding" value="utf-8" ></constant>
<constant name="struts.custom.i18n.resources" value="App"></constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 父包default -->
<package name="default" namespace="" extends="struts-default">
<global-results>
<result name="login_success">/login_success.jsp</result>
<result name="login_failure">/login_failure.jsp</result>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="exit">
<result>/login.jsp</result>
</action>
<action name="language">
<result>/login.jsp</result>
</action>
</package>
<!-- 子包user继承于父包default -->
<package name="user" namespace="/user" extends="default">
<global-results>
<result>/user/result.jsp</result>
</global-results>
<action name="login*" class="net.nw.action.UserAction{1}">
<result name="input">/login.jsp</result>
</action>
<action name="*_*" class="net.nw.action.{1}Action4" method="{2}">
</action>
</package>
<include file="admin.xml"></include>
<package name="test" namespace="" extends="default">
<action name="test1" class="net.nw.action.TestAction1">
<!-- 默认result类型等于dispatcher -->
<result>/test/test1.jsp</result>
</action>
<action name="test2" class="net.nw.action.TestAction2">
<!-- result类型等于redirect -->
<result type="redirect">/test/test2.jsp</result>
</action>
<action name="test3" class="net.nw.action.TestAction1">
<!-- result类型等于chain -->
<result type="chain">test2</result>
</action>
<action name="test4" class="net.nw.action.TestAction2">
<!-- result类型等于redirectAction -->
<result type="redirectAction">test1</result>
</action>
<action name="test5" class="net.nw.action.TestAction2">
<!-- result类型等于plainText -->
<result type="plainText">/test/test2.jsp</result>
</action>
</package>
<package name="error" namespace="" extends="default">
<action name="**">
<result>/error.jsp</result>
</action>
</package>
</struts>
测试效果截图如下:
OGNL访问集合测试范例
第1步:在net.nw.action.UserAction2.java添加红色字体标识的代码:
package net.nw.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import net.nw.dao.UserDao;
import net.nw.vo.Contact;
import net.nw.vo.User;
public class UserAction2 extends ActionSupport implements ModelDriven<User>,RequestAware,SessionAware,ApplicationAware{
/**
* 对象序列化需要此serialVersionUID
*/
private static final long serialVersionUID = 1L;
private User user = new User();//注意:此处user一定要实例化
private Map<String,Object> request;//IOC注入来初始化对象
private Map<String,Object> session;
private Map<String,Object> application;
public static String sid = "21377137713X";
private List<Contact> contactors = new ArrayList<Contact>();
private Set<String> cities = new HashSet<String>();
private Map<String,String> dics = new HashMap<String,String>();
public List<Contact> getContactors() {
return contactors;
}
public void setContactors(List<Contact> contactors) {
this.contactors = contactors;
}
public Set<String> getCities() {
return cities;
}
public void setCities(Set<String> cities) {
this.cities = cities;
}
public Map<String, String> getDics() {
return dics;
}
public void setDics(Map<String, String> dics) {
this.dics = dics;
}
public UserAction2() {
System.out.println("构造方法UserAction2 ......");
contactors.add(new Contact("tom@136.com","13000000001","beijing"));
contactors.add(new Contact("jack@126.com","13500000001","shanghai"));
contactors.add(new Contact("mike@qq.com","13600000001","guangzhou"));
cities.add("beijing");
cities.add("shanghai");
cities.add("xi'an");
dics.put("001", "zhangsan");
dics.put("002", "lisi");
dics.put("003", "wangwu");
}
//调用业务层操作类的方法
public String execute(){
UserDao dao = new UserDao();
if(dao.userLogin(user)){
request.put("msg", "北京欢迎您!");
session.put("msg", "北京欢迎您!");
application.put("msg", "北京欢迎您!");
return "login_success";
} else{
this.addFieldError("login_error","用户名称或者用户密码不正确");
return "login_failure";
}
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
public String add(){
System.out.println("普通用户添加 ...");
return SUCCESS;
}
public String delete(){
System.out.println("普通用户删除 ...");
return SUCCESS;
}
public String modify(){
System.out.println("普通用户修改 ...");
return SUCCESS;
}
@Override
public void setApplication(Map<String, Object> application) {
// TODO Auto-generated method stub
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
// TODO Auto-generated method stub
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = request;
}
public static String greet(){
return "Welcome to china.";
}
}
第2步:访问List集合Set集合还有Map集合里面的值
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=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>登录成功</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>
<center>
<h1>登录成功</h1>
我们的口号是:<s:property value="#request.msg"/>(从request对象中获取)<br>
<s:property value="#session.msg"/>(从session对象中获取)<br>
<s:property value="#application.msg"/>(从application对象中获取)<br>
<s:property value="#attr.msg"/>(从attribute对象中获取)<br>
<br>
欢迎用户:<s:property value="username"/><br>
密码是:<s:property value="password"/><br>
联系方式如下:<br>
<s:property value="contact.email"/><br>
<s:property value="contact.phone"/><br>
<s:property value="contact.address"/><br>
<li><a href="<%=path%>/user/User_add.action?method=添加">添加</a></li>
<li><a href="<%=path%>/user/User_delete.action?method=删除">删除</a></li>
<li><a href="<%=path%>/user/User_modify.action?method=修改">修改</a></li>
<b>OGNL方法调用测试范例</b>
<li>调用普通类的普通方法:<s:property value="contact.print()" /></li>
<li>调用普通类的构造方法:<s:property value="new net.nw.vo.Contact(\"cat@136.com\",\"0755-12345678\",\"shenzhen\")" /></li>
<li>调用Action类的普通方法:<s:property value="add()" /></li>
<li>调用Action类的静态方法:<s:property value="@net.nw.action.UserAction2@greet()" /></li>
<li>调用Action类的静态属性:<s:property value="@net.nw.action.UserAction2@sid" /></li>
<li>调用普通类的构造方法:<s:property value="new net.nw.vo.Contact(\"cat@136.com\",\"0755-12345678\",\"shenzhen\")" /></li>
<li>调用JDK类库的方法:<s:property value="@java.util.Calendar@getInstance()" /></li>
<li>调用JDK类库的Math类方法:<s:property value="@@random()" /></li><br>
<b>OGNL访问集合测试范例</b>
<li>访问List泛型类型集合:<s:property value="contactors" /></li>
<li>访问List泛型类型集合中某个对象:<s:property value="contactors[1]" /></li>
<li>访问List泛型类型集合中某个属性的集合:<s:property value="contactors.{email}" /></li>
<li>访问List泛型类型集合中某个属性的集合中的某个属性:<s:property value="contactors.{email}[0]" /></li><br>
<li>访问Set泛型类型:<s:property value="cities" /></li><br>
<li>访问Map泛型类型的集合:<s:property value="dics" /></li>
<li>访问Map泛型类型的集合的keys:<s:property value="dics.keys" /></li>
<li>访问Map泛型类型的集合的values:<s:property value="dics.values" /></li>
<li>访问Map泛型类型的集合中某个key值对应的对象:<s:property value="dics[\"002\"]" /></li><!-- dics['002'] 单引号也可以 -->
<s:debug></s:debug><br>
</center>
</body>
</html>
第3步:发布运行程序,效果截图如下:
本项目的下载地址:
- S7.1_Struts2_OGNL OGNL对象导航图范例&OGNL方法调用范例还有集合访问的范例
- S7.2_Struts2_OGNL OGNL表达式的投影和选择技术范例,OGNL表达式#和$符号的使用范例,以及值栈
- VC中调用ADO对象访问数据库的范例
- 范例
- 范例
- Go语言的继承范例和重写范例还有接口范例
- AMF3+JAVA的调用范例
- AMF3+JAVA的调用范例
- dll的调用、使用范例
- AMF3+JAVA的调用范例
- AMF3+JAVA的调用范例
- ruby范例3:对象,方法调用,属性赋值
- 范例1
- OGNL 对象图导航语言
- Java的RMI(远程方法调用)的实现范例
- Java的RMI(远程方法调用)的实现范例
- 集合类处理范例
- IronPython调用系统类库方法范例
- 大规模机器学习
- 重磅微信福利
- Codeforces Round #385 (Div. 2)
- PS之滤镜
- POJ 3468 A Simple Problem with Integers(树状数组)
- S7.1_Struts2_OGNL OGNL对象导航图范例&OGNL方法调用范例还有集合访问的范例
- 【C#】常见的异常及异常处理方式
- redis使用list发生事故
- oracle学习之:Rman简介
- 关于Redis---数据类型-Hashes
- HDU1789
- 复习和总结H5 CSS3常见的知识点
- 面向对象之接口
- Leetcode 45. Jump Game II (Hard) (cpp)