web.xml
....
<servlet>
<servlet-name>chuck</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>chuck</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
...
chuck-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
-->
<!-- -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.freemarker.FreeMarkerView
</value>
</property>
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.ftl</value>
</property>
<property name="contentType" value="text/html;charset=utf-8"></property>
</bean>
<bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test.action">TestController</prop>
<prop key="/test2.action">TestController2</prop>
</props>
</property>
</bean>
<bean id="TestController" class="org.chuck.springmvccontrol.TestController"></bean>
<bean id="TestController2" class="org.chuck.springmvccontrol.TestController2"></bean>
</beans>
TestController.java
package org.chuck.springmvccontrol;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class TestController implements Controller {
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
Map root = new HashMap();
String message = "[这是一个消息]";
String user = "chuck";
root.put("message", message);
root.put("username", user);
return new ModelAndView("subfolder/test", root);
}
}
TestController2.java
package org.chuck.springmvccontrol;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class TestController2 implements Controller {
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
NewsItem nt = new NewsItem();
nt.setTitle("标题");
nt.setShowContent("内容");
Map root = new HashMap();
root.put("newsitem", nt);
nt.setAddTime(Calendar.getInstance().getTime().toString());
return new ModelAndView("template", root);
}
}
NewsItem.java
package org.chuck.springmvccontrol;
public class NewsItem {
public String title;
public String addTime;
public String showContent;
setXXX()....
getXXX()...
}
WEB_INF/view/subfolder/test.ftl
<html>
<head>
<title>FROM test.ftl</title>
</head>
<body>
${message}
:<font color=red>${username}</font>
</body>
</html>
WEB_INF/view/template.ftl
<html>
<head>
<title>Title:${newsitem.title} </title>
</head>
<body>
<table width="100%" border="0�� cellpadding="0�� cellspacing="0�� bgcolor="#FFFFFF">
<tr><td>
<table width="95%" border="0�� align="center" cellpadding="2�� cellspacing="6�� >
<tr>
<td height="10�� align="left" colspan=2 ></td>
</tr>
<tr>
<td align="left" width="538"�� >
<strong>${newsitem.title}</strong> ( ${newsitem.addTime} )
</td>
<td align="right">
<a name="shit">hello world</a>
</td>
</tr>
<tr>
<td align="left" valign=top colspan=2>
<hr align="left" width="95%" size="1�� noshade color="#cc0000�� >
</td>
</tr>
<tr>
<td colspan=2>${newsitem.showContent}
</td>
</tr>
</table>
<br>
</td></tr>
</table>
</body>
</html>