[size=medium]学习IBATIS在web中的应用[/size]
[b]采用的jar包:[/b]
velocity-1.6.4.jar
velocity-tools-view-2.0.jar
在web。xml中的配置:
这样就可以在前端使用velocity,除此之外,还可以在Java代码中设置所使用的变量的值:
在前端使用:
另外,可以在模板中设置内置的变量:
上述的class可换为自己的类。实现自己的类内嵌。
[b]采用的jar包:[/b]
velocity-1.6.4.jar
velocity-tools-view-2.0.jar
在web。xml中的配置:
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>velocity</servlet-name>
<url-pattern>*.vm</url-pattern>
</servlet-mapping>
这样就可以在前端使用velocity,除此之外,还可以在Java代码中设置所使用的变量的值:
import org.apache.velocity.VelocityContext;
users = userService.getAllUsers();
if (users.size() != 0)
{
VelocityContext context=new VelocityContext();
context.put("users", users);
return SUCCESS;
}
在前端使用:
<table>
<tr>
<td>userId</td>
<td>userName</td>
<td>userAddress</td>
</tr>
#foreach( $user in $users)
<tr>
<td>$user.getId()</td>
<td>$user.getName()</td>
<td>$!user.getAddress()</td>
</tr>
#end
</table>
另外,可以在模板中设置内置的变量:
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>org.apache.velocity.tools.view.servlet.VelocityViewServlet</servlet-class>
<init-param>
<param-name>toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>
</init-param>
</servlet>
<toolbox>
<tool>
<key>date</key>
<scope>application</scope>
<class>org.apache.velocity.tools.generic.DateTool</class>
<parameter name="format" value="yyyy-M-d"/>
</tool>
</toolbox>
上述的class可换为自己的类。实现自己的类内嵌。