freemaker与spring的整合(三)
来源:互联网 发布:mysql主键 增加 编辑:程序博客网 时间:2024/06/05 11:05
项目是基于之前学习的maven来构建的,包的引入都在pom文件当中
项目的文件结构如图:
1.web.xml文件的编写
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:beans.xml</param-value></context-param><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>fre</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>fre</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
2.编写fre-servlet.xml文件
引入freemaker的支持,并且需要将freemaker的controller放在jsp的controller之前
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><mvc:annotation-driven /><context:component-scan base-package="org.konghao.fre.controller"></context:component-scan><mvc:resources location="/resources/" mapping="/resources/**" /><bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/WEB-INF/ftl/"/></bean><bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="true"/> <property name="prefix" value=""/> <property name="suffix" value=".ftl"/> <property name="contentType" value="text/html; charset=UTF-8"></property></bean><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/jsp/"></property><property name="suffix" value=".jsp"></property></bean><bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"><property name="mediaTypes"><map><entry key="json" value="application/json" /><entry key="xml" value="text/xml" /><entry key="htm" value="text/html" /></map></property><property name="defaultContentType" value="text/html" /></bean></beans>
3.HelloController的编写
package org.konghao.fre.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class HelloController {@RequestMapping("/hello")public String hello(Model model) {model.addAttribute("username", "张三");return "hello/hello";}@RequestMapping("/helloworld")public String helloworld(Model model) {model.addAttribute("username","张是");return "world";}}
4.编写freemaker的模板文件
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>${username}</h1></body></html>
5.编写不会jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!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>${username }</body></html>
6.启动jetty测试,或者是tomcat测试
访问hello和helloworld看看是否可以正常显示
- freemaker与spring的整合(三)
- (三)spring与Hibernate的整合
- spring整合velocity和freemaker
- spring 整合freemaker 模板引擎
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- ibatis学习(三)---ibatis与spring的整合
- Spring与Hibernate整合(三)
- WebService与Spring整合《三》
- spring 学习之三(spring 与hibernate, struts2整合)
- 三种整合 Struts 应用程序与 Spring 的方式
- Spring与struts 整合的三种方式
- 三种整合 Struts 应用程序与 Spring 的方式
- Struts2与Spring、Hibernate三者整合的过程示例
- SQL之定值排序
- 用最快的方法判断一个数是不是2的n次方
- 举例说明android中出现java.lang.IllegalStateException: database not open这种错误的原因及解决办法
- uva10085 - The most distant state
- 如何导入mdf文件和ldf文件到sql server 2005
- freemaker与spring的整合(三)
- MFC RTTI实现原理
- MFC的picture控件静态动态加载位图
- 只有真正地懂得取舍才有可能做好一个网站
- UBI文件系统
- ajax+php实例
- VMware虚拟机安装windows8 时报错“Oxc000035a”解决办法
- 如何判断Android设备是手机还是平板?
- Unity学习笔记4:声音和用户界面