现在的位置: 首页 > 综合 > 正文

struts2 支持velocity的模板布局功能

2014年01月14日 ⁄ 综合 ⁄ 共 7257字 ⁄ 字号 评论关闭

struts2 是支持 velocity 的,所以不需要什么大规模更改.大多就是测试的时候按提示添加缺少的 class.这里让它支持 velocity 的布局功能

 

 

首先在项目中新建个类,路径如下.这个类改写了 struts2 里的 velocity 类

 

 

WEB-INF下新建个文件.命名:velocity.properties,内容如下(当然可以自己再加别的),用来存放velocity相关的一些信息:

tools.view.servlet.layout.directory =/template/
tools.view.servlet.layout.default.template=defalut.vm
input.encoding=UTF-8
output.encoding=UTF-8

 

tools.view.servlet.layout.directory =/template/  这行假定所有的 .vm 模板文件都在根目录的
/template 文件夹下,下面的login.vm和blank.vm 都是在这个目录下.

 

接着配置 struts.xml,在 struts 根节点下加入:

<constant name="struts.velocity.configfile" value="/WEB-INF/velocity.properties"/>

 

还是 struts.xml 下面这个配置用来输出,注意黑体 :

<package name="default" namespace="/" extends="struts-default">
        <result-types>
            <result-type name="velocity" class="struts2.velocity.VelocityLayoutResult" />
        </result-types>

        <action name="login" class="com.exdoit.Main" method="login">
            <result type="velocity ">/template/login.vm </result>
        </action>
 </package>

 

 

 

这个是 login.vm 的代码 #set($layout = "blank.vm") 用来设置布局模板 .

#set($layout = "blank.vm")
<ul>
    <li><span>帐号:</span><input type="text" name="name" id="loginForm_name" /><span><a href="" target="_blank">用户注册</a></span></li>
    <li><span>密码:</span><input type="password" name="pw" id="loginForm_pw" /><span><a href="" target="_blank">忘记密码</a></span></li>
    <li><input type="button" value="登录" id="loginForm_logonbtn" /><input type="button" value="取消" id="loginForm_cancelbtn" /></li>
</ul>

 

 

这个是 blank.vm 的代码 ${screen_content} 就是要替换成 login.vm 的内容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>${page_title}</title>
<link media="screen,print" rel="stylesheet" href="/common/css/inc.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="/common/JS/jquery/jquery.js" charset="UTF-8"></script>
<script type="text/javascript" src="/common/JS/common.js"></script>
</head>
<body>
${screen_content}
</body>
</html>

 

 

运行测试...

 

 


 

注,struts2.velocity.VelocityLayoutResult 这个类是Google来的.当时忘了记源地址.对原作者表示感谢...

我修改的原版的其中几行.用来设置模板路径这些问题.毕竟有可能不是所有的 vm 文件都一定在 velocity.properties 指定的目录下.

 

 

当路径 @/ 开头时,如:#set($layout = "@/other/layout/blank.vm") 表示这路径是从根目录开始.无视 velocity.properties 的路径配置.

在当前项目配置中假如:

#set($layout = "@/other/layout/blank.vm")  == /other/layout/blank.vm

#set($layout = "other/layout/blank.vm")  == /template /other/layout/blank.vm

 

 

在action 中设置 request.setAttribute("layoutDir", "/abc/") ,可以指定result 里vm的路径.无视 velocity.properties 的路径配置,注意这个是仅这个 action 的输出结果会换路径,其它action不受影响 .

在当前项目配置中假如:

#set($layout = "@/other/layout/blank.vm")  == /other/layout/blank.vm

#set($layout = "other/layout/blank.vm")  == /abc /other/layout/blank.vm

 

 

 

抱歉!评论已关闭.