struts2结合freemarker

(1)在struts.xml添加跳转:
 <package name="loginPackage" extends="jason-default">
     <action name="jLogin" class="JLogin">
       <result name="success" >/WEB-INF/jason/index.jsp</result>
        <result name="error">/WEB-INF/jason/login.jsp</result>
        <result name="input">/WEB-INF/jason/login.jsp</result>
         <result name="search" type="freemarker">/WEB-INF/templates/jsearch.ftl</result>
        <interceptor-ref name="jLoginStack"/>
     </action>
 </package>
(2)
在ACTION中,进行设置:
..........
   Hits hits = JLuceneUtils.luceneSearch(indexPath, searchMess);
   System.out.println("----------hits.length():" + hits.length());
   for (int a = 0; a < hits.length(); a++) {
    Document doc2 = (Document) hits.doc(a);
    System.out.println(searchMess + "的值是:"+ doc2.get("confContext"));
    ActionContext.getContext().getSession().put("jsearch_value",
doc2.get("confContext"));
   }
    ActionContext.getContext().getSession().put("jsearch_cout", hits.length());
   ActionContext.getContext().getSession().put("jsearch_name", searchMess);
(3)
设置模板文件:jsearch.ftl:
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"] />
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jason search result</title>
</head>
<body>
<table width="98%" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr>
    <td><strong>以面是jason用lucene查询的结果,共有${jsearch_cout}条符合:</strong></td>
  </tr>
  <tr>
    <#-- freemarker插值(如下,是利用EL语言,即将ACTION中的值先放入session/request/application) -->
    <td>查询内容:${jsearch_name} --- 查询值:${jsearch_value}</td>

  </tr>
  <tr>
    <td>
    <@s.url action="jLogin!login" id="login" />
    <a href="${login}">返回登录主页</a>

    </td>
  </tr>
</table>
</body>
</html>
(4)
如果要使用STRUTS2.0的标签,如上面.就要添加STRUTS2.0的标签库:
web.xml添加:
    <servlet>
        <servlet-name>JspSupportServlet</servlet-name>
        <servlet-class>
             org.apache.struts2.views.JspSupportServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
在jsearch.ftl最前面添加:
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"] />

你可能感兴趣的:(freemarker,struts,Lucene,search,action,login)