配置freemarker,记得加上jar包

<?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:tx="http://www.springframework.org/schema/tx"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xmlns:mvc="http://www.springframework.org/schema/mvc" 
	   xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/tx 
	   http://www.springframework.org/schema/tx/spring-tx.xsd 
	   http://www.springframework.org/schema/aop 
	   http://www.springframework.org/schema/aop/spring-aop.xsd 
	   http://www.springframework.org/schema/mvc 
	   http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context 
	   http://www.springframework.org/schema/context/spring-context.xsd">
	<mvc:annotation-driven/>
	<context:component-scan base-package="com.sawshaw" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	 <!-- 视图解析器1:html视图解析器 必须先配置freemarkerConfig,注意html是没有prefix前缀属性的 -->
	 <bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="freemarkerSettings">
			<bean
				class="org.springframework.beans.factory.config.PropertiesFactoryBean">
				<property name="properties">
					<props>
						<prop key="default_encoding">utf-8</prop>
						<prop key="output_encoding">utf-8</prop>
					</props>
				</property>
			</bean>
		</property>
		<property name="templateLoaderPath">
			<value>/WEB-INF/views/</value>
		</property>
	</bean>
	<bean id="htmlviewResolver"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
		p:suffix=".html" p:order="0">
		<property name="contentType" value="text/html;charset=UTF-8" />
	</bean>
	<!-- 视图解析器2:jsp视图解析器 -->
	<bean id="jspviewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/views/" p:suffix=".jsp" p:order="1">
		<property name="contentType" value="text/html;charset=UTF-8" />
	</bean>
    <!-- <mvc:resources location="/" mapping="*.html"/> -->
	<mvc:resources location="/pages/" mapping="/pages/*"/>
	<mvc:resources location="/img/" mapping="/img/**"/>
	<mvc:resources location="/css/" mapping="/css/**"/>
	<mvc:resources location="/js/" mapping="/js/**"/>
</beans>

 

Controller

package com.sawshaw.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

/**
 * @author mercy
 *页面跳转功能
 */
@Controller
@RequestMapping("/me")
public class PageFowardController {
	@RequestMapping("/greeting")
	@ResponseBody
	public String greeting(){
		JSONObject js=new JSONObject();
		js.put("id", "myId");
		js.put("content", "mycontent");
		return js.toJSONString();
	}
	//转向web-inf的hello.jsp
	@RequestMapping("/getHelloUrl")
	public ModelAndView handleRequest( HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView("forward:/WEB-INF/views/hello.jsp");
		return mav;
	}
	//不配置freemarker返回web-infd的hello.jsp,配置了返回 web-inf的hello.html
	@RequestMapping("/getHelloUrl0")
	public String handleRequest0( HttpServletRequest request,HttpServletResponse response){
		return "hello";
	}
	//转发请求
	@RequestMapping("/getHelloUrl1")
	public void handleRequest1( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getRequestDispatcher("getHelloUrl7").forward(request, response);
	}
	
	/**
	 * @param request
	 * 这个方法是重定向webapp下的hello.html
	 * @param response
	 * @return
	 */
	@RequestMapping("/getHelloUrl2")
	public ModelAndView handleRequest2( HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView("redirect:/hello.html");
	    return mv;
	}
	/**
	 * @param request
	 * 这个方法是重定向webapp下的hello.html
	 * @param response
	 * @return
	 */
	@RequestMapping("/getHelloUrl3")
	public String handleRequest3( HttpServletRequest request,HttpServletResponse response){
		return "redirect:/hello.html";
	}
	
	/**
	 * @param request
	 * 这个方法是重定向webapp下的hello.html
	 * @param response
	 * @return
	 */
	@RequestMapping("/getHelloUrl4")
	public void handleRequest4( HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.sendRedirect(request.getContextPath()+"/hello.html");
	}
	
	//返回webroot的hello.jsp
	@RequestMapping("/getHelloUrl5")
	public void handleRequest5( HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		//失败
		//request.getRequestDispatcher("hello.jsp").forward(request, response);
		//成功
		response.sendRedirect(request.getContextPath()+"/hello.jsp");
	}
	//配置freemarker后返回web-inf的hello.html,有同名的html和jsp优先返回html
	@RequestMapping("/getHelloUrl6")
	public ModelAndView handleRequest6( HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView("hello");
		return mav;
	}
	//返回web-inf的nihao.jsp
	@RequestMapping("/getHelloUrl7")
	public ModelAndView handleRequest7( HttpServletRequest request,HttpServletResponse response){
		ModelAndView mav = new ModelAndView("nihao");
		return mav;
	}
	
	@RequestMapping("/postData")
	@ResponseBody
	public String postData(HttpServletRequest request,HttpServletResponse response){
		String userName=request.getParameter("userName");
		System.out.println("userName:"+userName);
		return userName;
	}

}

  

项目结构:

 springMVC访问 WEB-INF 下的 jsp 和 html_xml

作者: lost blog

 

关于作者:专注服务器端开发

本文版权归作者和共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接 如有问题,