二.相关方法:
1.将导航之类的用统一的一个文件来管理.
<jsp:include page="common/header.html" />
如果include是一个JSP文件,可能会产生乱码,需要在头部加:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>以保证编码的统一.
2.加入了struts2相关jar包,据说Struts2与GAE有一定的冲突,所以我又添加了一个struts-gae.jar,这个是从网上找到的用于整合两者的包.可以从:
http://code.google.com/p/struts2-gae/downloads/list下载得到.
3.使用JDO中排序得到最新的article:
Query query = PM.newQuery(Article.class);
query.setOrdering("time desc");
List<Article> articles = (List<Article>)query.execute();
query排序及获得所有存储的对象:
Query query = PM.newQuery(Article.class);
query.setOrdering("time desc");
articles = (List<Article>)query.execute();
4.
struts2中action获取jsp页面参数
ActionContext context = ActionContext.getContext();
Map params = context.getParameters();
String username = (String) params.get("username");
5.Blog 中id的设置方法,我以前没用过,感觉很不错:
Date date = new Date();
Long id = date.getTime()
6.自定义filter需实现filter接口,实现doFilter方法,此外,Filter是一条链,如果想继续其它的Filter,代码如下:
// Pass control on to the next filter
chain.doFilter(request, response);
7.在servlet中实现页面跳转:
req.setAttribute("article", article);
req.getRequestDispatcher("/index.jsp").forward(req,resp);
8.传值问题(从Servlet向JSP页面传值的操作):在页面中使用<s:if></s:if>,注意:s:if中判断的对象必须包含在action中(也就是必须是对应action的成员变量),对于只是Servlet中request传递过来的参数是无效的!
<s:if test="#article == null"> <jsp:forward page="/initIndex" /> </s:if>
9. Action中获得Session及request:
//在Action中获得request对象.
HttpServletRequest request = ServletActionContext.getRequest ();
//在Action中获得Session对象:
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
//在Action中获得Session的拷贝:
ActionContext.getContext().getSession();
10.JSP页面EL表达式无效的问题,解决方法:
方法1.在JSP页面头部加:<%@ page isELIgnored="false" %>
方法2.在WEB.xml中添加:
11.要想在首页实现jsp:forward 跳转至Action
方法一:需要在web.xml中配置:
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern >/*</url-pattern> <dispatcher>REQUEST</dispatcher>//1 <dispatcher>FORWARD</dispatcher> //2 加多这两行就行了 </filter-mapping>
页面上:
<jsp:forward page="xxxx.action"></jsp:forward>
方法二:使用Filter
12.页面获得对象的属性值
<jsp:getProperty name="article" property="time" />
注意:s:property与jsp:getProperty是不一样的:
s:property获得的是在Action中存储的对象(相当于Servlet中getParameter所得到的对象),而jsp:getProperty使用的是request/session/application中Attribution存储的对象.
13.对象与常量比较时,将常量放前面不会出现NullPointException
14.Interceptor的配置
注意:默认情况下action是有默认拦截器的,但是如果自己配置了拦截器,默认拦截器将不起作用,需要将默认拦截器手动添加.
15.invocation.invoke()。这是ActionInvocation中的方法,而ActionInvocation是Action调度者,所以这个方法具备以下2层含义:
1. 如果拦截器堆栈中还有其他的Interceptor,那么invocation.invoke()将调用堆栈中下一个Interceptor的执行。
2. 如果拦截器堆栈中只有Action了,那么invocation.invoke()将调用Action执行。
所以,我们可以发现,invocation.invoke()这个方法其实是整个拦截器框架的实现核心。
15输出HTML标签:
<c:out value="${article.content}" escapeXml="false" />
<!--EndFragment-->