无论做什么,都请记得那是为自己而做,那就毫无怨言。不刻意的装饰自己,那些都是给别人看的,而真的在乎我的人不会在乎这些。
基于网络改进为:最正常版本
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 针对freemarker的视图配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="order" value="5" />
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
Controller建立
在url上敲http://localhost:8080/welcome就会到WEB-INF/view/welcom.ftl页面渲染数据
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
public class SpringMvcController { @RequestMapping(value="/welcome",method={RequestMethod.GET})
public ModelAndView getFirstPage(HttpServletRequest request) {
//welcom就是视图的名称(welcom.ftl)
ModelAndView mv = new ModelAndView("welcom");
mv.addObject("name", "My First Spring Mvc");
return mv;
}
}
welcom.ftl页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello ${name}
</body>
</html>
页面出来的效果:
Hello My First Spring Mvc
到此这篇关于spring mvc整合freemarker基于注解方式就介绍到这了。做人聪不聪明其实不重要,甚至情商不高也没关系,但一定得够大气。做人线条粗一点不是坏事,千万不要几句嘲讽就让你垂头丧气、一点打击就让你万念俱灰。人生永远是这个道理:在意的越多,遇到的麻烦就会越多。什么都不在乎的人,反倒一点麻烦都没有。更多相关spring mvc整合freemarker基于注解方式内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!