配置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的更多相关文章

  1. 【servlet】客户端是否可以访问到WEB-INF下的jsp文件

    一般情况下(不考虑出现安全问题被入侵,那样啥都能访问到),WEB-INF下的jsp文件单凭浏览器端请求时访问不到的. 想访问的话需要通过服务端servlet的转发. 下面通过转发和重定向的尝试来观察访 ...

  2. springmvc 拦截器,不拦截jsp文件

    spring mvc的拦截器只拦截controller不拦截jsp文件,如果不拦截jsp文件也会给系统带安全性问题. 解决方案有两种: 1.将所有的jsp文件放入到WEB-INF文件夹下,这样用户是直 ...

  3. 记录-springMVC访问web-inf下文件问题+在jsp页面导入jquery插件路径不对问题

    环境:spring + springMvc + mybatis + maven 关于在springMVC环境访问web-inf目录下文件,其一有在springMVC xml文件下加 <!-- 对 ...

  4. springMVC框架访问web-inf下的jsp文件

    博客原文章:http://td.xue163.com/1042/1/10425265.html 用户提出问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC ...

  5. 关于springMVC框架访问web-inf下的jsp文件

    问题:springMVC框架访问web-inf下的jsp文件,具体如下: 使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <property name=&qu ...

  6. 访问WEB-INF下的jsp页面

      访问web-inf下的jsp文件, 1)使用springMVC,一般都会使用springMVC的视图解析器,大概会这样配置 <!--jsp视图解析器--> <bean class ...

  7. 关于Spring MVC 中地址栏访问 /WEB-INF下的.jsp

    WEB-INF是对资源的保护,直接在地址栏访问WEB-INF目录下的页面,会显示404,关于为什么要把页面放在WEB-INF下,可以自行百度 在这里我是用SpringMVC 对WEB-INF目录下的页 ...

  8. java:maven中webapp下的jsp不能访问web-inf下面的bean

    java:maven中webapp下的jsp不能访问web-inf下面的bean 当然 WEB-INF下面的文件是不能访问的,只能吧jsp文件放入到WEB-INF下面,然后通过配置WEB-INF下we ...

  9. java web中servlet、jsp、html 互相访问的路径问题

    java web中servlet.jsp.html 互相访问的路径问题 在java web种经常出现 404找不到网页的错误,究其原因,一般是访问的路径不对. java web中的路径使用按我的分法可 ...

  10. XAMPP环境访问非Web DocumentRoot下绝对路径

    假设你的XAMPP网站文档根目录在C:/xampp/apache/htdocs/下面,那么访问这个目录下的文件是很直接的. 但是有时候需要把用户上传文件指定到特殊目录,比如E盘,那么就需要用户能够访问 ...

随机推荐

  1. Gridview、DataList、Repeater获取行索引号

    Gridview.DataList.Repeater如何获取行索引号?很多情况下都会用得到,下面贴出代码,注意行索引号是从0开始,不是从1开始,如果要从1开始,请在代码里面+1就行了. Gridvie ...

  2. android project 文件夹

    android多国语言文件夹 http://www.blogjava.net/zhaojianhua/archive/2012/02/09/369676.html Android平板开发精确适配不同的 ...

  3. 图像添加到ABBYY 文档有什么方法

    ABBYY FineReader 12作为一款功能全面的OCR图文识别软件,我们自是可以在其中将图像添加到FineReader文档中去,且添加在文档尾部,否则将会创建一个新的FineReader文档. ...

  4. QT 实现QGraphicsProxyWidget对象可选择或移动(item管理实现)

    上篇博文<QT QGraphicsProxyWidget对象可选择或移动的一些tricks>介绍了实现QT QGraphicsProxyWidget对象可选择或移动的一些小的第三方技巧,但 ...

  5. Chrome扩展应用

    现在越来越多的用户将chrome浏览器设置为自己默认的浏览器,不仅是因为他的界面美,最重要的是他对html5和CSS3完美的支持,且调试工具非常好用,还有丰富的扩展库.如何安装自己的扩展呢? 点击自定 ...

  6. iOS 开发,工程中混合使用 ARC 和非ARC(转)

    [前提知识] ARC:Automatic Reference Counting,自动引用计数 在开发 iOS 3 以及之前的版本的项目时我们要自己负责使用引用计数来管理内存,比如要手动 retain. ...

  7. SpringMVC由浅入深day01_12.4 pojo绑定_12.5自定义参数绑定实现日期类型绑定_12.6集合类

    12.4 pojo绑定 页面中input的name和controller的pojo形参中的属性名称一致,将页面中数据绑定到pojo. 页面定义: controller的pojo形参的定义: 打断点测试 ...

  8. 升级MAC OX上的Python到3.4

    第1步:下载Python3.4 下载地址如下: 下载Mac OS X 64-bit/32-bit installerhttps://www.python.org/downloads/release/p ...

  9. Python之虚拟环境管理

    Python本身有很多个版本,第三方的Python包又有很多可用的版本,所以经常会遇到下面的问题: 运行不同的Python程序,需要使用不同版本的Python(2.x或3.x). 在同一中Python ...

  10. date 类型转为varchar

    select t.type_id as typeId, t.type_name as typeName, t.type_order as typeOrder, t.type_link as typeL ...