`
Adams1986
  • 浏览: 11462 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Struts2学习笔记(转载)

阅读更多
Struts2学习笔记

Question: constant name="struts.action.extension" value="action"    问题
Answer:这个是系统从struts.properties默认继承这个配置,所以用的时候会自动加上.action
The URL extension to use to determine if the request is meant for a Struts action
用URL扩展名来确定是否这个请求是被用作Struts action,其实也就是设置 action的后缀,例如login.do的'do'字。
s:form 的时候用

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的元素之间:
好像需要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:的action才与struts.xml中的Action名一样,
而 <form> 中的action应为你的Action的路径的全名如/mypath/myaction.action;
Answer:Action中的属性值可以通过Javabean规范与请求是的参数绑定,
所以等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;
%{...}在标签属性中引用变量,如

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

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

转载资料 from http://www.blogjava.net/max
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics