配置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. saltstack系列2之zabbix-agent自动化部署

    实施前提条件 zabbix-agent通过编译安装的,打成tar包,并且自己先配好master的ip等等之类的配置在/etc下,然后我们后面只需要修改一个Hostname这个配置项即可.. salts ...

  2. 小波变换——子带编码,Subband Coding

    离散小波变换.多级信号分解,多分辨率分析. Multiresolution Analysis(MRA.多分辨率分析) 子带编码(Subband Coding) 简称SBC. 一种以信号频谱为依据的编码 ...

  3. mysql中json_remove函数的使用?

    需求描述: 今天看json记录,可以通过json_remove函数对一个key或多个key从个json记录中去掉. 操作过程: 1.查看一个已经存在的json表 mysql> select * ...

  4. GoF--装饰者设计模式

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例. 装饰器模式的应用场景: 1.需要扩展一个类的功能. 2.动态的为 ...

  5. [AX2012]代码更改默认财务维度

    在前文(http://www.cnblogs.com/duanshuiliu/p/3243048.html)最后演示了如何使用代码更改默认财务维度,那段代码模拟了创建各数据表记录的过程,实际上AX提供 ...

  6. js节流函数高级版

    节流函数其主要作用就是防止用户在短时间内多次触发该事件. <!DOCTYPE html> <html lang="en"> <head> < ...

  7. iOS 严重问题解释(crash)

    问题1:Exception Type: 00000020 Exception Codes: 0x000000008badf00d Exception Note: SIMULATED (this is  ...

  8. MySQL on Linux 部署手册

    1. 背景 MySQL为开源数据库,因此可以基于源码实现安装.基于源码安装有更多的灵活性.也就是说我们可以针对自己的硬件平台选用合适的编译器来优化编译后的二进制代码,根据不同的软件平台环境调整相关的编 ...

  9. mysql的联表删除

    联表删除: 1.从数据表t1 中把那些id值在数据表t2 里有匹配的记录全删除掉 DELETE t1 FROM t1,t2 WHERE t1.id=t2.id    或DELETE  FROM t1 ...

  10. Cookie利用神器:CookieHacker

    转自evilcos的博客 看到那么多苦逼的跨站师在问Cookie利用工具,不忍心,还是把自己写的Chrome扩展开源出来吧,功能极简,仿造<我的渗透利器>里提到的Original Cook ...