freemarker 整合springMVC
web-config.xml配置:
<?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.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.test.controller" />
<!-- freemarker config 在其它视图之前配置 ,当找不到视图时,会向下执行下面视图配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/ftl/" /> <!--配置 模板加载路径 -->
</bean>
<bean id="freeMarker"
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"/> <!-- 处理乱码 -->
</bean>
<!-- 页面View层基本信息设定 拦截所有视图请求,当找不到时报404,所以放后面-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" /> <!-- 如果使用jstl的话,配置的属性 -->
<property name="suffix" value=".jsp" /> <!-- 后缀 -->
<property name="prefix" value="/WEB-INF/jsp/" /> <!-- 前缀 -->
</bean>
</beans>
controller
@Controller
public class HelloCtroller {
@RequestMapping(value="hello")
public String hello(Model model){
model.addAttribute("username","张三");
return "hello";
}
@RequestMapping(value="world")
public String helloworld(Model model){
model.addAttribute("username","李四");
return "world"; //http://localhost:8080/freemarker/world 在freeMarker视图配置中 找不到视图world.ftl,则用全局视图找到world.jsp,故返回world.jsp
}
}
文件组织结构:
freemarker 整合struts2
解压struts2-core-X.X.X.jar文件,把在META-INF文件夹下面的struts-tags.tld文件复制到WEB-INF文件夹下
在web.xml文件中配置freemark同时启动JSPSupportServlet. web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>freemarker</servlet-name>
<servlet-class>
freemarker.ext.servlet.FreemarkerServlet
</servlet-class>
<!--下面的配置freemarke的ftl文件的位置 -->
<init-param>
<param-name>TemplatePath</param-name>
<param-value>/</param-value>
</init-param>
<!-- 是否和服务器(tommcat)一起启动。0为不。1为是-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>freemarker</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
<servlet>
<!-- define a JspSupportServlet Object -->
<servlet-name>JspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<!-- setting JspSupportServlet auto start -->
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" extends="struts-default">
<action name="test" class="com.sxt.test.TestAction">
<result name="success" type="freemarker">test.ftl</result>
</action>
</package>
</struts>
TestAction
public class TestAction extends ActionSupport {
private String companyName="尚学堂";
public String execute() throws Exception {
System.out.println("TestAction.execute()");
return SUCCESS;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
test.ftl
<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"] /> <#--这一句是为了让Freemarker识别struts2的标签 指出struts-tags.tld位置-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<h1>${companyName}</h1>
<@s.form action="reg" >
<@s.textfield label="用户名" name="uname" />
<@s.submit />
</@s.form>
</body>
</html>
组织结构: