struts2配置问题

来源:互联网 发布:淘宝评论失败怎么回事 编辑:程序博客网 时间:2024/05/01 01:44

打开index.jsp页面,编码格式改为utf-8,加入<%@ taglib uri="/struts-tags"  prefix="s" %>,就可以使用struts的标签,具体代码如下:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="/struts-tags"  prefix="s" %>  
  3. <%  
  4. String path = request.getContextPath();  
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.       
  13.     <title>My JSP 'index.jsp' starting page</title>  
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   </head>  
  23.     
  24.   <body>  
  25.   <ol>  
  26.     <li>访问静态方法:<s:property value="@com.gk.StaticTest@s()"/></li>  
  27.     <li>访问静态属性:<s:property value="@com.gk.StaticTest@str"/></li>  
  28.     <li>访问java.lang.Math类的静态方法:<s:property value="@@max(2,5)"/></li>  
  29.     <s:debug></s:debug>  
  30.     </ol>  
  31.   </body>  
  32. </html>  


一定要在struts.xml配置文件中配置允许使用OGNL访问静态方法,否则不能使用OGNL访问静态方法,必须在struts.xml文件中加入这行代码:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>  

其中完整struts.xml文件如下:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  
  8.     <constant name="struts.devMode" value="true"></constant>  
  9.     <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>  
  10. </struts>  

0 0
原创粉丝点击