springmvcf
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
Spring MVC 是一个强大的Java Web开发框架,而FreeMarker则是一个模板引擎,常用于视图层的渲染。在本文中,我们将深入探讨如何结合Spring MVC与FreeMarker构建一个基于注解的Web应用程序环境。 我们需要在项目中引入Spring MVC和FreeMarker的相关依赖。如果你使用的是Maven,可以在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.x.y.RELEASE</version> </dependency> <!-- FreeMarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.x.y</version> </dependency> </dependencies> ``` 这里的版本号需要根据当前最新的稳定版本进行替换。 接下来,配置Spring MVC。在`web.xml`中,我们需要设置DispatcherServlet并指定其配置文件: ```xml <web-app> ... <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ... </web-app> ``` 然后在`spring-servlet.xml`中配置FreeMarker视图解析器: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/views/"/> <property name="freemarkerSettings"> <props> <prop key="defaultEncoding">UTF-8</prop> <prop key="template_update_delay">0</prop> </props> </property> <property name="freemarkerVariables"> <map> <entry key="xml_escape" value-ref="fmXmlEscape" /> </map> </property> </bean> <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".ftl"/> </bean> ... </beans> ``` 这里指定了FreeMarker模板的根路径以及模板文件的后缀名`.ftl`。 现在我们来创建一个简单的Controller。使用Spring的@Controller和@RequestMapping注解,我们可以将HTTP请求映射到特定的方法: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloWorldController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String sayHello() { return "hello"; } } ``` 在这个例子中,当我们访问`/hello`时,Spring MVC会调用`sayHello`方法,并返回"hello"作为视图名称。由于我们之前配置了FreeMarkerViewResolver,所以它会寻找名为`hello.ftl`的FreeMarker模板文件。 在`/WEB-INF/views`目录下创建`hello.ftl`文件,编写HTML模板: ```html <!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <h1>Welcome to Spring MVC + FreeMarker!</h1> <p>This is a simple example.</p> </body> </html> ``` 当用户访问`/hello`时,他们将会看到这个简单的欢迎页面。 至此,我们已经成功地搭建了一个基于注解的Spring MVC + FreeMarker环境。你可以在此基础上扩展更多的Controller和模板,实现更复杂的业务逻辑和视图渲染。在实际开发中,还可以利用Spring MVC的ModelAndView、ModelMap等特性,以及FreeMarker的高级功能,如宏定义、条件判断、循环结构等,来增强应用的功能性和灵活性。
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![thumb](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/TXT.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
- 1
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- liu11193730122013-12-17代码很好,不过少一个jar包~自己加上就行了,commons-logging.jar
![avatar](https://profile-avatar.csdnimg.cn/88b001ab8cb0448ca66c0935d1ddba6c_u012242610.jpg!1)
- 粉丝: 2
- 资源: 36
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 在python中操作excel表格.py
- matlab实现比例导引三自由度弹道仿真系统设计-PDR-导弹控制-导弹设计-matlab
- (源码)基于ARM架构的优化库实现.zip
- 北京+天津+河北+山东共4省市超清超详细地质空间分布图-JPG格式
- 基于LabVIEW和Matlab绘制的电机、控制器与系统效率MAP图(附源码及测试数据),基于LabVIEW与Matlab的电机、控制器及系统效率MAP图绘制与源码解读(附测试数据),基于labvie
- matlab实现激光雷达点云数据ICP配准计算的深度研究-激光雷达-点云数据-ICP配准计算-matlab
- 非线性模型预测控制NMPC在无人船轨迹跟踪与避障系统中的应用研究:结合MATLAB编程实现与Casadi仿真软件的分析探讨,非线性模型预测控制NMPC在无人船轨迹跟踪与避障系统中的应用研究-基于Ma
- 基于Quartus平台的五级流水线RISC-V CPU设计及其功能验证报告-包括Verilog代码、汇编代码与详细设计文档,基于Quartus平台的RISC-V五级流水线CPU设计与验证:Veril
- python代码按坐标截图.py
- python从视频中截图代码.py
- python三行代码截图工具.py
- python烟花代码电脑截图.py
- python怎么截取代码长图.py
- python数据清洗代码截图.py
- 网页定时截图python代码.py
- 数字识别python代码截图.py
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)