黑马程序员技术交流社区
标题:
Spring MVC 整合 Freemarker (2)
[打印本页]
作者:
HM代景康
时间:
2013-10-1 20:22
标题:
Spring MVC 整合 Freemarker (2)
六、创建控制器Controller类
com.qisentech.springmvc.action.HelloWordController.java
[java]
/*
* 文 件 名 : com.qisentech.springmvc.action.HelloWordController.java
* 创建日期 : 2013-8-7 11:40:04
* 创 建 者 : qsyang
*/
package com.qisentech.springmvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* @author qsyang
* @version 1.0
*/
public class HelloWordController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest hsr
, HttpServletResponse hsr1) throws Exception {
ModelAndView mv = new ModelAndView("hello");
mv.addObject("title", "Spring MVC And Freemarker");
mv.addObject("content", " Hello world , test my first spring mvc ! ");
return mv;
}
}
/*
* 文 件 名 : com.qisentech.springmvc.action.HelloWordController.java
* 创建日期 : 2013-8-7 11:40:04
* 创 建 者 : qsyang
*/
package com.qisentech.springmvc.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* @author qsyang
* @version 1.0
*/
public class HelloWordController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest hsr
, HttpServletResponse hsr1) throws Exception {
ModelAndView mv = new ModelAndView("hello");
mv.addObject("title", "Spring MVC And Freemarker");
mv.addObject("content", " Hello world , test my first spring mvc ! ");
return mv;
}
}
代码中new ModelAndView("hello")中的hello就是模板文件的名称
由于我们的Freemarker的模板文件是配置在WEB-INF/view下,参考applicationContext.xml配置
<property name="templateLoaderPath" value="/WEB-INF/view/" />
所以我们需要在WEB-INF/view下新建模板文件,名称(hello.ftl)
[html]
<!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=UTF-8">
<title>${title}</title>
</head>
<body>
${content}
</body>
</html>
<!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=UTF-8">
<title>${title}</title>
</head>
<body>
${content}
</body>
</html>
七、将控制器交给Spring MVC进行拦截
添加以下内容到springmvc-servlet.xml
[html]
<!-- 地址转发器 -->
<bean name="HelloAction" class="com.qisentech.springmvc.action.HelloWordController" />
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 映射URL地址 -->
<prop key="/hello">HelloAction</prop>
</props>
</property>
</bean>
<!-- 地址转发器 -->
<bean name="HelloAction" class="com.qisentech.springmvc.action.HelloWordController" />
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 映射URL地址 -->
<prop key="/hello">HelloAction</prop>
</props>
</property>
</bean>
以上步骤处理好,我们就可以运行了
页面输出:
Hello world , test my first spring mvc !
就此,我们的第一个Spring MVC + Freemarker的示例结束了!
需要源码的,可以留言,留下邮箱,我看到了给你发过去!
附上项目结构图:
http://java.chinaitlab.com/Spring/940760_3.html
作者:
啊邓
时间:
2014-7-29 17:35
592283370@qq.com
Spring MVC + Freemarker的源码 谢谢啦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2