---
title: "Struts2学习笔记"
author: "hotforcc"
date: "2007-08-25T09:15:35Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2007-08-25T09:15:35Z"
id: 116591
url: "https://hotforcc.iteye.com/blog/116591"
---

Struts2学习笔记  
转载资料 from http://www.blogjava.net/max

Question: constant name="struts.action.extension" value="action"   &lt;constant name="struts.action.extension" value="action"&gt;&lt;constant value="action" name="struts.action.extension"&gt;问题  
Answer:这个是系统从&lt;/constant&gt;struts.properties&lt;constant name="struts.action.extension" value="action"&gt;默认继承这个配置，所以用&lt;s:form&gt;的时候会自动加上.action  
&lt;/s:form&gt;&lt;/constant&gt;The URL extension to use to determine if the request is meant for a Struts action  
用URL扩展名来确定是否这个请求是被用作Struts action，其实也就是设置 action的后缀，例如login.do的'do'字。  
&lt;s:form&gt;s:form 的时候用  
&lt;/s:form&gt;&lt;constant name="struts.action.extension" value="action"&gt;&lt;s:form&gt;  
Question:Struts2的xml的配置  
Answer:Struts2默认会读取classpath下的struts-default.xml,struts-plugin.xml,struts.xml这三个文件。  
struts-plugin.xml的位置struts-plugin.xml会在你下载的plugin的jar包中，如struts2-spring-plugin-2.0.6.jar。

Question:java.lang.NullPointerException 异常  
Answer:应该没在web.xml有配置struts 2的filter，试下将以下列代码加到web.xml的&lt;web-app&gt;&lt;/web-app&gt;元素之间：  
好像需要Tomcat 5.5以上，5.0会有问题，诸如NullPointerExceptio之类的 

Question:Could not find or error in struts.properties  
java.lang.IllegalStateException: struts.properties missing  
问题解决:把struts.properties 放到classes下即可了

Question:&lt;s:form&gt;的action才与struts.xml中的Action名一样，  
而&lt;/s:form&gt; &lt;/s:form&gt;&lt;/constant&gt; &lt;/constant&gt;

中的action应为你的Action的路径的全名如/mypath/myaction.action;  
Answer:Action中的属性值可以通过Javabean规范与请求是的参数绑定，  
所以&lt;s:textfield&gt;等Form标志的name属性必须与Action属性名一致。

Question:取得Servlet API中的一些对象，如request、response或session等，应该怎么做？  
Answer:com.opensymphony.xwork2.ActionContext,可以通过它的静态方法getContext()获取当前Action的上下文对象,  
非IOC:  
另外，org.apache.struts2.ServletActionContext作为辅助类（Helper Class），可以帮助您快捷地获得这几个对象,  
HttpServletRequest request = ServletActionContext.getRequest();  
HttpServletResponse response = ServletActionContext.getResponse();  
HttpSession session = request.getSession();  
IOC:  
import org.apache.struts2.interceptor.ServletRequestAware;  
import org.apache.struts2.interceptor.ServletResponseAware;  
import org.apache.struts2.interceptor.SessionAware;  
publicclass IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {  
   private String message;  
   private HttpServletRequest request;  
   private HttpServletResponse response;   
}

Question:表达式的问题，有${表达式}、带有#的表达式（如上文中的#session.msg），还有%开始的表达式，  
这些好像都是取出里面的值，请问这些$、#、%开头的表达式有什么不同？各自在什么情况下使用？  
Answer:OGNL中的${...}可以在定义Action的result时，传递变量，如/deleteBook.action?isbn=${isbn}。也可以在国际化时引用变量；  
#用于在页面上引用ActionContext的值，也可以构造映射（Map）等，如#request.xxx；  
%{...}在标签属性中引用变量，如&lt;s:url value="Edit.action?isbn=%{isbn}"&gt;

Question:如何得到cookies  
Answer:先在Action中拿到HttpServletRequest的对象（请参考《Struts 2.0的Action讲解》），然后调用request.getCookies()。

Question:  
页面就可以直接获取${message}，请问这默认是request级别的吗?  
如果是session级别的，是不是要在取得session后  
在代码中明确写入，session.setAttribute(xx.xxxx)  
Answer:这些值是放在ActionContext中的，所以不是request或session等  
&lt;/s:url&gt;&lt;/s:textfield&gt;