配置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. MathType输入框怎么调整

    在用MathType编辑公式编辑器时,除了可以对MathType工具栏的显示比例进行调整以外,还可以对编辑时的输入框进行调整.这样在编辑的过程中,工具栏可以看得很清楚,同时框输入框也可以看得很清楚,这 ...

  2. cesium导入3D模型(obj转gltf)

    cesium中支持载入3D模型,不过只支持gltf格式.gltf是khronos组织(起草OpenGL标准的那家)定义的一种交换格式,用于互联网或移动设备上展现3d内容,充分支持opengl,webg ...

  3. WIN7隐藏GUEST登录账户

    在Windows7中,我们有时候需要开启Guest用户,以方便给别的同事共享打印机和部分文件,但同时又不希望别人用Guest账号从本地登陆界面进入本机.这个时候就需要将本地登陆界面的Guest用户进行 ...

  4. HTML5标签canvas制作平面图

    摘要: HTML5规范已经完成了,互联网上已经有数不清的站点使用了HTML5.从现在开始研究HTML5,本文是自己在学习canvas过程中的记录,以备后需. 历史: 这个 HTML 元素是为了客户端矢 ...

  5. 表单验证&lt;AngularJs&gt;

    经常使用的表单验证指令 1. 必填项验证 某个表单输入是否已填写,仅仅要在输入字段元素上加入HTML5标记required就可以: <input type="text" re ...

  6. Hibernate_day03讲义_使用Hibernate完成一对多的关系映射并操作

  7. [AX2012]关于财务默认维度

    和以前的版本一样,AX2012中很多地方都使用财务维度,比如客户.销售订单.销售订单行等,根据相应的财务维度设置,生成的相应财务分录将带有财务维度,方便后续对财务分录交易的分析.下图是在客户记录上设置 ...

  8. Python "HTTP Error 403: Forbidden"

    问题: 执行下面的语句时 def set_IPlsit(): url = 'https://www.whatismyip.com/' response = urllib.request.urlopen ...

  9. Windows 2012 R2 安装Nessus

    1.nessus官网注册 注册地址:https://www.tenable.com/products/nessus-home Name字段随意,邮箱需要填写自己的,方便接受注册码 2.注册后,登录邮箱 ...

  10. [JS] 如何自定义字符串格式化输出

    在其他语言中十分常见的字符串格式化输出,居然在 Javascript 中不见踪影,于是决定自己实现该方法,以下就是个人编写的最简洁实现: String.prototype.format = funct ...