关于OGNL对类属性的访问

Caretaker9 2015-11-25 09:57:12
首先是简单的Student类
package com.aihidao.OGNL;

public class Student {

public String name;
public int id;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String say(){
return "我是一个学生";
}

}


关于OGNL的Action
package com.aihidao.OGNL;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class OGNLAction extends ActionSupport{
private String name;
private Student student;
private Map<String,Object> request=null;
public OGNLAction(){

student=new Student();
student.setId(1);
student.setName("王大锤");
name="皮卡丘";
ActionContext ac=ActionContext.getContext();
request=(Map<String,Object>)ac.get("request");
request.put("info", "万万没想到,啦啦啦啦啦啦!");

}

public String execute() throws Exception{
System.out.println("进入execute()!");
System.out.println(student.getId());
System.out.println(student.getName());
System.out.println(student.say());
return "success";
}
}


关于struts配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myPackage" extends="struts-default">
<action name="ognl" class="com.aihidao.OGNL.OGNLAction">
<result>/success.jsp</result>
</action>
</package>
</struts>



关于web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Web04_OGNL</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list><!-- Struts2过滤器 -->
<filter>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- Struts2过滤器映射 -->
<filter-mapping>
<!-- 过滤器名称 -->
<filter-name>struts2</filter-name>
<!-- 过滤器映射 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


关于success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" import="com.opensymphony.xwork2.ognl.OgnlValueStack"%>
<%@ 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 'success.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>
普通name:<s:property value="#name"/><br><hr>
学生姓名:<s:property value="#student.name"/><br>
学生ID::<s:property value="#student.id"/><br>
学生说:<s:property value="#student.say()"/><br>
学生信息:<s:property value="#request.info"/>
</body>
</html>


index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<a href="ognl.action" >查看用户信息</a>
</body>
</html>


关于结果


为什么没有显示其他值?
...全文
79 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Wonep 2015-11-25
action 没有seter geter 方法。 要 set get student
回复
去掉#号试试
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-25 09:57
社区公告
暂无公告