springmvc与常用的模板引擎都有集成.
1.freemarker
<!-- freemarker的配置 --> <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/freemarker/" /> <property name="defaultEncoding" value="utf-8" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">10</prop> <prop key="locale">zh_CN</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="number_format">#.##</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> <property name="suffix" value=".ftl" /> <property name="contentType" value="text/html;charset=utf-8" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> <property name="exposeSpringMacroHelpers" value="true" /> </bean>
2. velocity
2.1 xml配置
<!-- velocity的配置 --> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="/WEB-INF/template/" /> <property name="velocityProperties"> <props> <prop key="input.encoding">UTF-8</prop><!-- 指定模板引擎进行模板处理的编码 --> <prop key="output.encoding">UTF-8</prop><!-- 指定输出流的编码 --> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="suffix" value=".vm" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="dateToolAttribute" value="dateTool" /> <property name="numberToolAttribute" value="numberTool" /> <property name="exposeRequestAttributes" value="true" /> <property name="exposeSessionAttributes" value="true" /> </bean>
2.2 pom依赖
此外,pom中也需要依赖下面两个jar:
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-tools</artifactId> <version>2.0</version> </dependency>
2.3 java及html
//java public ModelAndView getArts(String fuzzyName) throws Exception { List<GaoKaoScoreDto> gaoKaoScoreList=dao.getFuzzyQuery(fuzzyName); //loads the /WEB-INF/template/gaoKaoScore.vm return new ModelAndView("gaoKaoScore","gaoKaoScoreList",gaoKaoScoreList); } //velocity in html <tbody> #foreach ($element in $gaoKaoScoreList) <tr> <td>$element.no</td> <td>$element.name</td> <td>$element.idCard</td> <td>$element.gender</td> <td>$element.type</td> <td>$element.chinese</td> <td>$element.math</td> <td>$element.comprehensive</td> <td>$element.english</td> <td>$element.total</td> <td>$element.luoFenplusZhaoGuFen</td> <td>$element.ranking</td> </tr> #end </tbody>
时间: 2024-09-22 16:40:18