基于注解的SpringMVC+freemarker demo实例
web项目图
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <!-- 配置DispatcherServlet --> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定spring mvc配置文件位置 不指定使用默认情况 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> <!--默认:/WEB-INF/<servlet-name>-servlet.xml classpath方式:<param-value>classpath:/spring-xml/*.xml</param-value> --> </init-param> <!-- 设置启动顺序 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置映射 servlet-name和DispatcherServlet的servlet一致 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern><!-- 拦截以/所有请求 --> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认注解映射支持 --> <mvc:annotation-driven/> <!-- 自动扫描包 --> <context:component-scan base-package="com.spring.freemarker" /> <!--<context:annotation-config /> 配置自动扫描包配置此配置可省略--> <!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 配置自动扫描包配置此配置可省略/>--> <!-- 配置freeMarker的模板路径 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="WEB-INF/ftl/" /> <property name="defaultEncoding" value="UTF-8" /> </bean> <!-- freemarker视图解析器 --> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".ftl" /> <property name="contentType" value="text/html;charset=UTF-8" /> <!-- 此变量值为pageContext.request, 页面使用方法:rc.contextPath --> <property name="requestContextAttribute" value="rc" /> </bean> </beans>
FreeMarkerController类
package com.spring.freemarker; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.spring.vo.User; @Controller @RequestMapping("/home") public class FreeMarkerController { @RequestMapping("/index") public ModelAndView Add(HttpServletRequest request, HttpServletResponse response) { User user = new User(); user.setUsername("zhangsan"); user.setPassword("1234"); List<User> users = new ArrayList<User>(); users.add(user); return new ModelAndView("index", "users", users); } }
User类
package com.spring.vo; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
index.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=UTF-8"> <title>Insert title here</title> </head> <body> <#list users as user> username : ${user.username}<br/> password : ${user.password} </#list> </body> </html>
部署到tomcat,运行:http://localhost:8080/springmvc/home/index
显示结果:
username : zhangsan
password : 1234
相关推荐
最简单的Maven+SpringMVC+Freemarker入门Demo
maven管理项目+springmvc+spring+hibernate+freemarker 集成框架。其中注解格式清晰,hibernate进行了代码封装,对开发效率有了提高,对异常进行了封装。freemarker也有优化,参考common包下。对日期工具类有各种...
springMVC+freemarker详细整合实例,供大家学习,如果下载下来的项目上有一个红色感吧号就把架包重新加一次,架包位于web-info下面的lib文件夹内
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
本人自己 搭建的 一个maven + springmvc + freemarker 框架,希望对你们 有所帮助。
基础框架,可满足开发需要
一个基于SpringMVC+Hibernate+Freemarker 的干净的框架
基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis+Maven项目案例 基于SpringMVC+Spring+MyBatis...
整合了springmvc+FreeMarker+shiro的小Demo!
springmvc+freemarker+easyUi 框架技术代码
基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+MyBatis+Maven项目案例源码+数据库.zip 基于SpringMVC+Spring+...
基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...
springMVC+freemarker框架demo,有兴趣的可以下载看看
springmvc+hibernate+freemarker基于注解,绝对可用,myeclipse下开发的
基于SpringMVC+Spring3+Mybatis框架的OA项目 基于SpringMVC+Spring3+Mybatis框架的OA项目 基于SpringMVC+Spring3+Mybatis框架的OA项目 基于SpringMVC+Spring3+Mybatis框架的OA项目 基于SpringMVC+Spring3+Mybatis...
基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场车牌识别系统 基于Spring+SpringMVC+Mybaits的小区停车场...
基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip ...
大型商城网站源码springmvc+freemarker+velocity+ibatis,最新最前沿的技术,特别适合二次开发
毕设项目:基于SpringMVC+MyBatis开发学生管理系统 毕设项目:基于SpringMVC+MyBatis开发学生管理系统 毕设项目:基于SpringMVC+MyBatis开发学生管理系统 毕设项目:基于SpringMVC+MyBatis开发学生管理系统 毕设项目...
简单基础,带注释的springmvc+freemarker开发登录事例