本人在测试Struts2.0中的表达式语言的时候,程序报错,可是检查却好像并没有什么错误啊!还真是郁闷了,现在贴出来想和大家一起探讨一下: 
首先是一个Book类: 
package tutorial.model; 
public class Book 

    private String isbn; 
    private String title; 
    private double price;     public Book() 
    { 
    }     public Book(String isbn,String title,dobule price) 
    { 
        this.isbn = isbn; 
        this.title = title; 
        this.price = price; 
    }     public String getIsbn() 
    { 
        return isbn; 
    }     public void setIsbn(String isbn) 
    { 
          this.isbn = isbn; 
    }       public double getPrice() 
      { 
          return price; 
      }       public void setPrice(double price) 
      { 
          this.price = price; 
      }       public String getTitle() 
      { 
              return title; 
      }       public void setTitle(String title) 
      { 
          this.title = title; 
      } 
} Action类: BookAction.java package tutorial.action import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; import javax.servlet.ServletContext; 
import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; 
import org.apache.struts2.interceptor.SessionAware; 
import org.apache.struts2.util.ServletContextAware; import tutorial.model.Book; import com.opensymphony.xwork2.ActionSupport;   public class OgnlAction extends ActionSupport implements ServletRequestAware,SessionAware,ServletContextAware 

    private static final long serialVersionUID = 1L;     private HttpServletRequest request; 
    private Map <String,String> session; 
    private ServletContext application; 
    private List <Book> books;     public void setServletRequest(HttpServletRequest request) 
    { 
          this.request = request; 
    }     @SuppressWarnings("unchecked") 
    public void setSession(Map session) 
    { 
          this.session = session; 
    }     public void setServletContext(ServletContext application) 
    { 
        this.application = application; 
    }     public List <Book> getBooks() 
    { 
        return books; 
    }   @Override 
  public String execute() 
  { 
        request.setAttribute("userName","Max From request"); 
        session.put("userName","Max From session"); 
        application.setAttribute("userName","Max From application");         books = new LinkedList <Book>(); 
        books.add(new Book("978-0735619678", "Code Complete, Second Edition", 32.99)); 
        books.add(new Book("978-0596007867", "The Art of Project Management", 35.96)); 
        books.add(new Book("978-0201633610", "Design Patterns: Elements of Reusable Object-Oriented Software", 43.19)); 
        books.add(new Book("978-0596527341", "Information Architecture for the World Wide Web: Designing Large-Scale Web Sites", 25.19)); 
        books.add(new Book("978-0735605350", "Software Estimation: Demystifying the Black Art", 25.19)); 
        
        return SUCCESS;   } 
} Ognl.jsp <%@ page language="java" contentType="text/html;charset=utf-8" 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>Struts OGNL Demo </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> 
    <h3>访问OGNL上下文和Action上下文 </h3> 
    <p>parameters: <s:property value="#parameters.userName"/> </p> 
    <p>request.userName: <s:property value="#request.userName"/> </p> 
    <p>session.userName: <s:property value="#session.userName"/> </p> 
    <p>application.userName: <s:property value="#application.userName"/> </p> 
    <p>attr.userName: <s:property value="#attr.userName"/> </p> 
    <hr/> 
    <h3>用于过滤和投影集合 </h3> 
    <p>Books more than $35 </p> 
    <ul> 
    <s:iterator value="books.{?#this.price>35}"> 
      <li> <s:property value="title"/>-$ <s:property value="price"/> </li> 
    </s:iterator> 
    </ul> 
    <p> 
    The price of "Code Complete,Second Edition"is: <s:property value="books.{?#this.title=='Code Complete,Second Edition'}.{price}[0]"/> 
    </p> 
    <hr/> 
    <h3>构造Map </h3> 
    <s:set name="foobar" value="#{'foo1':'bar1','foo2':'bar2'}"/> 
    <p>The value of key "foo1" is <s:property value="#foobar['foo1']"/> </p> 
  </body> 
</html> web.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts2 OGNL </display-name> <filter> 
<filter-name>struts2 </filter-name> 
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp </filter-class> 
</filter> <filter-mapping> 
<filter-name>strut2</filter-name> 
<url-pattern>/* </url-pattern> 
</filter-mapping> <filter> 
<filter-name>struts2 </filter-name> 
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> 
</filter> <filter-mapping> 
<filter-name>struts2 </filter-name> 
<url-pattern>/* </url-pattern> 
</filter-mapping> 
</web-app> 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> 
  <constant name="struts.devMode" value="true"/> 
  <package name="Struts-OGNL-DEMO" extends="struts-default"> 
  <action name="Ognl" class="tutorial.action.OgnlAction"> 
    <result>/Ognl.jsp </result> 
  </action> 
  </package> 
</struts> 
错误信息: 
2008-7-5 11:17:27 com.opensymphony.xwork2.util.OgnlValueStack logLookupFailure 
警告: Caught an exception while evaluating expression 'books.{?#this.price>35}' against value stack 
java.lang.NullPointerException 
at ognl.OgnlRuntime$ClassCache.get(OgnlRuntime.java:147) 
at ognl.OgnlRuntime.getHandler(OgnlRuntime.java:1578)f\ 
at ognl.OgnlRuntime.getElementsAccessor(OgnlRuntime.java:1545) 
at ognl.ASTSelect.getValueBody(ASTSelect.java:53) 
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170) 
at ognl.SimpleNode.getValue(SimpleNode.java:210) 
at ognl.ASTChain.getValueBody(ASTChain.java:109) 
at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170) 
at ognl.SimpleNode.getValue(SimpleNode.java:210) 
at ognl.Ognl.getValue(Ognl.java:333) 
at ognl.Ognl.getValue(Ognl.java:310) 
at com.opensymphony.xwork2.util.OgnlUtil.getValue(OgnlUtil.java:190) 
at com.opensymphony.xwork2.util.OgnlValueStack.findValue(OgnlValueStack.java:208) 
at org.apache.struts2.components.Component.findValue(Component.java:254) 
at org.apache.struts2.components.IteratorComponent.start(IteratorComponent.java:210) 
at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:54) 
at org.apache.jsp.Ognl_jsp._jspx_meth_s_005fiterator_005f0(Ognl_jsp.java:257) 
at org.apache.jsp.Ognl_jsp._jspService(Ognl_jsp.java:118) 
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:413) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:99) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634) 
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445) 
at java.lang.Thread.run(Thread.java:595) 
2008-7-5 11:17:27 com.opensymphony.xwork2.util.OgnlValueStack logLookupFailure 
警告: NOTE: Previous warning message was issued due to devMode set to true. 
2008-7-5 11:17:27 com.opensymphony.xwork2.util.OgnlValueStack logLookupFailure \\\\\\
jar包已经添加了!

解决方案 »

  1.   

    应该是你过滤value>35那块代码的问题,仔细检查下,和这相关的代码
      

  2.   

    在Action的execute方法中,books这个变量未写到request中,jsp拿不到books的值.
    request.setAttribute("books",books); 
      

  3.   

    books.{?#this.price>35},books.{?#this.title=='Code Complete,Second Edition'}.{price}[0]这两个出现的问题是一样的,都是ognl 那边不能识别,我用的是Struts2,ognl的jar包也导入进去了,不知道哪里出错
      

  4.   

    写这个value="#{'foo1':'bar1','foo2':'bar2'}"的时候也会出现警告信息,难道真的是jar包失效