Spring MVC 整合sitemesh, freemarker 支持完全REST方式

使用Spring MVC REST 方式,希望使用如下的URL:
/users/1
/users


public class FreemarkerDispatcherServlet extends DispatcherServlet {
	public static final long serialVersionUID = -2440216393145762479L;

	private static final UrlPathHelper urlPathHelper = new UrlPathHelper();

	private FreemarkerDecoratorServlet servlet = new FreemarkerDecoratorServlet();

	@Override
	protected void initFrameworkServlet() throws ServletException {
		servlet.init(getServletConfig());
	}

	@Override
	protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String path = urlPathHelper.getRequestUri(request);

		System.out.println(path);
		if (StringUtils.startsWith(path, "/WEB-INF/decorators/")) {
			servlet.doGet(request, response);
		} else {
			super.doService(request, response);
		}
	}
}


web.xml中的配置:
<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>freemarker.ext.servlet.FreemarkerDispatcherServlet</servlet-class>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<init-param>
			<param-name>default_encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.gif</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

你可能感兴趣的:(spring,mvc,freemarker,servlet,REST)