当前位置: 首页 > 工具软件 > min-velocity > 使用案例 >

spring-velocity-jsp

冯和硕
2023-12-01

1,使用annotation 声明控制器   

    <mvc:annotation-driven />

<context:component-scan base-package="com.sf.cc.controller" />

2.velocity引擎
<bean id="velocityConfigurer"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath">
<value>/WEB-INF/vm/</value>
</property>
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>

3.velocity解析器(order越小,先匹配,如果找不到velocity模版,在找jsp视图)
<bean id="viewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="suffix">
<value>.vm</value>
</property>
<property name="contentType">
<value>text/html;charset=utf-8</value>
</property>
<property name="order" value="1"></property>
</bean>

4.jsp解析器(如果jsp视图的order小于velocity,如果找不带jsp视图,直接404,鄙人还在寻求why,知这麻烦留言)
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
<property name="order" value="2"></property>

</bean>

5.html代码

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".v").on("click", function() {
jQuery(".vif").attr("src", "vm/velo.hm");
});
});
</script>
<title>Velocity-Ajax</title>
</head>
<body>
<iframe class="vif"></iframe>
<input class="v" type="button" value="olo" />
</body>

6.模版

#set( $this = "Velocity")
$this is great!
#foreach( $name in $list )
    $name
#end
#set( $condition = true)
#if ($condition)
 The condition is true!
#else
 The condition is false!
#end
author $author

7.控制器

@Controller
public class VelocityController {
// http://xxx.renren.com/vm/velo.hm
@RequestMapping("/vm/velo")
public ModelAndView v() {
ModelAndView mav = new ModelAndView("hello");
ArrayList<String> list = new ArrayList<String>();
list.add("<br/>System.currentTime:" + System.currentTimeMillis());
list.add("<br/>Welcome Velocity<br/>");
mav.addObject("list", list);
mav.addObject("author", "<br/>work");
return mav;
}
}


 类似资料: